00001 #ifndef NMWR_GB_MUTATOR_H
00002 #define NMWR_GB_MUTATOR_H
00003
00004
00005
00006
00007
00008
00009 #include <iostream.h>
00010 #include <stdlib.h>
00011
00012 #include "Config/compiler-config.h"
00013 #include "IO/mutator-base.h"
00014
00015 #include "Utility/pre-post-conditions.h"
00016 #include "Utility/type-name-traits.h"
00017
00018
00019
00020
00021
00022
00023
00024
00026
00030 template<class T>
00031 class TypedMutator : public Mutator {
00032
00033 public:
00034 T& v;
00035 public:
00036 TypedMutator(T& vv) : v(vv) {}
00037
00038 virtual void read(std::istream& in) {
00039 REQUIRE_ALWAYS((in.good()), "Stream not good! v = " << v << '\n',1);
00040 in >> v;
00041 ENSURE_ALWAYS((in.eof() || in.good()), "Input failed! v = " << v << '\n',1);
00042 }
00043 virtual void print(std::ostream& out) const
00044 { out << v;}
00045 virtual void print(std::ostream& out, const std::string& prefix = "") const
00046 { out << prefix << v;}
00047
00048 virtual std::string vartypename() const { return std::string(type_name_traits<T>::name());}
00049 };
00050
00051
00053 template<class T>
00054 class NotifyOnChangeMutator : public TypedMutator<T> {
00055 public:
00056 typedef TypedMutator<T> base;
00057 NotifyOnChangeMutator(T& t, controlable& c)
00058 : base(t), controlee(c) {}
00059 virtual void read(std::istream& in) {
00060 T old(value());
00061 base::read(in);
00062 if( old != value()) controlee.notify();
00063 }
00064 private:
00065 controlable& controlee;
00066 };
00067
00068
00070
00076 class SetTrueOnReadMutator : public TypedMutator<bool> {
00077 typedef TypedMutator<bool> tm;
00078 public:
00079 SetTrueOnReadMutator(bool& flag) : tm(flag) {}
00080 virtual void read(std::istream&) { v = true;}
00081 virtual void print(std::ostream& out) const { out << v;}
00082 virtual void print(std::ostream& out, const std::string& name) const
00083 { if(v) out << name;}
00084 };
00085
00087 class SetFalseOnReadMutator : public TypedMutator<bool> {
00088 typedef TypedMutator<bool> tm;
00089 public:
00090 SetFalseOnReadMutator(bool& flag) : tm(flag) {}
00091 virtual void read(std::istream&) { v = false;}
00092 virtual void print(std::ostream& out) const { out << !v;}
00093 virtual void print(std::ostream& out, const std::string& name) const
00094 { if(!v) out << name;}
00095 };
00096
00097
00099
00105 class FlipOnReadMutator : public TypedMutator<bool> {
00106 typedef TypedMutator<bool> tm;
00107 public:
00108 FlipOnReadMutator(bool& flag) : tm(flag) {}
00109 virtual void read(std::istream&) { v = !v;}
00110 virtual void print(std::ostream& out) const { out << v;}
00111 virtual void print(std::ostream& out, const std::string& name) const
00112 { out << name;}
00113 };
00114
00115
00117
00123 template<class T, class Tsec>
00124 class SetOnReadMutator : public TypedMutator<T> {
00125 typedef TypedMutator<T> tm;
00126 protected:
00127 Tsec& v2;
00128 Tsec deflt;
00129 public:
00130 SetOnReadMutator(T& t, Tsec& t2, Tsec def) : tm(t), v2(t2), deflt(def) {}
00131 virtual void read(std::istream& in) { tm::read(in); v2 = deflt;}
00132 };
00133
00134
00135
00137 template<class T>
00138 class CommentedMutator : public TypedMutator<T> {
00139 typedef TypedMutator<T> tm;
00140 std::string comment;
00141 public:
00142 CommentedMutator(T& t, const std::string& c)
00143
00144 : TypedMutator<T>(t), comment(c) {}
00145
00146 virtual std::string description() const { return comment;}
00147 };
00148
00150
00151
00152
00153
00154
00155
00156
00157
00158 class MessageOnReadMutator : public Mutator {
00159 private:
00160 std::ostream* out;
00161 std::string text;
00162 public:
00163 MessageOnReadMutator(std::ostream & ou, std::string const& txt)
00164 : out(&ou), text(txt) {}
00165 virtual void read (std::istream& in) { (*out) << text; exit(0); }
00166 virtual void print(std::ostream& ) const {}
00167 virtual void print(std::ostream& , std::string const& ) const {}
00168
00169 virtual std::string vartypename() const { return "";}
00170
00171 };
00172
00174
00176
00177
00178 template<class T>
00179 inline TypedMutator<T>* GetMutator(T& t) { return new TypedMutator<T>(t);}
00180
00181 template<class T>
00182 inline CommentedMutator<T>* GetMutator(T& t, std::string const& comment)
00183 { return new CommentedMutator<T>(t,comment);}
00184
00185 template<class T>
00186 inline CommentedMutator<T>* GetMutator(T& t, const char* comment)
00187 { return new CommentedMutator<T>(t,comment);}
00188
00189
00190 template<class T>
00191 inline NotifyOnChangeMutator<T>* GetNotifyingMutator(T& t, controlable& observ)
00192 { return new NotifyOnChangeMutator<T>(t,observ);}
00193
00194
00195 inline SetTrueOnReadMutator* GetTrueOnReadMutator(bool& t)
00196 { return new SetTrueOnReadMutator(t);}
00197
00198
00199 inline SetFalseOnReadMutator* GetFalseOnReadMutator(bool& t)
00200 { return new SetFalseOnReadMutator(t);}
00201
00202
00203 inline FlipOnReadMutator* GetFlipOnReadMutator(bool& t)
00204 { return new FlipOnReadMutator(t);}
00205
00206
00207 template<class T, class TObs>
00208 inline SetOnReadMutator<T,TObs>* GetSetOnReadMutator(T& t, TObs& obs, TObs deflt)
00209 { return new SetOnReadMutator<T,TObs>(t,obs,deflt); }
00210
00211
00212 template<class T>
00213 inline CommentedMutator<T>* GetCommentedMutator(T& t, std::string const& comment)
00214 { return new CommentedMutator<T>(t,comment); }
00215
00216 template<class T>
00217 inline CommentedMutator<T>* GetCommentedMutator(T& t, const char* comment)
00218 { return new CommentedMutator<T>(t,std::string(comment)); }
00219
00220 #endif