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
00125
00126
00157 struct ConfigBase
00158 {
00180 ConfigBase(const char* name_, const char* def_val)
00181 : name(name_), default_value(def_val) {}
00182
00198 virtual ~ConfigBase(void){};
00199
00225 virtual bool update(const char* val) = 0;
00226
00234 const char* name;
00235
00243 const char* default_value;
00244 };
00245
00246
00247
00248
00281 template <typename VarType,
00282 typename TransFunc = bool (*)(VarType&, const char*)>
00283 class Config
00284 : public ConfigBase
00285 {
00286 public:
00312 Config(const char* name, VarType& var, const char* def_val,
00313 TransFunc trans = coil::stringTo)
00314 : ConfigBase(name, def_val), m_var(var), m_trans(trans)
00315 {
00316 }
00317
00333 virtual ~Config(void){}
00334
00358 virtual bool update(const char* val)
00359 {
00360 if ((*m_trans)(m_var, val)) { return true; }
00361 (*m_trans)(m_var, default_value);
00362 return false;
00363 }
00364
00365 protected:
00373 VarType& m_var;
00374
00383 TransFunc m_trans;
00384 };
00385
00386
00387
00388
00534 class ConfigAdmin
00535 {
00536 public:
00556 ConfigAdmin(coil::Properties& prop);
00557
00573 ~ConfigAdmin(void);
00574
00611 template <typename VarType>
00612 bool bindParameter(const char* param_name, VarType& var,
00613 const char* def_val,
00614 bool (*trans)(VarType&, const char*) = coil::stringTo)
00615 {
00616 if (param_name == 0) { return false; }
00617 if (def_val == 0) { return false; }
00618 if (isExist(param_name)) { return false; }
00619 if (!trans(var, def_val)) { return false; }
00620 m_params.push_back(new Config<VarType>(param_name, var, def_val, trans));
00621 return true;
00622 }
00623
00650 void update(void);
00651
00686 void update(const char* config_set);
00687
00725 void update(const char* config_set, const char* config_param);
00726
00753 bool isExist(const char* name);
00754
00775 bool isChanged(void) {return m_changed;}
00776
00796 const char* getActiveId(void) {return m_activeId.c_str();}
00797
00822 bool haveConfig(const char* config_id)
00823 {
00824 return (m_configsets.hasKey(config_id) == NULL) ? false : true;
00825 }
00826
00847 bool isActive(void)
00848 {
00849 return m_active;
00850 }
00851
00852
00853
00854
00874 const std::vector<coil::Properties*>& getConfigurationSets(void);
00875
00903 const coil::Properties& getConfigurationSet(const char* config_id);
00904
00934 bool setConfigurationSetValues(const coil::Properties& configuration_set);
00935
00959 const coil::Properties& getActiveConfigurationSet(void);
00960
00984 bool addConfigurationSet(const coil::Properties& configuration_set);
00985
01040 bool removeConfigurationSet(const char* config_id);
01041
01069 bool activateConfigurationSet(const char* config_id);
01070
01071
01072
01073
01074 void setOnUpdate(OnUpdateCallback* cb);
01075
01076 void setOnUpdateParam(OnUpdateParamCallback* cb);
01077
01078 void setOnSetConfigurationSet(OnSetConfigurationSetCallback* cb);
01079
01080 void setOnAddConfigurationSet(OnAddConfigurationAddCallback* cb);
01081
01082 void setOnRemoveConfigurationSet(OnRemoveConfigurationSetCallback* cb);
01083
01084 void setOnActivateSet(OnActivateSetCallback* cb);
01085
01086
01087
01088
01121 void addConfigurationParamListener(ConfigurationParamListenerType type,
01122 ConfigurationParamListener* listener,
01123 bool autoclean = true);
01124
01149 void removeConfigurationParamListener(ConfigurationParamListenerType type,
01150 ConfigurationParamListener* listener);
01151
01183 void addConfigurationSetListener(ConfigurationSetListenerType type,
01184 ConfigurationSetListener* listener,
01185 bool autoclean = true);
01186
01209 void removeConfigurationSetListener(ConfigurationSetListenerType type,
01210 ConfigurationSetListener* listener);
01211
01246 void
01247 addConfigurationSetNameListener(ConfigurationSetNameListenerType type,
01248 ConfigurationSetNameListener* listener,
01249 bool autoclean = true);
01250
01277 void
01278 removeConfigurationSetNameListener(ConfigurationSetNameListenerType type,
01279 ConfigurationSetNameListener* listener);
01280
01281 protected:
01301 void onUpdate(const char* config_set);
01302
01324 void onUpdateParam(const char* config_set, const char* config_param);
01325
01345 void onSetConfigurationSet(const coil::Properties& config_set);
01346
01366 void onAddConfigurationSet(const coil::Properties& config_set);
01367
01387 void onRemoveConfigurationSet(const char* config_id);
01388
01408 void onActivateSet(const char* config_id);
01409
01410 private:
01411 ConfigAdmin(const ConfigAdmin& ca);
01412 ConfigAdmin& operator=(const ConfigAdmin& ca);
01413
01414 struct find_conf
01415 {
01416 std::string m_name;
01417 find_conf(const char* name) : m_name(name) {};
01418 bool operator()(ConfigBase* conf)
01419 {
01420 if (conf == 0) { return false; }
01421 return (m_name == conf->name);
01422 }
01423 };
01424
01425 coil::Properties& m_configsets;
01426 coil::Properties m_emptyconf;
01427 std::vector<ConfigBase*> m_params;
01428 std::string m_activeId;
01429 bool m_active;
01430 bool m_changed;
01431 std::vector<std::string> m_newConfig;
01432 ConfigurationListeners m_listeners;
01433
01434 };
01435 };
01436 #endif // RTC_CONFIGADMIN_H