00001
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
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
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
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
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
00106
00107
00108
00109
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
00126
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
00135 : TypedMutator<T>(t), comment(c) {}
00136 virtual ~CommentedMutator() {}
00137
00138 virtual std::string description() const { return comment;}
00139 };
00140
00142
00144
00145
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
00158 template<class T>
00159 inline NotifyOnChangeMutator<T>* GetNotifyingMutator(T& t, controlable& observ)
00160 { return new NotifyOnChangeMutator<T>(t,observ);}
00161
00162
00163 inline SetTrueOnReadMutator* GetTrueOnReadMutator(bool& t)
00164 { return new SetTrueOnReadMutator(t);}
00165
00166
00167 inline SetFalseOnReadMutator* GetFalseOnReadMutator(bool& t)
00168 { return new SetFalseOnReadMutator(t);}
00169
00170
00171 inline FlipOnReadMutator* GetFlipOnReadMutator(bool& t)
00172 { return new FlipOnReadMutator(t);}
00173
00174
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
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