AMROC Main     Blockstructured Adaptive Mesh Refinement in object-oriented C++


Main Page   Class Hierarchy   Compound List   File List  

mutator.h

Go to the documentation of this file.
00001 #ifndef NMWR_GB_MUTATOR_H
00002 #define NMWR_GB_MUTATOR_H
00003 
00004 
00011 #include <iostream.h>
00012 #include "stl_CompConf.h"
00013 #include "string_CompConf.h"
00014 #include "IO/mutator-base.h"
00015 
00016 
00017 // simplest concrete Mutator
00018 template<class T>
00019 class TypedMutator : public Mutator {
00020 protected:
00021   T& v;
00022 public:
00023   TypedMutator(T& vv) : v(vv) {}
00024   T value() {return v;}
00025   virtual void read(istream& in)   { in >> v;}
00026   virtual void print(ostream& out) const 
00027     { out << v;}
00028   virtual void print(ostream& out, const string& prefix = "") const 
00029     { out << prefix << v;}
00030 };
00031 
00032  
00033 // notify controlee, if variable changes.
00034 template<class T>
00035 class NotifyOnChangeMutator : public TypedMutator<T> {
00036 public:
00037   typedef  TypedMutator<T> base;
00038   NotifyOnChangeMutator(T& t, controlable& c)
00039     : base(t), controlee(c) {}
00040   virtual void read(istream& in) {
00041     T old(value());
00042     base::read(in);
00043     if( old != value()) controlee.notify();
00044   }
00045 private:
00046   controlable& controlee;
00047 };
00048 
00049 
00050 /*
00051 template<class T>
00052 class DebugTypedMutator : public TypedMutator<T> {
00053   typedef TypedMutator<T> tm;
00054 public: 
00055   DebugTypedMutator(T& t) : tm(t) {}
00056   virtual void read(istream& in) {
00057     cerr << "Mutator: old value: " << value();
00058     tm::read(in);
00059     cerr << "  new value: " << value();
00060    }
00061   virtual void print(ostream& out)
00062     { tm::print(out); out << " (@ " << (void*)&v << ")";}
00063 };
00064 */
00065 
00066 class SetTrueOnReadMutator : public TypedMutator<bool> {
00067   typedef TypedMutator<bool> tm;
00068  public:
00069   SetTrueOnReadMutator(bool& flag) : tm(flag) {}
00070   virtual void read(istream&) { v = true;}
00071   virtual void print(ostream& out) const { out << v;}
00072   virtual void print(ostream& out, const string& name) const 
00073     { if(v) out << name;} 
00074 };
00075 
00076 
00077 class SetFalseOnReadMutator : public TypedMutator<bool> {
00078   typedef TypedMutator<bool> tm;
00079  public:
00080   SetFalseOnReadMutator(bool& flag) : tm(flag) {}
00081   virtual void read(istream&) { v = false;}
00082   virtual void print(ostream& out) const { out << !v;}
00083   virtual void print(ostream& out, const string& name) const 
00084     { if(!v) out << name;} 
00085 };
00086 
00087 
00088 
00089 class FlipOnReadMutator : public TypedMutator<bool> {
00090   typedef TypedMutator<bool> tm;
00091  public:
00092   FlipOnReadMutator(bool& flag) : tm(flag) {}
00093   virtual void read(istream&) { v = !v;}
00094   virtual void print(ostream& out) const { out << v;}
00095   virtual void print(ostream& out, const string& name) const 
00096     {  out << name;} 
00097 };
00098 
00099 // examples: 
00100 // Ctrl.add("show", new SetTrueOnRead(show));
00101 //  will set show to true if read,
00102 // Ctrl.add("toggle-binary", new SetTrueOnRead(bin_flag));
00103 // will flip the value of bin_flag if "toggle-binary" is read.
00104 
00105 
00106 
00107 template<class T, class Tsec>
00108 class SetOnReadMutator : public TypedMutator<T> {
00109   typedef TypedMutator<T> tm;
00110 protected:
00111   Tsec& v2;
00112   Tsec  deflt;
00113 public:
00114   SetOnReadMutator(T& t, Tsec& t2, Tsec def) : tm(t), v2(t2), deflt(def) {}
00115   virtual void read(istream& in) { tm::read(in); v2 = deflt;} 
00116 };
00117 
00118 // example: SetOnReadMutator<double,bool>(x,xread,true)
00119 // will set xread to true if x is read.
00120 
00121 template<class T>
00122 class CommentedMutator : public TypedMutator<T> {
00123   typedef TypedMutator<T> tm;
00124   string comment;
00125 public:
00126   CommentedMutator(T& t, const string& c) 
00127     //: tm(t), comment(c) {}
00128     : TypedMutator<T>(t), comment(c) {}
00129   //  virtual void print(ostream& out) const { tm::print(out); out  << "  " << comment;} 
00130   virtual string description() const { return comment;}
00131 };
00132 
00134 // Mutator-generating Functions
00136 
00137 // simplest: base to all other Mutators below
00138 template<class T>
00139 inline TypedMutator<T>* GetMutator(T& t) { return new TypedMutator<T>(t);}
00140 
00141 template<class T>
00142 inline CommentedMutator<T>* GetMutator(T& t, const string& comment) 
00143 { return new CommentedMutator<T>(t,comment);}
00144  
00145 template<class T>
00146 inline CommentedMutator<T>* GetMutator(T& t, const char* comment) 
00147 { return new CommentedMutator<T>(t,comment);}
00148  
00149 // notify observ if t is read & changed
00150 template<class T>
00151 inline NotifyOnChangeMutator<T>* GetNotifyingMutator(T& t, controlable& observ) 
00152 { return new NotifyOnChangeMutator<T>(t,observ);}
00153 
00154 // t = true if read
00155 inline SetTrueOnReadMutator* GetTrueOnReadMutator(bool& t)
00156 { return new SetTrueOnReadMutator(t);}
00157 
00158 // t = false if read
00159 inline SetFalseOnReadMutator* GetFalseOnReadMutator(bool& t)
00160 { return new SetFalseOnReadMutator(t);}
00161 
00162 // t = !t if read
00163 inline FlipOnReadMutator* GetFlipOnReadMutator(bool& t)
00164 { return new FlipOnReadMutator(t);}
00165 
00166 // obs = deflt if t is read
00167 template<class T, class TObs>
00168 inline SetOnReadMutator<T,TObs>* GetSetOnReadMutator(T& t, TObs& obs, TObs deflt)
00169 { return new SetOnReadMutator<T,TObs>(t,obs,deflt); }
00170 
00171 // write a comment if printed
00172 template<class T>
00173 inline CommentedMutator<T>* GetCommentedMutator(T& t, const string& comment)
00174 { return  new  CommentedMutator<T>(t,comment); }
00175 
00176 template<class T>
00177 inline CommentedMutator<T>* GetCommentedMutator(T& t, const char* comment)
00178 { return  new  CommentedMutator<T>(t,string(comment)); }
00179 
00180 #endif


Quickstart     Users Guide     Programmers Reference     Installation      Examples     Download



AMROC Main      Home      Contact
last update: 06/01/04