00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __CONFIGURABLE_H
00025 #define __CONFIGURABLE_H
00026
00027 #include <iostream>
00028 #include <cstdlib>
00029
00030 #include <limits>
00031 #include <list>
00032 #include <utility>
00033 #include <string>
00034 #include <map>
00035 #include "stl_adds.h"
00036 #include "backcaller.h"
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 class Configurable : public BackCaller
00082 {
00083 public:
00084
00085 typedef std::string paramkey;
00086 typedef std::string paramdescr;
00087
00088 typedef double paramval;
00089 typedef std::list<std::pair<paramkey, paramval> > paramlist;
00090 typedef std::map<paramkey, paramval*> parammap;
00091
00092
00093 typedef bool parambool;
00094 typedef std::list<std::pair<paramkey, parambool> > paramboollist;
00095 typedef std::map<paramkey, parambool*> paramboolmap;
00096
00097
00098 typedef int paramint;
00099 typedef std::list<std::pair<paramkey, paramint> > paramintlist;
00100 typedef std::map<paramkey, paramint*> paramintmap;
00101
00102 typedef std::map<paramkey, paramdescr> paramdescrmap;
00103
00104
00105 typedef std::pair<paramval, paramval> paramvalBounds;
00106 typedef std::map<paramkey, paramvalBounds> paramvalBoundsMap;
00107 #define valDefMaxBound 10.0
00108 #define valDefMinBound -10.0
00109
00110 typedef std::pair<paramint, paramint> paramintBounds;
00111 typedef std::map<paramkey, paramintBounds> paramintBoundsMap;
00112 #define intDefMinBound -10
00113 #define intDefMaxBound 10
00114
00115 typedef std::pair<paramkey, paramval*> paramvalpair;
00116 typedef std::pair<paramkey, parambool*> paramboolpair;
00117 typedef std::pair<paramkey, paramint*> paramintpair;
00118
00119 typedef std::vector<Configurable*> configurableList;
00120
00121
00122 struct matchId : public std::unary_function<Configurable*, bool>
00123 {
00124 matchId(int id) :
00125 id(id)
00126 {
00127 }
00128 int id;
00129 bool operator()(Configurable* c)
00130 {
00131 return c->id == id;
00132 }
00133 };
00134
00135 Configurable()
00136 {
00137 id = rand();
00138 }
00139
00140 Configurable(const std::string& name, const std::string& revision) :
00141 name(name), revision(revision)
00142 {
00143 id = rand();
00144 }
00145
00146
00147
00148
00149
00150
00151
00152
00153 virtual ~Configurable()
00154 {
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164 virtual void notifyOnChange(const paramkey& key){}
00165
00166
00167
00168
00169
00170
00171
00172 virtual void addParameter(const paramkey& key, paramval* val,
00173 paramval minBound, paramval maxBound,
00174 const paramdescr& descr = paramdescr() ) {
00175 mapOfValues[key] = val;
00176 if (minBound>*val) minBound = (*val)>0 ? 0 : (*val)*2;
00177 if (maxBound<*val) maxBound = (*val)>0 ? (*val)*2 : 0;
00178 if(!descr.empty()) mapOfDescr[key] = descr;
00179 mapOfValBounds[key]=paramvalBounds(minBound,maxBound);
00180 }
00181
00182
00183 virtual void addParameter(const paramkey& key, paramval* val,
00184 const paramdescr& descr = paramdescr()){
00185 addParameter(key,val,valDefMinBound, valDefMaxBound, descr);
00186 }
00187
00188
00189
00190
00191
00192 virtual void addParameter(const paramkey& key, parambool* val,
00193 const paramdescr& descr = paramdescr()) {
00194 mapOfBoolean[key] = val;
00195 if(!descr.empty()) mapOfDescr[key] = descr;
00196 }
00197
00198
00199
00200
00201 virtual void addParameter(const paramkey& key, paramint* val,
00202 paramint minBound, paramint maxBound,
00203 const paramdescr& descr = paramdescr()) {
00204 mapOfInteger[key] = val;
00205 if (minBound>*val) minBound = (*val)>0 ? 0 : (*val)*2;
00206 if (maxBound<*val) maxBound = (*val)>0 ? (*val)*2 : 0;
00207 if(!descr.empty()) mapOfDescr[key] = descr;
00208 mapOfIntBounds[key]=paramintBounds(minBound,maxBound);
00209 }
00210
00211 virtual void addParameter(const paramkey& key, paramint* val,
00212 const paramdescr& descr = paramdescr()){
00213 addParameter(key,val,intDefMinBound, intDefMaxBound, descr);
00214 }
00215
00216
00217
00218
00219
00220 virtual void addParameterDef(const paramkey& key, paramval* val, paramval def,
00221 paramval minBound, paramval maxBound,
00222 const paramdescr& descr = paramdescr()){
00223 *val = def;
00224 addParameter(key,val, minBound, maxBound, descr);
00225 }
00226
00227 virtual void addParameterDef(const paramkey& key, paramval* val, paramval def,
00228 const paramdescr& descr = paramdescr()){
00229 addParameterDef(key,val,def,valDefMinBound, valDefMaxBound, descr);
00230 }
00231
00232
00233
00234 virtual void addParameterDef(const paramkey& key, parambool* val, parambool def,
00235 const paramdescr& descr = paramdescr()) {
00236 *val = def;
00237 addParameter(key,val,descr);
00238 }
00239
00240
00241 virtual void addParameterDef(const paramkey& key, paramint* val, paramint def,
00242 paramint minBound, paramint maxBound,
00243 const paramdescr& descr = paramdescr()) {
00244 *val = def;
00245 addParameter(key,val, minBound, maxBound, descr);
00246 }
00247
00248 virtual void addParameterDef(const paramkey& key, paramint* val, paramint def,
00249 const paramdescr& descr = paramdescr()) {
00250 addParameterDef(key,val,def,intDefMinBound, intDefMaxBound, descr);
00251 }
00252
00253
00254 virtual void setParamDescr(const paramkey& key, const paramdescr& descr,
00255 bool traverseChildren = true);
00256
00257
00258
00259 int getId() const {
00260 return id;
00261 }
00262
00263
00264 virtual paramkey getName() const {
00265 return name;
00266 }
00267
00268
00269 virtual paramkey getRevision() const {
00270 return revision;
00271 }
00272
00273
00274
00275
00276
00277
00278
00279 virtual void setName(const paramkey& name, bool callSetNameOfInspectable = true);
00280
00281
00282 virtual void setRevision(const paramkey& revision) {
00283 this->revision = revision;
00284 }
00285
00286
00287
00288
00289 virtual paramval getParam(const paramkey& key, bool traverseChildren = true) const;
00290
00291
00292
00293
00294
00295
00296 virtual bool hasParam(const paramkey& key, bool traverseChildren = true) const;
00297
00298
00299
00300
00301 virtual bool setParam(const paramkey& key, paramval val, bool traverseChildren = true);
00302
00303
00304
00305
00306
00307
00308 virtual void setParamBounds(const paramkey& key, paramval minBound, paramval maxBound, bool traverseChildren = true);
00309
00310 virtual void setParamBounds(const paramkey& key, paramint minBound, paramint maxBound, bool traverseChildren = true);
00311
00312 virtual void setParamBounds(const paramkey& key, paramvalBounds bounds, bool traverseChildren = true);
00313
00314 virtual void setParamBounds(const paramkey& key, paramintBounds bounds, bool traverseChildren = true);
00315
00316
00317
00318
00319
00320
00321 virtual paramlist getParamList() const {
00322 return paramlist();
00323 }
00324
00325
00326 virtual std::list<paramkey> getAllParamNames(bool traverseChildren = true);
00327
00328 virtual parammap getParamValMap() const {
00329 return mapOfValues;
00330 }
00331
00332 virtual paramintmap getParamIntMap() const {
00333 return mapOfInteger;
00334 }
00335
00336 virtual paramboolmap getParamBoolMap() const {
00337 return mapOfBoolean;
00338 }
00339
00340
00341 virtual paramdescr getParamDescr(const paramkey& key, bool traverseChildren = true) const;
00342
00343 virtual paramvalBounds getParamvalBounds(const paramkey& key, bool traverseChildren = true) const;
00344
00345 virtual paramintBounds getParamintBounds(const paramkey& key, bool traverseChildren = true) const;
00346
00347 virtual bool hasParamDescr(const paramkey& key, bool traverseChildren = true) const;
00348
00349 virtual bool hasParamvalBounds(const paramkey& key, bool traverseChildren = true) const;
00350
00351 virtual bool hasParamintBounds(const paramkey& key, bool traverseChildren = true) const;
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 static void insertCVSInfo(paramkey& str, const char* file, const char* revision);
00365
00366
00367
00368
00369 virtual bool storeCfg(const char* filenamestem, const std::list<std::string>& comments = std::list<std::string>());
00370
00371 virtual bool restoreCfg(const char* filenamestem);
00372
00373 void print(FILE* f, const char* prefix, int columns=90, bool traverseChildren = true) const;
00374
00375
00376 bool parse(FILE* f, const char* prefix = 0, bool traverseChildren = true);
00377
00378
00379
00380
00381
00382 virtual void addConfigurable(Configurable* conf);
00383
00384
00385
00386
00387
00388 virtual void removeConfigurable(Configurable* conf);
00389
00390
00391
00392
00393 virtual const configurableList& getConfigurables() const;
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403 virtual void configurableChanged();
00404
00405 static const CallbackableType CALLBACK_CONFIGURABLE_CHANGED = 11;
00406
00407 protected:
00408
00409 void copyParameters(const Configurable&, bool traverseChildren = true);
00410
00411
00412 void printdescr(FILE* f, const char* prefix, const paramkey& key,
00413 int columns, int indent) const;
00414
00415
00416 private:
00417 int id;
00418 paramkey name;
00419 paramkey revision;
00420
00421 parammap mapOfValues;
00422 paramboolmap mapOfBoolean;
00423 paramintmap mapOfInteger;
00424 paramdescrmap mapOfDescr;
00425
00426 paramvalBoundsMap mapOfValBounds;
00427 paramintBoundsMap mapOfIntBounds;
00428
00429 void initParamBounds(const paramkey& key);
00430
00431 configurableList ListOfConfigurableChildren;
00432 Configurable* parent;
00433 };
00434
00435 #endif // __CONFIGURABLE_H