Robotsystem from Robot Group Leipzig: configurable.h Source File

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Directories
- Examples

- File List
- File Members

# 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 an 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
    00034 the remaining tags are self explanatory
    00035
    00036 Example
    00037 \code
    00038 [Component name which can contain spaces and digits and .,- ][ID1]
    00039 key1 = value1
    00040 key2 = value2
    00041 .
    00042 .
    00043 [Other Component name which can contain spaces and digits and .,- ][ID2]
    00044 key3 = value3
    00045 \endcode
    00046
    00047 Answer: (from Communicator to Simulation environment)\n
    00048 1. On change of a parameter:
    00049 \code
    00050 [ID] key=newvalue
    00051 \endcode
    00052 or
    00053 \code
    00054 key=newvalue
    00055 \endcode
    00056 the latter one means that the parameter is changed in all components
    00057
    00058 2. Request of the description as defined above.
    00059 \code
    00060 #Something I don\'t care
    00061 \endcode
    00062 */
    00063
    00064 class Configurable {
    00065 public:
    00066
    00067   typedef std::string paramkey;
    00068   typedef double paramval;
    00069   typedef std::list< std::pair<paramkey, paramval> > paramlist;
    00070
    00071   /// nice predicate function for finding a Layer with its vectorname
    00072   struct matchId : public std::unary_function<Configurable*, bool> {
    00073     matchId(int id) : id(id) {}
    00074     int id;
    00075     bool operator()(Configurable* c) { return c->id == id; }
    00076   };
    00077
    00078   Configurable(){ id = rand();}
    00079   virtual ~Configurable(){}
    00080
    00081   /// return the id of the configurable objects, which is created by random on initialisation
    00082   int getId() const { return id;}
    00083
    00084
    00085   /// return the name of the object (with version number) Hint: {  return "$ID$"; }
    00086   virtual paramkey getName() const ABSTRACT;
    00087
    00088
    00089   /** returns the value of the requested parameter
    00090       or 0 (+ error message to stderr) if unknown.
    00091   */
    00092   virtual paramval getParam(const paramkey& key) const{
    00093     std::cerr << __FUNCTION__ << ": parameter " << key << " unknown\n";
    00094     return 0;
    00095   }
    00096
    00097   /** sets the value of the given parameter
    00098       or does nothing if unknown.
    00099   */
    00100   virtual bool setParam(const paramkey& key, paramval val){
    00101     return false; // fprintf(stderr, "%s:parameter %s unknown\n", __FUNCTION__, key);
    00102   }
    00103   /** The list of all parameters with there value as allocated lists.
    00104       @return list of key-value pairs
    00105   */
    00106   virtual paramlist getParamList() const ABSTRACT;
    00107
    00108   /** This is a utility function for inserting the filename and the revision number
    00109       at the beginning of the given string buffer str and terminates it.
    00110       @param str buffer (call by reference)
    00111         that should have space for file+revision+2 characters
    00112       @param file filename given by CVS i.e. $RCSfile: configurable.h,v $
    00113       @param revision revision number given by CVS i.e. $Revision: 1.9.6.5 $
    00114   */
    00115   static void insertCVSInfo(paramkey& str, const char* file, const char* revision);
    00116
    00117 #ifndef AVR
    00118   /** stores the key values paires into the file : filenamestem.cfg */
    00119   bool storeCfg(const char* filenamestem);
    00120   /** restores the key values paires from the file : filenamestem.cfg */
    00121   bool restoreCfg(const char* filenamestem);
    00122   void print(FILE* f, const char* prefix) const;
    00123   void parse(FILE* f);
    00124 #endif
    00125 private:
    00126   int id;
    00127 };
    00128
    00129
    00130 #endif