00001 #ifndef _CONFIG_VARIABLE_H__
00002 #define _CONFIG_VARIABLE_H__
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include <sstream>
00007
00008 enum
00009 {
00010 CONFIG_VARIABLE_SUCCESS = 0,
00011 CONFIG_VARIABLE_FAILURE = 1,
00012 CONFIG_VARIABLE_READONLY = 2,
00013 CONFIG_VARIABLE_CACHED = 3,
00014 CONFIG_VARIABLE_READ = 4
00015 };
00016
00017 class ConfigVariable
00018 {
00019
00020
00021 private:
00022 protected:
00023 std::string m_name;
00024 unsigned int m_namelength;
00025 void* m_thisref;
00026 public:
00027
00028 ConfigVariable(const char* name)
00029 {
00030 m_thisref=NULL;
00031 m_name=std::string(name);
00032 }
00033
00034 virtual ~ConfigVariable()
00035 {}
00036
00037 virtual std::string toString()=0;
00038 virtual int fromString(std::string)=0;
00039
00040 std::string getName(){return m_name;}
00041 };
00042
00043 class ConfigVariableInt : public ConfigVariable
00044 {
00045 typedef int (*variableSetHandler) (void* classref,const int* value);
00046 typedef int (*variableGetHandler) (void* classref,int* value);
00047
00048 private:
00049 protected:
00050 variableGetHandler m_vgh;
00051 variableSetHandler m_vsh;
00052 public:
00053
00054 ConfigVariableInt(const char* name, variableGetHandler vgh,variableSetHandler vsh,void* classref): ConfigVariable(name)
00055 {
00056 m_vgh=vgh;
00057 m_vsh=vsh;
00058 m_thisref=classref;
00059 }
00060 virtual std::string toString()
00061 {
00062
00063 ostringstream oss(ostringstream::out);
00064 int value=0;
00065 int retval = m_vgh(m_thisref,&value);
00066 if(retval==CONFIG_VARIABLE_SUCCESS)
00067 {
00068 oss << value;
00069 return oss.str();
00070 }
00071 return std::string("CONFIG_VARIABLE_FAILURE");
00072 }
00073 virtual int fromString(std::string s)
00074 {
00075 int mvalue=strtol(s.c_str(),NULL,10);
00076 return m_vsh(m_thisref,&mvalue);
00077 }
00078 };
00079
00080
00081 class ConfigVariableString : public ConfigVariable
00082 {
00083
00084 typedef int (*variableSetHandler) (void* classref,const char* value);
00085 typedef int (*variableGetHandler) (void* classref,char* value);
00086 private:
00087 protected:
00088 variableGetHandler m_vgh;
00089 variableSetHandler m_vsh;
00090 public:
00091
00092 ConfigVariableString(const char* name,variableGetHandler vgh,variableSetHandler vsh,void* classref): ConfigVariable(name)
00093 {
00094 m_vgh=vgh;
00095 m_vsh=vsh;
00096 m_thisref=classref;
00097 }
00098 virtual std::string toString()
00099 {
00100
00101 char buffer[1024];
00102 memset(buffer,0,1024);
00103 int retval = m_vgh(m_thisref,buffer);
00104 if(retval==CONFIG_VARIABLE_SUCCESS)
00105 {
00106 return std::string(buffer);
00107 }
00108 return std::string("CONFIG_VARIABLE_FAILURE");
00109 }
00110 virtual int fromString(std::string s)
00111 {
00112 if(s.length()>0)
00113 return m_vsh(m_thisref,s.c_str());
00114 return CONFIG_VARIABLE_FAILURE;
00115
00116 }
00117 };
00118
00119
00120 class ConfigVariableBool : public ConfigVariable
00121 {
00122
00123 typedef int (*variableSetHandler) (void* classref,const bool* value);
00124 typedef int (*variableGetHandler) (void* classref,bool* value);
00125 private:
00126 protected:
00127 variableGetHandler m_vgh;
00128 variableSetHandler m_vsh;
00129 public:
00130 ConfigVariableBool(const char* name,variableGetHandler vgh,variableSetHandler vsh,void* classref): ConfigVariable(name)
00131 {
00132 m_vgh=vgh;
00133 m_vsh=vsh;
00134 m_thisref=classref;
00135 }
00136 virtual std::string toString()
00137 {
00138 bool value=false;
00139 int retval=m_vgh(m_thisref,&value);
00140 if(retval!=CONFIG_VARIABLE_SUCCESS)
00141 return std::string("CONFIG_VARIABLE_FAILURE");
00142 if(value)
00143 return std::string("true");
00144 return std::string("false");
00145 }
00146
00147 virtual int fromString(std::string s)
00148 {
00149
00150 bool mvalue = (s.compare("true") == 0);
00151 return m_vsh(m_thisref,&mvalue);
00152 }
00153 };
00154
00155 class ConfigVariableDouble : public ConfigVariable
00156 {
00157 typedef int (*variableSetHandler) (void* classref,const double* value);
00158 typedef int (*variableGetHandler) (void* classref,double* value);
00159
00160 private:
00161 protected:
00162 variableGetHandler m_vgh;
00163 variableSetHandler m_vsh;
00164 public:
00165
00166 ConfigVariableDouble(const char* name,variableGetHandler vgh,variableSetHandler vsh,void* classref): ConfigVariable(name)
00167 {
00168 m_vgh=vgh;
00169 m_vsh=vsh;
00170 m_thisref=classref;
00171 }
00172
00173 virtual std::string toString()
00174 {
00175 ostringstream oss;
00176 double value=0;
00177 int retval = m_vgh(m_thisref,&value);
00178 if(retval==CONFIG_VARIABLE_SUCCESS)
00179 {
00180 oss << value;
00181 return oss.str();
00182 }
00183 return std::string("CONFIG_VARIABLE_FAILURE");
00184 }
00185 virtual int fromString(std::string s)
00186 {
00187 double mvalue=strtod(s.c_str(),NULL);
00188 return m_vsh(m_thisref,&mvalue);
00189 }
00190 virtual std::string toStdString(){}
00191 virtual int fromStdString(std::string){}
00192 };
00193
00194
00195 #endif