00001
00019 #ifndef RTC_CONFIGADMIN_H
00020 #define RTC_CONFIGADMIN_H
00021
00022 #include <coil/Properties.h>
00023 #include <coil/stringutil.h>
00024 #include <string>
00025 #include <vector>
00026 #include <iostream>
00027
00042 namespace RTC
00043 {
00055 class OnUpdateCallback
00056 {
00057 public:
00073 virtual ~OnUpdateCallback(void){};
00091 virtual void operator()(const char* config_set) = 0;
00092 };
00093
00105 class OnUpdateParamCallback
00106 {
00107 public:
00123 virtual ~OnUpdateParamCallback(void){};
00141 virtual void operator()(const char* config_set, const char* config_param) = 0;
00142 };
00143
00155 class OnSetConfigurationSetCallback
00156 {
00157 public:
00173 virtual ~OnSetConfigurationSetCallback(void){};
00191 virtual void operator()(const coil::Properties& config_set) = 0;
00192 };
00193
00205 class OnAddConfigurationAddCallback
00206 {
00207 public:
00223 virtual ~OnAddConfigurationAddCallback(void){};
00241 virtual void operator()(const coil::Properties& config_set) = 0;
00242 };
00243
00255 class OnRemoveConfigurationSetCallback
00256 {
00257 public:
00273 virtual ~OnRemoveConfigurationSetCallback(void){};
00291 virtual void operator()(const char* config_set) = 0;
00292 };
00293
00305 class OnActivateSetCallback
00306 {
00307 public:
00323 virtual ~OnActivateSetCallback(void){};
00341 virtual void operator()(const char* config_id) = 0;
00342 };
00343
00344
00345
00346
00377 struct ConfigBase
00378 {
00400 ConfigBase(const char* name_, const char* def_val)
00401 : name(name_), default_value(def_val) {}
00402
00418 virtual ~ConfigBase(void){};
00419
00445 virtual bool update(const char* val) = 0;
00446
00454 const char* name;
00455
00463 const char* default_value;
00464 };
00465
00466
00467
00468
00501 template <typename VarType,
00502 typename TransFunc = bool (*)(VarType&, const char*)>
00503 class Config
00504 : public ConfigBase
00505 {
00506 public:
00532 Config(const char* name, VarType& var, const char* def_val,
00533 TransFunc trans = coil::stringTo)
00534 : ConfigBase(name, def_val), m_var(var), m_trans(trans)
00535 {
00536 }
00537
00553 virtual ~Config(void){}
00554
00578 virtual bool update(const char* val)
00579 {
00580 if ((*m_trans)(m_var, val)) { return true; }
00581 (*m_trans)(m_var, default_value);
00582 return false;
00583 }
00584
00585 protected:
00593 VarType& m_var;
00594
00603 TransFunc m_trans;
00604 };
00605
00606
00607
00608
00754 class ConfigAdmin
00755 {
00756 public:
00776 ConfigAdmin(coil::Properties& prop);
00777
00793 ~ConfigAdmin(void);
00794
00831 template <typename VarType>
00832 bool bindParameter(const char* param_name, VarType& var,
00833 const char* def_val,
00834 bool (*trans)(VarType&, const char*) = coil::stringTo)
00835 {
00836 if (param_name == 0) { return false; }
00837 if (def_val == 0) { return false; }
00838 if (isExist(param_name)) { return false; }
00839 if (!trans(var, def_val)) { return false; }
00840 m_params.push_back(new Config<VarType>(param_name, var, def_val, trans));
00841 return true;
00842 }
00843
00870 void update(void);
00871
00906 void update(const char* config_set);
00907
00945 void update(const char* config_set, const char* config_param);
00946
00973 bool isExist(const char* name);
00974
00995 bool isChanged(void) {return m_changed;}
00996
01016 const char* getActiveId(void) {return m_activeId.c_str();}
01017
01042 bool haveConfig(const char* config_id)
01043 {
01044 return (m_configsets.hasKey(config_id) == NULL) ? false : true;
01045 }
01046
01067 bool isActive(void)
01068 {
01069 return m_active;
01070 }
01071
01072
01073
01074
01094 const std::vector<coil::Properties*>& getConfigurationSets(void);
01095
01123 const coil::Properties& getConfigurationSet(const char* config_id);
01124
01154 bool setConfigurationSetValues(const coil::Properties& configuration_set);
01155
01179 const coil::Properties& getActiveConfigurationSet(void);
01180
01204 bool addConfigurationSet(const coil::Properties& configuration_set);
01205
01260 bool removeConfigurationSet(const char* config_id);
01261
01289 bool activateConfigurationSet(const char* config_id);
01290
01308 void setOnUpdate(OnUpdateCallback* cb);
01309
01327 void setOnUpdateParam(OnUpdateParamCallback* cb);
01328
01346 void setOnSetConfigurationSet(OnSetConfigurationSetCallback* cb);
01347
01365 void setOnAddConfigurationSet(OnAddConfigurationAddCallback* cb);
01366
01384 void setOnRemoveConfigurationSet(OnRemoveConfigurationSetCallback* cb);
01385
01403 void setOnActivateSet(OnActivateSetCallback* cb);
01404
01405 protected:
01425 void onUpdate(const char* config_set);
01426
01448 void onUpdateParam(const char* config_set, const char* config_param);
01449
01469 void onSetConfigurationSet(const coil::Properties& config_set);
01470
01490 void onAddConfigurationSet(const coil::Properties& config_set);
01491
01511 void onRemoveConfigurationSet(const char* config_id);
01512
01532 void onActivateSet(const char* config_id);
01533
01534 private:
01535 ConfigAdmin(const ConfigAdmin& ca);
01536 ConfigAdmin& operator=(const ConfigAdmin& ca);
01537
01538 struct find_conf
01539 {
01540 std::string m_name;
01541 find_conf(const char* name) : m_name(name) {};
01542 bool operator()(ConfigBase* conf)
01543 {
01544 if (conf == 0) { return false; }
01545 return (m_name == conf->name);
01546 }
01547 };
01548
01549 coil::Properties& m_configsets;
01550 coil::Properties m_emptyconf;
01551 std::vector<ConfigBase*> m_params;
01552 std::string m_activeId;
01553 bool m_active;
01554 bool m_changed;
01555 std::vector<std::string> m_newConfig;
01556
01557 OnUpdateCallback* m_updateCb;
01558 OnUpdateParamCallback* m_updateParamCb;
01559 OnSetConfigurationSetCallback* m_setConfigSetCb;
01560 OnAddConfigurationAddCallback* m_addConfigSetCb;
01561 OnRemoveConfigurationSetCallback* m_removeConfigSetCb;
01562 OnActivateSetCallback* m_activateSetCb;
01563
01564
01565 };
01566 };
01567 #endif // RTC_CONFIGADMIN_H