TemplateValue.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef TEMPLATEVALUE_H_
00028 #define TEMPLATEVALUE_H_
00029
00030
00031 #include <string>
00032
00033
00034 #include "IValue.h"
00035 #include "restore.h"
00036
00037
00038
00039
00040
00041
00042 inline std::string doubleToString(double value) {
00043 char buffer[128];
00044 sprintf(buffer,"% .12lf",value);
00045 return buffer;
00046 }
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 template<class Typ, std::string toString(Typ)=doubleToString>
00057 class TemplateValue : public IValue {
00058 public:
00059
00060
00061
00062
00063
00064
00065 TemplateValue(Typ value, std::string name = "templateValue") : IValue(name), m_value(value) {}
00066
00067
00068
00069
00070 virtual ~TemplateValue() {}
00071
00072
00073
00074
00075
00076 inline Typ getValue(void)const {return m_value;}
00077
00078
00079
00080
00081
00082 inline void setValue(Typ value) {m_value=value;}
00083
00084
00085
00086
00087
00088
00089
00090 virtual IValue* operator*(const IValue& value)const {
00091 TemplateValue<Typ,toString>* newValue;
00092
00093
00094 const TemplateValue<Typ,toString>* castValue = dynamic_cast<const TemplateValue<Typ,toString>* >(&value);
00095 if(castValue==0)
00096 return 0;
00097
00098
00099 const Typ typeValue = castValue->getValue();
00100 newValue = new TemplateValue<Typ,toString>(m_value*typeValue);
00101
00102
00103 return newValue;
00104 }
00105
00106
00107
00108
00109
00110
00111
00112 virtual IValue* operator+(const IValue& value)const {
00113 TemplateValue<Typ,toString>* newValue;
00114
00115
00116 const TemplateValue<Typ,toString>* castValue = dynamic_cast<const TemplateValue<Typ,toString>* >(&value);
00117 if(castValue==0)
00118 return 0;
00119
00120
00121 const Typ typeValue = castValue->getValue();
00122 newValue = new TemplateValue<Typ,toString>(m_value+typeValue);
00123
00124
00125 return newValue;
00126 }
00127
00128
00129
00130
00131
00132
00133 virtual operator std::string(void)const {
00134 return toString(m_value);
00135 }
00136
00137
00138
00139
00140
00141
00142 virtual bool store(FILE* f) const {
00143 RESTORE_GA_TEMPLATE<Typ> temp;
00144 RESTORE_GA_TEMPLATE<int> integer;
00145
00146
00147 if(f==NULL) {
00148 printf("\n\n\t>>> [ERROR] <<<\nNo File to store GA [temp value].\n\t>>> [END] <<<\n\n\n");
00149 return false;
00150 }
00151
00152 temp.value = m_value;
00153
00154 integer.value=(int)m_name.length();
00155 for(unsigned int d=0;d<sizeof(RESTORE_GA_TEMPLATE<int>);d++) {
00156 fprintf(f,"%c",integer.buffer[d]);
00157 }
00158 fprintf(f,"%s",m_name.c_str());
00159
00160 for(unsigned int x=0;x<sizeof(RESTORE_GA_TEMPLATE<Typ>);x++) {
00161 fprintf(f,"%c",temp.buffer[x]);
00162 }
00163
00164 return true;
00165 }
00166
00167
00168
00169
00170
00171
00172 virtual bool restore(FILE* f) {
00173 RESTORE_GA_TEMPLATE<Typ> temp;
00174 RESTORE_GA_TEMPLATE<int> integer;
00175 char* buffer;
00176 int toread;
00177
00178
00179 if(f==NULL) {
00180 printf("\n\n\t>>> [ERROR] <<<\nNo File to restore GA [temp value].\n\t>>> [END] <<<\n\n\n");
00181 return false;
00182 }
00183
00184 for(toread=0;toread<(int)sizeof(RESTORE_GA_TEMPLATE<int>);toread++){
00185 fscanf(f,"%c",&integer.buffer[toread]);
00186 }
00187 toread=integer.value;
00188 buffer=new char[toread];
00189 for(int y=0;y<toread;y++){
00190 fscanf(f,"%c",&buffer[y]);
00191 }
00192 buffer[toread]='\0';
00193 m_name=buffer;
00194 delete[] buffer;
00195
00196 for(unsigned int x=0;x<sizeof(RESTORE_GA_TEMPLATE<Typ>);x++) {
00197 fscanf(f,"%c",&temp.buffer[x]);
00198 }
00199
00200 m_value = temp.value;
00201
00202 return true;
00203 }
00204
00205 protected:
00206
00207
00208
00209 Typ m_value;
00210
00211 private:
00212
00213
00214
00215
00216 TemplateValue() : IValue() {}
00217 };
00218
00219 #endif