00001
00019 #ifndef RTC_CONFIGADMIN_H
00020 #define RTC_CONFIGADMIN_H
00021
00022 #include <string>
00023 #include <vector>
00024 #include <iostream>
00025 #include <coil/Properties.h>
00026 #include <coil/stringutil.h>
00027 #include <rtm/ConfigurationListener.h>
00028
00043 namespace RTC
00044 {
00056 typedef ConfigurationSetNameListener OnUpdateCallback;
00057
00069 typedef ConfigurationParamListener OnUpdateParamCallback;
00070
00082 typedef ConfigurationSetListener OnSetConfigurationSetCallback;
00083
00095 typedef ConfigurationSetListener OnAddConfigurationAddCallback;
00096
00108 typedef ConfigurationSetNameListener OnRemoveConfigurationSetCallback;
00109
00121 typedef ConfigurationSetNameListener OnActivateSetCallback;
00122
00123
00124 class ConfigAdmin;
00125
00126
00127
00128
00159 struct ConfigBase
00160 {
00182 ConfigBase(const char* name_, const char* def_val)
00183 : name(name_), default_value(def_val),
00184 string_value(""), m_admin(NULL), m_callback(NULL)
00185 {}
00186
00202 virtual ~ConfigBase(void){};
00203
00204
00205 typedef void (ConfigAdmin::*CallbackFunc)(const char*, const char*);
00206
00223 void setCallback(ConfigAdmin* cadmin, CallbackFunc cbf);
00224
00240 void notifyUpdate(const char* key, const char* val);
00241
00267 virtual bool update(const char* val) = 0;
00268
00276 const char* name;
00277
00285 const char* default_value;
00286
00287 protected:
00295 std::string string_value;
00303 ConfigAdmin* m_admin;
00311 CallbackFunc m_callback;
00312 };
00313
00314
00315
00316
00349 template <typename VarType,
00350 typename TransFunc = bool (*)(VarType&, const char*)>
00351 class Config
00352 : public ConfigBase
00353 {
00354 public:
00380 Config(const char* name, VarType& var, const char* def_val,
00381 TransFunc trans = coil::stringTo)
00382 : ConfigBase(name, def_val), m_var(var), m_trans(trans)
00383 {
00384 }
00385
00401 virtual ~Config(void){}
00402
00426 virtual bool update(const char* val)
00427 {
00428 if (string_value == val) { return true; }
00429 string_value = val;
00430
00431 if ((*m_trans)(m_var, val))
00432 {
00433 notifyUpdate(name, val);
00434 return true;
00435 }
00436 (*m_trans)(m_var, default_value);
00437 notifyUpdate(name, val);
00438 return false;
00439 }
00440
00441 protected:
00449 VarType& m_var;
00450
00459 TransFunc m_trans;
00460 };
00461
00462
00463
00464
00610 class ConfigAdmin
00611 {
00612 public:
00632 ConfigAdmin(coil::Properties& prop);
00633
00649 ~ConfigAdmin(void);
00650
00691 template <typename VarType>
00692 bool bindParameter(const char* param_name, VarType& var,
00693 const char* def_val,
00694 bool (*trans)(VarType&, const char*) = coil::stringTo)
00695 {
00696 if (param_name == 0) { return false; }
00697 if (def_val == 0) { return false; }
00698 if (isExist(param_name)) { return false; }
00699 if (!trans(var, def_val)) { return false; }
00700 Config<VarType>* c = new Config<VarType>(param_name, var, def_val, trans);
00701 m_params.push_back(c);
00702 c->setCallback(this, &RTC::ConfigAdmin::onUpdateParam);
00703 update(getActiveId(), param_name);
00704 return true;
00705 }
00706
00732 bool unbindParameter(const char* param_name);
00733
00760 void update(void);
00761
00796 void update(const char* config_set);
00797
00835 void update(const char* config_set, const char* config_param);
00836
00863 bool isExist(const char* name);
00864
00885 bool isChanged(void) {return m_changed;}
00886
00906 coil::vstring& changedParameters() { return m_changedParam; }
00907
00927 const char* getActiveId(void) {return m_activeId.c_str();}
00928
00953 bool haveConfig(const char* config_id)
00954 {
00955 return (m_configsets.hasKey(config_id) == NULL) ? false : true;
00956 }
00957
00978 bool isActive(void)
00979 {
00980 return m_active;
00981 }
00982
00983
00984
00985
01005 const std::vector<coil::Properties*>& getConfigurationSets(void);
01006
01034 const coil::Properties& getConfigurationSet(const char* config_id);
01035
01065 bool setConfigurationSetValues(const coil::Properties& configuration_set);
01066
01090 const coil::Properties& getActiveConfigurationSet(void);
01091
01115 bool addConfigurationSet(const coil::Properties& configuration_set);
01116
01171 bool removeConfigurationSet(const char* config_id);
01172
01200 bool activateConfigurationSet(const char* config_id);
01201
01202
01203
01204
01205 void setOnUpdate(OnUpdateCallback* cb);
01206
01207 void setOnUpdateParam(OnUpdateParamCallback* cb);
01208
01209 void setOnSetConfigurationSet(OnSetConfigurationSetCallback* cb);
01210
01211 void setOnAddConfigurationSet(OnAddConfigurationAddCallback* cb);
01212
01213 void setOnRemoveConfigurationSet(OnRemoveConfigurationSetCallback* cb);
01214
01215 void setOnActivateSet(OnActivateSetCallback* cb);
01216
01217
01218
01219
01252 void addConfigurationParamListener(ConfigurationParamListenerType type,
01253 ConfigurationParamListener* listener,
01254 bool autoclean = true);
01255
01280 void removeConfigurationParamListener(ConfigurationParamListenerType type,
01281 ConfigurationParamListener* listener);
01282
01314 void addConfigurationSetListener(ConfigurationSetListenerType type,
01315 ConfigurationSetListener* listener,
01316 bool autoclean = true);
01317
01340 void removeConfigurationSetListener(ConfigurationSetListenerType type,
01341 ConfigurationSetListener* listener);
01342
01377 void
01378 addConfigurationSetNameListener(ConfigurationSetNameListenerType type,
01379 ConfigurationSetNameListener* listener,
01380 bool autoclean = true);
01381
01408 void
01409 removeConfigurationSetNameListener(ConfigurationSetNameListenerType type,
01410 ConfigurationSetNameListener* listener);
01411
01412 protected:
01432 void onUpdate(const char* config_set);
01433
01455 void onUpdateParam(const char* config_set, const char* config_param);
01456
01476 void onSetConfigurationSet(const coil::Properties& config_set);
01477
01497 void onAddConfigurationSet(const coil::Properties& config_set);
01498
01518 void onRemoveConfigurationSet(const char* config_id);
01519
01539 void onActivateSet(const char* config_id);
01540
01541 private:
01542 ConfigAdmin(const ConfigAdmin& ca);
01543 ConfigAdmin& operator=(const ConfigAdmin& ca);
01544
01545 struct find_conf
01546 {
01547 std::string m_name;
01548 find_conf(const char* name) : m_name(name) {};
01549 bool operator()(ConfigBase* conf)
01550 {
01551 if (conf == 0) { return false; }
01552 return (m_name == conf->name);
01553 }
01554 };
01555
01556 coil::Properties& m_configsets;
01557 coil::Properties m_emptyconf;
01558 std::vector<ConfigBase*> m_params;
01559 std::string m_activeId;
01560 bool m_active;
01561 bool m_changed;
01562 coil::vstring m_changedParam;
01563 coil::vstring m_newConfig;
01564 ConfigurationListeners m_listeners;
01565
01566 };
01567 };
01568 #endif // RTC_CONFIGADMIN_H