00001
00019 #ifndef ConfigAdmin_h
00020 #define 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 {
00044 class OnUpdateCallback
00045 {
00046 public:
00047 virtual ~OnUpdateCallback(void){};
00048 virtual void operator()(const char* config_set) = 0;
00049 };
00050
00051 class OnUpdateParamCallback
00052 {
00053 public:
00054 virtual ~OnUpdateParamCallback(void){};
00055 virtual void operator()(const char* config_set, const char* config_param) = 0;
00056 };
00057
00058 class OnSetConfigurationSetCallback
00059 {
00060 public:
00061 virtual ~OnSetConfigurationSetCallback(void){};
00062 virtual void operator()(const coil::Properties& config_set) = 0;
00063 };
00064
00065 class OnAddConfigurationAddCallback
00066 {
00067 public:
00068 virtual ~OnAddConfigurationAddCallback(void){};
00069 virtual void operator()(const coil::Properties& config_set) = 0;
00070 };
00071
00072 class OnRemoveConfigurationSetCallback
00073 {
00074 public:
00075 virtual ~OnRemoveConfigurationSetCallback(void){};
00076 virtual void operator()(const char* config_set) = 0;
00077 };
00078
00079 class OnActivateSetCallback
00080 {
00081 public:
00082 virtual ~OnActivateSetCallback(void){};
00083 virtual void operator()(const char* config_id) = 0;
00084 };
00085
00086
00087
00088
00119 struct ConfigBase
00120 {
00142 ConfigBase(const char* name_, const char* def_val)
00143 : name(name_), default_value(def_val) {}
00144
00160 virtual ~ConfigBase(void){};
00161
00187 virtual bool update(const char* val) = 0;
00188
00196 const char* name;
00197
00205 const char* default_value;
00206 };
00207
00208
00209
00210
00242 template <typename VarType,
00243 typename TransFunc = bool (*)(VarType&, const char*)>
00244 class Config
00245 : public ConfigBase
00246 {
00247 public:
00273 Config(const char* name, VarType& var, const char* def_val,
00274 TransFunc trans = coil::stringTo)
00275 : ConfigBase(name, def_val), m_var(var), m_trans(trans)
00276 {
00277 }
00278
00294 virtual ~Config(void){}
00295
00319 virtual bool update(const char* val)
00320 {
00321 if ((*m_trans)(m_var, val)) { return true; }
00322 (*m_trans)(m_var, default_value);
00323 return false;
00324 }
00325
00326 protected:
00334 VarType& m_var;
00335
00344 TransFunc m_trans;
00345 };
00346
00347
00348
00349
00369 class ConfigAdmin
00370 {
00371 public:
00391 ConfigAdmin(coil::Properties& prop);
00392
00408 ~ConfigAdmin(void);
00409
00446 template <typename VarType>
00447 bool bindParameter(const char* param_name, VarType& var,
00448 const char* def_val,
00449 bool (*trans)(VarType&, const char*) = coil::stringTo)
00450 {
00451 if (param_name == 0) { return false; }
00452 if (def_val == 0) { return false; }
00453 if (isExist(param_name)) { return false; }
00454 if (!trans(var, def_val)) { return false; }
00455 m_params.push_back(new Config<VarType>(param_name, var, def_val, trans));
00456 return true;
00457 }
00458
00484 void update(const char* config_set);
00485
00511 void update(const char* config_set, const char* config_param);
00512
00539 void update(void);
00540
00565 bool isExist(const char* name);
00566
00587 bool isChanged(void) {return m_changed;}
00588
00608 const char* getActiveId(void) {return m_activeId.c_str();}
00609
00634 bool haveConfig(const char* config_id)
00635 {
00636 return (m_configsets.hasKey(config_id) == NULL) ? false : true;
00637 }
00638
00659 bool isActive(void)
00660 {
00661 return m_active;
00662 }
00663
00664
00665
00666
00686 const std::vector<coil::Properties*>& getConfigurationSets(void);
00687
00715 const coil::Properties& getConfigurationSet(const char* config_id);
00716
00746 bool setConfigurationSetValues(const coil::Properties& configuration_set);
00747
00771 const coil::Properties& getActiveConfigurationSet(void);
00772
00796 bool addConfigurationSet(const coil::Properties& configuration_set);
00797
00825 bool removeConfigurationSet(const char* config_id);
00826
00854 bool activateConfigurationSet(const char* config_id);
00855
00856 void setOnUpdate(OnUpdateCallback* cb);
00857 void setOnUpdateParam(OnUpdateParamCallback* cb);
00858 void setOnSetConfigurationSet(OnSetConfigurationSetCallback* cb);
00859 void setOnAddConfigurationSet(OnAddConfigurationAddCallback* cb);
00860 void setOnRemoveConfigurationSet(OnRemoveConfigurationSetCallback* cb);
00861 void setOnActivateSet(OnActivateSetCallback* cb);
00862
00863 protected:
00864 void onUpdate(const char* config_set);
00865 void onUpdateParam(const char* config_set, const char* config_param);
00866 void onSetConfigurationSet(const coil::Properties& config_set);
00867 void onAddConfigurationSet(const coil::Properties& config_set);
00868 void onRemoveConfigurationSet(const char* config_id);
00869 void onActivateSet(const char* config_id);
00870
00871 private:
00872 ConfigAdmin(const ConfigAdmin& ca);
00873 ConfigAdmin& operator=(const ConfigAdmin& ca);
00874
00875 struct find_conf
00876 {
00877 std::string m_name;
00878 find_conf(const char* name) : m_name(name) {};
00879 bool operator()(ConfigBase* conf)
00880 {
00881 if (conf == 0) { return false; }
00882 return (m_name == conf->name);
00883 }
00884 };
00885
00886 coil::Properties& m_configsets;
00887 coil::Properties m_emptyconf;
00888 std::vector<ConfigBase*> m_params;
00889 std::string m_activeId;
00890 bool m_active;
00891 bool m_changed;
00892 std::vector<std::string> m_newConfig;
00893
00894 OnUpdateCallback* m_updateCb;
00895 OnUpdateParamCallback* m_updateParamCb;
00896 OnSetConfigurationSetCallback* m_setConfigSetCb;
00897 OnAddConfigurationAddCallback* m_addConfigSetCb;
00898 OnRemoveConfigurationSetCallback* m_removeConfigSetCb;
00899 OnActivateSetCallback* m_activateSetCb;
00900
00901
00902 };
00903 };
00904 #endif // ConfigAdmin_h