TemplateValue.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2009 by Robot Group Leipzig                        *
00003  *    martius@informatik.uni-leipzig.de                                    *
00004  *    fhesse@informatik.uni-leipzig.de                                     *
00005  *    der@informatik.uni-leipzig.de                                        *
00006  *    guettler@informatik.uni-leipzig.de                                   *
00007  *    jhoffmann@informatik.uni-leipzig.de                                  *
00008  *    joergweide84@aol.com (robot12)                                       *
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  *   This class is a implementation for the IValue interface. It is for    *
00027  *   standard data types concepted as a template class.                    *
00028  *                                                                         *
00029  *   $Log: TemplateValue.h,v $
00030  *   Revision 1.10  2009/11/05 14:09:01  robot12
00031  *   bugfix inside restore and store
00032  *
00033  *   Revision 1.9  2009/10/23 10:48:02  robot12
00034  *   bugfix in store and restore
00035  *
00036  *   Revision 1.8  2009/10/21 14:08:19  robot12
00037  *   add restore and store functions to the ga package
00038  *
00039  *   Revision 1.7  2009/07/21 08:37:59  robot12
00040  *   add some comments
00041  *
00042  *   Revision 1.6  2009/06/26 13:08:25  robot12
00043  *   finishing Values and add some comments
00044  *
00045  *   Revision 1.5  2009/05/14 15:29:54  robot12
00046  *   bugfix: mutation change the oldGen, not the new!!! now fixed
00047  *
00048  *   Revision 1.4  2009/05/12 13:29:24  robot12
00049  *   some new function
00050  *   -> toString methodes
00051  *
00052  *   Revision 1.3  2009/05/11 14:08:51  robot12
00053  *   patch some bugfix....
00054  *
00055  *   Revision 1.2  2009/05/06 13:28:22  robot12
00056  *   some implements... Finish
00057  *
00058  *   Revision 1.1  2009/05/04 15:27:57  robot12
00059  *   rename of some files and moving files to other positions
00060  *    - SingletonGenAlgAPI has one error!!! --> is not ready now
00061  *
00062  *   Revision 1.2  2009/05/04 09:20:52  robot12
00063  *   some implements.. Finish --> first compile
00064  *
00065  *   Revision 1.1  2009/04/29 14:32:29  robot12
00066  *   some implements... Part4
00067  *
00068  *
00069  *
00070  ***************************************************************************/
00071 
00072 #ifndef TEMPLATEVALUE_H_
00073 #define TEMPLATEVALUE_H_
00074 
00075 //includes
00076 #include <string>
00077 
00078 //ga_tools includes
00079 #include "IValue.h"
00080 #include "restore.h"
00081 
00082 /**
00083  * general function to converrt a double value to a string
00084  * @param value (double) the value wath should convert
00085  * @return
00086  */
00087 inline std::string doubleToString(double value) {
00088   char buffer[128];
00089   sprintf(buffer,"% .12lf",value);
00090   return buffer;
00091 }
00092 
00093 /**
00094  * template class for a IValue standard data type
00095  * needs the data type and a methode for string converting as template argument
00096  *
00097  * All parts are declared and defined in the header because the needed template implementations
00098  * are needed to the compiling time of the lib. To create new classes in an using program it
00099  * must be in the header.
00100  */
00101 template<class Typ, std::string toString(Typ)=doubleToString>
00102 class TemplateValue : public IValue {
00103 public:
00104   /**
00105    * constructor
00106    * needs the value and a name (for IValue -> is default implemented as "templateValue")
00107    * @param value (Typ) the value of this IValue
00108    * @param name (string) the name
00109    */
00110   TemplateValue(Typ value, std::string name = "templateValue") : IValue(name), m_value(value)  {}
00111 
00112   /**
00113    * default destructor
00114    */
00115   virtual ~TemplateValue()  {}
00116 
00117   /**
00118    * this function can be used to read the standard data type.
00119    * @return (Typ) the value
00120    */
00121   inline Typ getValue(void)const {return m_value;}
00122 
00123   /**
00124    * this function is to change the value.
00125    * @param value
00126    */
00127   inline void setValue(Typ value) {m_value=value;}
00128 
00129   /**
00130    * the implementation of the mul operator, what is part of the interface.
00131    * This function only accept TemplateValues or the same type like "Typ"
00132    * @param value (const IValue&) the other part of the operation
00133    * @return (IValue*) the result
00134    */
00135   virtual IValue* operator*(const IValue& value)const {
00136     TemplateValue<Typ,toString>* newValue;
00137 
00138     //cast the IValue to TemplateValue of the same type like "Typ"
00139     const TemplateValue<Typ,toString>* castValue = dynamic_cast<const TemplateValue<Typ,toString>* >(&value);
00140     if(castValue==0)
00141       return 0;
00142 
00143     //multiplicate the values
00144     const Typ typeValue = castValue->getValue();
00145     newValue = new TemplateValue<Typ,toString>(m_value*typeValue);
00146 
00147     //return result
00148     return newValue;
00149   }
00150 
00151   /**
00152    * the implementation of the add operator what is part of the interface.
00153    * This function only accept TemplateValues or the same type like "Typ"
00154    * @param value (const IValue&) the other part of the operation
00155    * @return (IValue*) the result
00156    */
00157   virtual IValue* operator+(const IValue& value)const {
00158     TemplateValue<Typ,toString>* newValue;
00159 
00160     //cast the IValue to TemplateValue of the same type like "Typ"
00161     const TemplateValue<Typ,toString>* castValue = dynamic_cast<const TemplateValue<Typ,toString>* >(&value);
00162     if(castValue==0)
00163       return 0;
00164 
00165     //add the values
00166     const Typ typeValue = castValue->getValue();
00167     newValue = new TemplateValue<Typ,toString>(m_value+typeValue);
00168 
00169     //return the result
00170     return newValue;
00171   }
00172 
00173   /**
00174    * cast operatot to string
00175    * use the convert methode.
00176    * @return (string) the cast result
00177    */
00178   virtual operator std::string(void)const {
00179     return toString(m_value);
00180   }
00181 
00182   /**
00183    * store the value in a file
00184    * @param f (FILE*) the file to store
00185    * @return (bool) true if all ok.
00186    */
00187   virtual bool store(FILE* f) const {
00188     RESTORE_GA_TEMPLATE<Typ> temp;
00189     RESTORE_GA_TEMPLATE<int> integer;
00190 
00191     //test
00192     if(f==NULL) {
00193       printf("\n\n\t>>> [ERROR] <<<\nNo File to store GA [temp value].\n\t>>> [END] <<<\n\n\n");
00194       return false;
00195     }
00196 
00197     temp.value = m_value;
00198 
00199     integer.value=(int)m_name.length();
00200     for(unsigned int d=0;d<sizeof(RESTORE_GA_TEMPLATE<int>);d++) {
00201       fprintf(f,"%c",integer.buffer[d]);
00202     }
00203     fprintf(f,"%s",m_name.c_str());
00204 
00205     for(unsigned int x=0;x<sizeof(RESTORE_GA_TEMPLATE<Typ>);x++) {
00206       fprintf(f,"%c",temp.buffer[x]);
00207     }
00208 
00209     return true;
00210   }
00211 
00212   /**
00213    * restore the value from a file
00214    * @param f (FILE*) the file where the value inside
00215    * @return (bool) true if all ok.
00216    */
00217   virtual bool restore(FILE* f) {
00218     RESTORE_GA_TEMPLATE<Typ> temp;
00219     RESTORE_GA_TEMPLATE<int> integer;
00220     char* buffer;
00221     int toread;
00222 
00223     //test
00224     if(f==NULL) {
00225       printf("\n\n\t>>> [ERROR] <<<\nNo File to restore GA [temp value].\n\t>>> [END] <<<\n\n\n");
00226       return false;
00227     }
00228 
00229     for(toread=0;toread<(int)sizeof(RESTORE_GA_TEMPLATE<int>);toread++){
00230       fscanf(f,"%c",&integer.buffer[toread]);
00231     }
00232     toread=integer.value;
00233     buffer=new char[toread];
00234     for(int y=0;y<toread;y++){
00235       fscanf(f,"%c",&buffer[y]);
00236     }
00237     buffer[toread]='\0';
00238     m_name=buffer;
00239     delete[] buffer;
00240 
00241     for(unsigned int x=0;x<sizeof(RESTORE_GA_TEMPLATE<Typ>);x++) {
00242       fscanf(f,"%c",&temp.buffer[x]);
00243     }
00244 
00245     m_value = temp.value;
00246 
00247     return true;
00248   }
00249 
00250 protected:
00251   /**
00252    * the real value
00253    */
00254   Typ m_value;
00255 
00256 private:
00257   /**
00258    * disable the default constructor
00259    * @return
00260    */
00261   TemplateValue() : IValue() {}
00262 };
00263 
00264 #endif /* TEMPLATEVALUE_H_ */
Generated on Fri Nov 4 10:59:39 2011 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.6.3