vtf-logo

mutator.h

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

Generated on Fri Aug 24 13:00:30 2007 for AMROC's Parameter IO - by  doxygen 1.4.7