Robot Simulator of the Robotics Group for Self-Organization of Control: TemplateValue.h Source File

# TemplateValue.h

Go to the documentation of this file.

    00001 /***************************************************************************
    00002  *   Copyright (C) 2008-2011 LpzRobots development team                    *
    00003  *    Joerg Weider   <joergweide84 at aol dot com> (robot12)               *
    00004  *    Georg Martius  <georg dot martius at web dot de>                     *
    00005  *    Frank Guettler <guettler at informatik dot uni-leipzig dot de        *
    00006  *    Frank Hesse    <frank at nld dot ds dot mpg dot de>                  *
    00007  *    Ralf Der       <ralfder at mis dot mpg dot de>                       *
    00008  *    Joern Hoffmann <jhoffmann at informatik dot uni-leipzig dot de       *
    00009  *                                                                         *
    00010  *   This program is free software; you can redistribute it and/or modify  *
    00011  *   it under the terms of the GNU General Public License as published by  *
    00012  *   the Free Software Foundation; either version 2 of the License, or     *
    00013  *   (at your option) any later version.                                   *
    00014  *                                                                         *
    00015  *   This program is distributed in the hope that it will be useful,       *
    00016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
    00017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
    00018  *   GNU General Public License for more details.                          *
    00019  *                                                                         *
    00020  *   You should have received a copy of the GNU General Public License     *
    00021  *   along with this program; if not, write to the                         *
    00022  *   Free Software Foundation, Inc.,                                       *
    00023  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
    00024  *                                                                         *
    00025  ***************************************************************************/
    00026
    00027 #ifndef TEMPLATEVALUE_H_
    00028 #define TEMPLATEVALUE_H_
    00029
    00030 //includes
    00031 #include <string>
    00032
    00033 //ga_tools includes
    00034 #include "IValue.h"
    00035 #include "restore.h"
    00036
    00037 /**
    00038  * general function to converrt a double value to a string
    00039  * @param value (double) the value wath should convert
    00040  * @return
    00041  */
    00042 inline std::string doubleToString(double value) {
    00043   char buffer[128];
    00044   sprintf(buffer,"% .12lf",value);
    00045   return buffer;
    00046 }
    00047
    00048 /**
    00049  * template class for a IValue standard data type
    00050  * needs the data type and a methode for string converting as template argument
    00051  *
    00052  * All parts are declared and defined in the header because the needed template implementations
    00053  * are needed to the compiling time of the lib. To create new classes in an using program it
    00054  * must be in the header.
    00055  */
    00056 template<class Typ, std::string toString(Typ)=doubleToString>
    00057 class TemplateValue : public IValue {
    00058 public:
    00059   /**
    00060    * constructor
    00061    * needs the value and a name (for IValue -> is default implemented as "templateValue")
    00062    * @param value (Typ) the value of this IValue
    00063    * @param name (string) the name
    00064    */
    00065   TemplateValue(Typ value, std::string name = "templateValue") : IValue(name), m_value(value)  {}
    00066
    00067   /**
    00068    * default destructor
    00069    */
    00070   virtual ~TemplateValue()  {}
    00071
    00072   /**
    00073    * this function can be used to read the standard data type.
    00074    * @return (Typ) the value
    00075    */
    00076   inline Typ getValue(void)const {return m_value;}
    00077
    00078   /**
    00079    * this function is to change the value.
    00080    * @param value
    00081    */
    00082   inline void setValue(Typ value) {m_value=value;}
    00083
    00084   /**
    00085    * the implementation of the mul operator, what is part of the interface.
    00086    * This function only accept TemplateValues or the same type like "Typ"
    00087    * @param value (const IValue&) the other part of the operation
    00088    * @return (IValue*) the result
    00089    */
    00090   virtual IValue* operator*(const IValue& value)const {
    00091     TemplateValue<Typ,toString>* newValue;
    00092
    00093     //cast the IValue to TemplateValue of the same type like "Typ"
    00094     const TemplateValue<Typ,toString>* castValue = dynamic_cast<const TemplateValue<Typ,toString>* >(&value);
    00095     if(castValue==0)
    00096       return 0;
    00097
    00098     //multiplicate the values
    00099     const Typ typeValue = castValue->getValue();
    00100     newValue = new TemplateValue<Typ,toString>(m_value*typeValue);
    00101
    00102     //return result
    00103     return newValue;
    00104   }
    00105
    00106   /**
    00107    * the implementation of the add operator what is part of the interface.
    00108    * This function only accept TemplateValues or the same type like "Typ"
    00109    * @param value (const IValue&) the other part of the operation
    00110    * @return (IValue*) the result
    00111    */
    00112   virtual IValue* operator+(const IValue& value)const {
    00113     TemplateValue<Typ,toString>* newValue;
    00114
    00115     //cast the IValue to TemplateValue of the same type like "Typ"
    00116     const TemplateValue<Typ,toString>* castValue = dynamic_cast<const TemplateValue<Typ,toString>* >(&value);
    00117     if(castValue==0)
    00118       return 0;
    00119
    00120     //add the values
    00121     const Typ typeValue = castValue->getValue();
    00122     newValue = new TemplateValue<Typ,toString>(m_value+typeValue);
    00123
    00124     //return the result
    00125     return newValue;
    00126   }
    00127
    00128   /**
    00129    * cast operatot to string
    00130    * use the convert methode.
    00131    * @return (string) the cast result
    00132    */
    00133   virtual operator std::string(void)const {
    00134     return toString(m_value);
    00135   }
    00136
    00137   /**
    00138    * store the value in a file
    00139    * @param f (FILE*) the file to store
    00140    * @return (bool) true if all ok.
    00141    */
    00142   virtual bool store(FILE* f) const {
    00143     RESTORE_GA_TEMPLATE<Typ> temp;
    00144     RESTORE_GA_TEMPLATE<int> integer;
    00145
    00146     //test
    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    * restore the value from a file
    00169    * @param f (FILE*) the file where the value inside
    00170    * @return (bool) true if all ok.
    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     //test
    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    * the real value
    00208    */
    00209   Typ m_value;
    00210
    00211 private:
    00212   /**
    00213    * disable the default constructor
    00214    * @return
    00215    */
    00216   TemplateValue() : IValue() {}
    00217 };
    00218
    00219 #endif /* TEMPLATEVALUE_H_ */