Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TemplateValue.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2008-2011 LpzRobots development team *
3  * Joerg Weider <joergweide84 at aol dot com> (robot12) *
4  * Georg Martius <georg dot martius at web dot de> *
5  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
6  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
7  * Ralf Der <ralfder at mis dot mpg dot de> *
8  * Joern Hoffmann <jhoffmann at informatik dot uni-leipzig dot de *
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  * This program is distributed in the hope that it will be useful, *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18  * GNU General Public License for more details. *
19  * *
20  * You should have received a copy of the GNU General Public License *
21  * along with this program; if not, write to the *
22  * Free Software Foundation, Inc., *
23  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
24  * *
25  ***************************************************************************/
26 
27 #ifndef TEMPLATEVALUE_H_
28 #define TEMPLATEVALUE_H_
29 
30 //includes
31 #include <string>
32 
33 //ga_tools includes
34 #include "IValue.h"
35 #include "restore.h"
36 
37 /**
38  * general function to converrt a double value to a string
39  * @param value (double) the value wath should convert
40  * @return
41  */
42 inline std::string doubleToString(double value) {
43  char buffer[128];
44  sprintf(buffer,"% .12lf",value);
45  return buffer;
46 }
47 
48 /**
49  * template class for a IValue standard data type
50  * needs the data type and a methode for string converting as template argument
51  *
52  * All parts are declared and defined in the header because the needed template implementations
53  * are needed to the compiling time of the lib. To create new classes in an using program it
54  * must be in the header.
55  */
56 template<class Typ, std::string toString(Typ)=doubleToString>
57 class TemplateValue : public IValue {
58 public:
59  /**
60  * constructor
61  * needs the value and a name (for IValue -> is default implemented as "templateValue")
62  * @param value (Typ) the value of this IValue
63  * @param name (string) the name
64  */
65  TemplateValue(Typ value, std::string name = "templateValue") : IValue(name), m_value(value) {}
66 
67  /**
68  * default destructor
69  */
70  virtual ~TemplateValue() {}
71 
72  /**
73  * this function can be used to read the standard data type.
74  * @return (Typ) the value
75  */
76  inline Typ getValue(void)const {return m_value;}
77 
78  /**
79  * this function is to change the value.
80  * @param value
81  */
82  inline void setValue(Typ value) {m_value=value;}
83 
84  /**
85  * the implementation of the mul operator, what is part of the interface.
86  * This function only accept TemplateValues or the same type like "Typ"
87  * @param value (const IValue&) the other part of the operation
88  * @return (IValue*) the result
89  */
90  virtual IValue* operator*(const IValue& value)const {
92 
93  //cast the IValue to TemplateValue of the same type like "Typ"
94  const TemplateValue<Typ,toString>* castValue = dynamic_cast<const TemplateValue<Typ,toString>* >(&value);
95  if(castValue==0)
96  return 0;
97 
98  //multiplicate the values
99  const Typ typeValue = castValue->getValue();
100  newValue = new TemplateValue<Typ,toString>(m_value*typeValue);
101 
102  //return result
103  return newValue;
104  }
105 
106  /**
107  * the implementation of the add operator what is part of the interface.
108  * This function only accept TemplateValues or the same type like "Typ"
109  * @param value (const IValue&) the other part of the operation
110  * @return (IValue*) the result
111  */
112  virtual IValue* operator+(const IValue& value)const {
113  TemplateValue<Typ,toString>* newValue;
114 
115  //cast the IValue to TemplateValue of the same type like "Typ"
116  const TemplateValue<Typ,toString>* castValue = dynamic_cast<const TemplateValue<Typ,toString>* >(&value);
117  if(castValue==0)
118  return 0;
119 
120  //add the values
121  const Typ typeValue = castValue->getValue();
122  newValue = new TemplateValue<Typ,toString>(m_value+typeValue);
123 
124  //return the result
125  return newValue;
126  }
127 
128  /**
129  * cast operatot to string
130  * use the convert methode.
131  * @return (string) the cast result
132  */
133  virtual operator std::string(void)const {
134  return toString(m_value);
135  }
136 
137  /**
138  * store the value in a file
139  * @param f (FILE*) the file to store
140  * @return (bool) true if all ok.
141  */
142  virtual bool store(FILE* f) const {
144  RESTORE_GA_TEMPLATE<int> integer;
145 
146  //test
147  if(f==NULL) {
148  printf("\n\n\t>>> [ERROR] <<<\nNo File to store GA [temp value].\n\t>>> [END] <<<\n\n\n");
149  return false;
150  }
151 
152  temp.value = m_value;
153 
154  integer.value=(int)m_name.length();
155  for(unsigned int d=0;d<sizeof(RESTORE_GA_TEMPLATE<int>);d++) {
156  fprintf(f,"%c",integer.buffer[d]);
157  }
158  fprintf(f,"%s",m_name.c_str());
159 
160  for(unsigned int x=0;x<sizeof(RESTORE_GA_TEMPLATE<Typ>);x++) {
161  fprintf(f,"%c",temp.buffer[x]);
162  }
163 
164  return true;
165  }
166 
167  /**
168  * restore the value from a file
169  * @param f (FILE*) the file where the value inside
170  * @return (bool) true if all ok.
171  */
172  virtual bool restore(FILE* f) {
174  RESTORE_GA_TEMPLATE<int> integer;
175  char* buffer;
176  int toread;
177 
178  //test
179  if(f==NULL) {
180  printf("\n\n\t>>> [ERROR] <<<\nNo File to restore GA [temp value].\n\t>>> [END] <<<\n\n\n");
181  return false;
182  }
183 
184  for(toread=0;toread<(int)sizeof(RESTORE_GA_TEMPLATE<int>);toread++){
185  fscanf(f,"%c",&integer.buffer[toread]);
186  }
187  toread=integer.value;
188  buffer=new char[toread];
189  for(int y=0;y<toread;y++){
190  fscanf(f,"%c",&buffer[y]);
191  }
192  buffer[toread]='\0';
193  m_name=buffer;
194  delete[] buffer;
195 
196  for(unsigned int x=0;x<sizeof(RESTORE_GA_TEMPLATE<Typ>);x++) {
197  fscanf(f,"%c",&temp.buffer[x]);
198  }
199 
200  m_value = temp.value;
201 
202  return true;
203  }
204 
205 protected:
206  /**
207  * the real value
208  */
209  Typ m_value;
210 
211 private:
212  /**
213  * disable the default constructor
214  * @return
215  */
216  TemplateValue() : IValue() {}
217 };
218 
219 #endif /* TEMPLATEVALUE_H_ */
virtual bool restore(FILE *f)
restore the value from a file
Definition: TemplateValue.h:172
void setValue(Typ value)
this function is to change the value.
Definition: TemplateValue.h:82
Typ m_value
the real value
Definition: TemplateValue.h:209
virtual bool store(FILE *f) const
store the value in a file
Definition: TemplateValue.h:142
iparamkey name
Definition: inspectable.h:251
virtual IValue * operator*(const IValue &value) const
the implementation of the mul operator, what is part of the interface.
Definition: TemplateValue.h:90
template class for a IValue standard data type needs the data type and a methode for string convertin...
Definition: TemplateValue.h:57
TemplateValue(Typ value, std::string name="templateValue")
constructor needs the value and a name (for IValue -> is default implemented as "templateValue") ...
Definition: TemplateValue.h:65
std::string doubleToString(double value)
general function to converrt a double value to a string
Definition: TemplateValue.h:42
char buffer[]
Definition: restore.h:90
This class is a interface for a value which is part of a gen.
Definition: IValue.h:38
virtual IValue * operator+(const IValue &value) const
the implementation of the add operator what is part of the interface.
Definition: TemplateValue.h:112
Definition: restore.h:87
std::string m_name
the name of this class.
Definition: IValue.h:93
Typ value
Definition: restore.h:89
Typ getValue(void) const
this function can be used to read the standard data type.
Definition: TemplateValue.h:76
virtual ~TemplateValue()
default destructor
Definition: TemplateValue.h:70