configurable.h

Go to the documentation of this file.
00001 #ifndef __CONFIGURABLE_H
00002 #define __CONFIGURABLE_H
00003 
00004 #include <iostream>
00005 #include <list>
00006 #include <utility>
00007 #include <string>
00008 #include "stl_adds.h"
00009 
00010 
00011 #ifdef AVR
00012 // for some reason gcc-avr has problems with pure functions.
00013 // Therefore we use the hack to define an empty function instead of a pure one.
00014 // However we get a lot of warnings since we return nothing 
00015 #define ABSTRACT {}
00016 #else
00017 #define ABSTRACT =0
00018 #endif
00019 
00020 /**
00021    Abstact class for configurable objects. Sort of Hashmap interface. Parameters are double values
00022 
00023 The Configurator is a (planned) external tool that can be used for changing the values of configurable objects.
00024 
00025 * Protocoll for Configurator:
00026 \code
00027 To Configurator (BNF notation):
00028 Conf         := <Comp>*
00029 Comp         := <CompName> <Pair>*
00030 CompName     := [<string>][<int>]<newline>
00031 Pair         := <alphanum>=<double><newline>
00032 \endcode
00033 the remaining tags are self explanatory
00034 
00035 Example
00036 \code
00037 [Component name which can contain spaces and digits and .,- ][ID1]
00038 key1 = value1
00039 key2 = value2
00040 .
00041 .
00042 [Other Component name which can contain spaces and digits and .,- ][ID2]
00043 key3 = value3
00044 \endcode
00045 
00046 Answer: (from Communicator to Simulation environment)\n
00047 1. On change of a parameter:
00048 \code
00049 [ID] key=newvalue
00050 \endcode
00051 or
00052 \code
00053 key=newvalue
00054 \endcode
00055 the latter one means that the parameter is changed in all components
00056 
00057 2. Request of the description as defined above.
00058 \code
00059 #Something I don\'t care
00060 \endcode
00061 */
00062 
00063 class Configurable {
00064 public:
00065   
00066   typedef std::string paramkey;
00067   typedef double paramval;
00068   typedef std::list< std::pair<paramkey, paramval> > paramlist;
00069 
00070   /// nice predicate function for finding by ID
00071   struct matchId : public std::unary_function<Configurable*, bool> {
00072     matchId(int id) : id(id) {}
00073     int id;
00074     bool operator()(Configurable* c) { return c->id == id; }
00075   };
00076 
00077   Configurable(){ id = rand();}
00078   /// intialise with name and revision (use "$ID$")
00079   Configurable(const std::string& name, const std::string& revision)
00080     : name(name), revision(revision) {
00081       id = rand();
00082     }
00083   virtual ~Configurable(){}
00084 
00085   /// return the id of the configurable objects, which is created by random on initialisation
00086   int getId() const { return id;}
00087 
00088 
00089   /// return the name of the object
00090   virtual paramkey getName() const {
00091     return name;
00092   } 
00093 
00094   /// returns the revision of the object
00095   virtual paramkey getRevision() const {
00096     return revision;
00097   }
00098 
00099   /// stes the name of the object
00100   virtual void setName(const paramkey& name){
00101     this->name = name;
00102   } 
00103   /// sets the revision Hint: {  return "$ID$"; }
00104   virtual void getRevision(const paramkey& revision) {
00105     this->revision = revision; 
00106   }
00107 
00108   /** returns the value of the requested parameter 
00109       or 0 (+ error message to stderr) if unknown.
00110   */
00111   virtual paramval getParam(const paramkey& key) const{
00112     std::cerr << __FUNCTION__ << ": parameter " << key << " unknown\n";
00113     return 0;
00114   }
00115 
00116   /** sets the value of the given parameter 
00117       or does nothing if unknown.
00118   */
00119   virtual bool setParam(const paramkey& key, paramval val){
00120     return false; // fprintf(stderr, "%s:parameter %s unknown\n", __FUNCTION__, key);
00121   }
00122   /** The list of all parameters with there value as allocated lists.
00123       @return list of key-value pairs 
00124   */
00125   virtual paramlist getParamList() const {
00126     return paramlist();
00127   }
00128 
00129   /** This is a utility function for inserting the filename and the revision number
00130       at the beginning of the given string buffer str and terminates it.
00131       @param str buffer (call by reference) 
00132         that should have space for file+revision+2 characters
00133       @param file filename given by CVS i.e. $RCSfile: configurable.h,v $
00134       @param revision revision number given by CVS i.e. $Revision: 1.12 $
00135   */
00136   static void insertCVSInfo(paramkey& str, const char* file, const char* revision);
00137 
00138 #ifndef AVR
00139   /** stores the key values paires into the file : filenamestem.cfg */
00140   bool storeCfg(const char* filenamestem);
00141   /** restores the key values paires from the file : filenamestem.cfg */
00142   bool restoreCfg(const char* filenamestem);
00143   void print(FILE* f, const char* prefix) const;
00144   void parse(FILE* f);
00145 #endif  
00146 private: 
00147   int id;
00148   paramkey name;
00149   paramkey revision;
00150 };
00151 
00152 
00153 #endif

Generated on Mon Aug 7 16:40:14 2006 for Robotsystem of the Robot Group Leipzig by  doxygen 1.4.7