00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 #ifndef __RVL_ALG
00037 #define __RVL_ALG
00038 
00039 #include <iostream>
00040 #include <fstream>
00041 #include <sstream>
00042 #include <iomanip>
00043 #include <limits>
00044 #include <exception>
00045 #include <stdexcept>
00046 #include <typeinfo>
00047 #include <string>
00048 #include "except.hh"
00049 
00050 namespace RVLAlg {
00051 
00052   using namespace std;
00053 
00067   class Algorithm {
00068   public:
00069 
00070     Algorithm() {}
00071     virtual ~Algorithm() {}
00072 
00077     virtual void run() = 0;
00078 
00079   };
00080 
00084   class NoAlg: public Algorithm {
00085   public:
00086     void run() {}
00087   };
00088 
00106   class ListAlg: public Algorithm {
00107   public:
00108     ListAlg(Algorithm & first): islist(false), one(first), two(*this) {}
00109     ListAlg(Algorithm & first, Algorithm & next)
00110       : islist(true), one(first), two(next) {}
00111 
00112     virtual void run() {
00113       one.run();
00114       if( islist ) two.run();
00115     }
00116 
00117 
00118   protected:
00119     bool islist;
00120     Algorithm & one;
00121     Algorithm & two;
00122 
00123   };
00124 
00125 #define STOP_LOOP true
00126 #define CONTINUE_LOOP false
00127 
00155   class Terminator {
00156   public: 
00157     virtual ~Terminator() {}
00158     virtual bool query() = 0;
00159   };
00160 
00161 
00170   class LoopAlg: public Algorithm {
00171   public:
00172     LoopAlg(Algorithm & alg, Terminator & stop) : inside(alg), term(stop) { }
00173 
00174     virtual void run() {
00175       try {
00176     while( (!term.query()) ) {
00177       inside.run();
00178     }
00179       }
00180       catch (RVL::RVLException & e) {
00181     e<<"\ncalled from LoopAlg::run\n";
00182     throw e;
00183       }
00184     }
00185 
00186   protected:
00187     Algorithm & inside;
00188     Terminator & term;
00189   };
00190 
00196   class DoLoopAlg: public LoopAlg {
00197   public:
00198     DoLoopAlg(Algorithm & alg, Terminator & stop) : LoopAlg(alg,stop) {}
00199 
00200     virtual void run() {
00201       inside.run();
00202       LoopAlg::run();
00203     }
00204   };
00205 
00213   class CondListAlg: public ListAlg {
00214 
00215   public:
00216     CondListAlg(Algorithm & first, Algorithm & next, Terminator & _stop)
00217       : ListAlg(first,next), stop(_stop) {}
00218 
00219     virtual void run() {
00220       one.run();
00221       if( !stop.query() ) {
00222     two.run();
00223       }
00224     }
00225 
00226   protected:
00227 
00228     Terminator & stop;
00229   };
00230 
00240   template<class T>
00241   class StateAlg: public Algorithm {
00242   public:
00243     
00244 
00245 
00246     virtual T & getState() = 0;
00247     virtual const T & getState() const = 0;
00248   };
00249 
00252   class BranchAlg : public Algorithm {
00253     
00254   public:
00255     BranchAlg(Terminator & iftest, 
00256           Algorithm & thenclause, 
00257           Algorithm & elseclause )
00258       : thencl(thenclause), elsecl(elseclause), test(iftest) {}
00259     
00260     virtual void run() {
00261       if( test.query() )
00262     thencl.run();
00263       else
00264     elsecl.run();
00265     }
00266       
00267   protected:
00268     Algorithm & thencl;
00269     Algorithm & elsecl;
00270     Terminator & test;
00271   };
00272 
00273 
00274 }
00275 
00276 #endif