alg.hh

Go to the documentation of this file.
00001 // alg.H
00002 // created by ADP
00003 
00004 /*************************************************************************
00005 
00006 Copyright Rice University, 2004.
00007 All rights reserved.
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a
00010 copy of this software and associated documentation files (the "Software"),
00011 to deal in the Software without restriction, including without limitation
00012 the rights to use, copy, modify, merge, publish, distribute, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, provided that the above copyright notice(s) and this
00015 permission notice appear in all copies of the Software and that both the
00016 above copyright notice(s) and this permission notice appear in supporting
00017 documentation.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
00022 RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS
00023 NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL
00024 DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00025 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00026 ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
00027 THIS SOFTWARE.
00028 
00029 Except as contained in this notice, the name of a copyright holder shall
00030 not be used in advertising or otherwise to promote the sale, use or other
00031 dealings in this Software without prior written authorization of the
00032 copyright holder.
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       virtual void setState(const T & x) = 0;
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

Generated on 5 Jan 2017 for RvlAlg by  doxygen 1.4.7