00001
00002
00003 #ifndef _H_VARIABLE_H_
00004 #define _H_VARIABLE_H_
00005
00006 namespace std {}
00007 using namespace std;
00008
00009 #include "H_Standard.h"
00010 #include <typeinfo>
00011 #include <map>
00012
00013 class HVariable
00014 {
00015 public:
00016 HVariable(const char *name);
00017 template <class InputIterator>
00018 void add(InputIterator first, InputIterator last)
00019 {for (InputIterator I=first ; I!=last ; I++) add(*I);}
00020
00021 virtual const char *name() const = 0;
00022 virtual const char *type() const = 0;
00023 virtual void set_str(const string &) = 0;
00024 virtual string get_str() = 0;
00025 virtual ~HVariable();
00026 public:
00027 string m_name;
00028 };
00029 ostream &operator<< (ostream &out, HVariable &A);
00030
00031 istream &operator>>(istringstream &in, bool &x);
00032
00033 template <class T> class HVar : public HVariable
00034 {
00035 typedef void (*SetFunction)(T value);
00036 typedef T (*GetFunction)();
00037 private:
00038 T *m_value;
00039 SetFunction m_SetFunction;
00040 GetFunction m_GetFunction;
00041 public:
00042 HVar(const char *name, SetFunction set, GetFunction get) :
00043 HVariable(name), m_value(NULL), m_SetFunction(set), m_GetFunction(get)
00044 {}
00045 HVar(const char *name, T *value, SetFunction set = NULL, GetFunction get = NULL) :
00046 HVariable(name), m_value(value), m_SetFunction(set), m_GetFunction(get)
00047 {}
00048 const char *name() const
00049 {
00050 return m_name.c_str();
00051 }
00052 const char *type() const
00053 {
00054 return typeid(T).name();
00055 }
00056
00057 void set(T value)
00058 {
00059 if (m_SetFunction!=NULL)
00060 m_SetFunction(value);
00061 if (m_value!=NULL)
00062 *m_value = value;
00063 }
00064
00065 T get()
00066 {
00067 if (m_GetFunction!=NULL)
00068 return m_GetFunction();
00069 else
00070 return *m_value;
00071 }
00072
00073 void set_str(const string &value)
00074 {
00075 istringstream in(value.c_str());
00076 T Tvalue;
00077 in >> Tvalue;
00078 set(Tvalue);
00079 }
00080 string get_str()
00081 {
00082 ostringstream out;
00083 out << get();
00084 return out.str();
00085 }
00086 };
00087
00088 template <class Obj, class T> class HVarObj : public HVariable
00089 {
00090 typedef void (Obj:: *SetFunction)(T value);
00091 typedef T (Obj:: *GetFunction)();
00092 private:
00093 Obj *m_obj;
00094 T Obj:: *m_value;
00095 SetFunction m_SetFunction;
00096 GetFunction m_GetFunction;
00097 public:
00098 HVarObj(const char *name, Obj *obj, SetFunction set, GetFunction get) :
00099 HVariable(name), m_obj(obj), m_value(NULL), m_SetFunction(set), m_GetFunction(get)
00100 {}
00101 HVarObj(const char *name, Obj *obj, T Obj:: *value, SetFunction set = NULL, GetFunction get = NULL) :
00102 HVariable(name), m_obj(obj), m_value(value), m_SetFunction(set), m_GetFunction(get)
00103 {}
00104 const char *name() const
00105 {
00106 return m_name.c_str();
00107 }
00108 const char *type() const
00109 {
00110 return typeid(T).name();
00111 }
00112
00113 void set(T value)
00114 {
00115 if (m_SetFunction!=0)
00116 (m_obj->*m_SetFunction)(value);
00117 if (m_value!=0)
00118 m_obj->*m_value = value;
00119 }
00120
00121 T get()
00122 {
00123 if (m_GetFunction!=0)
00124 return (m_obj->*m_GetFunction)();
00125 else
00126 return m_obj->*m_value;
00127 }
00128
00129 void set_str(const string &value)
00130 {
00131 istringstream in(value.c_str());
00132 T Tvalue;
00133 in >> Tvalue;
00134 set(Tvalue);
00135 }
00136 string get_str()
00137 {
00138 ostringstream out;
00139 out << get();
00140 return out.str();
00141 }
00142 };
00143
00144
00145 class HExecutable
00146 {
00147 public:
00148 virtual void exec(const Command &c);
00149 virtual void serialize(ostream &out);
00150 virtual ~HExecutable();
00151 };
00152
00153 template <class T> class MethodCall : public HExecutable
00154 {
00155 private:
00156 T *m_object;
00157 void(T:: *m_method)();
00158 public:
00159 MethodCall(T *object,void(T:: *method)()) : m_object(object), m_method(method) {}
00160 virtual void exec(const Command &c)
00161 {
00162 (m_object->*m_method)();
00163 }
00164 virtual ~MethodCall() {}
00165 };
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 class HExecutableSet : public HExecutable
00184 {
00185 public:
00186 virtual ~HExecutableSet();
00187 void serialize(ostream &out);
00188 virtual void exec(const Command &c);
00189 HVariable &get(const string &variable);
00190 void add(HVariable *variable);
00191 private:
00192 map<string,HVariable *> Map;
00193 };
00194
00195 #endif //_H_VARIABLE_H_