00001
00019 #ifndef ConfigAdmin_h
00020 #define ConfigAdmin_h
00021
00022 #include <rtm/Properties.h>
00023 #include <rtm/StringUtil.h>
00024 #include <string>
00025 #include <vector>
00026 #include <iostream>
00027
00042 namespace RTC
00043 {
00044
00045
00046
00077 class ConfigBase
00078 {
00079 public:
00101 ConfigBase(const char* name_, const char* def_val)
00102 : name(name_), default_value(def_val) {}
00103
00119 virtual ~ConfigBase(){};
00120
00146 virtual bool update(const char* val) = 0;
00147
00155 const char* name;
00156
00164 const char* default_value;
00165 };
00166
00167
00168
00169
00201 template <typename VarType,
00202 typename TransFunc = bool (*)(VarType&, const char*)>
00203 class Config
00204 : public ConfigBase
00205 {
00206 public:
00232 Config(const char* name, VarType& var, const char* def_val,
00233 TransFunc trans = stringTo)
00234 : ConfigBase(name, def_val), m_var(var), m_trans(trans)
00235 {
00236 }
00237
00253 virtual ~Config(){}
00254
00278 virtual bool update(const char* val)
00279 {
00280 if ((*m_trans)(m_var, val)) return true;
00281 (*m_trans)(m_var, default_value);
00282 return false;
00283 }
00284
00285 protected:
00293 VarType& m_var;
00294
00303 TransFunc m_trans;
00304 };
00305
00306
00307
00308
00328 class ConfigAdmin
00329 {
00330 public:
00350 ConfigAdmin(RTC::Properties& prop);
00351
00367 ~ConfigAdmin();
00368
00405 template <typename VarType>
00406 bool bindParameter(const char* param_name, VarType& var,
00407 const char* def_val,
00408 bool (*trans)(VarType&, const char*) = ::stringTo)
00409 {
00410 if (isExist(param_name)) return false;
00411 if (!::stringTo(var, def_val)) return false;
00412 m_params.push_back(new Config<VarType>(param_name, var, def_val, trans));
00413 return true;
00414 }
00415
00441 void update(const char* config_set);
00442
00468 void update(const char* config_set, const char* config_param);
00469
00496 void update();
00497
00522 bool isExist(const char* name);
00523
00544 bool isChanged() {return m_changed;}
00545
00565 const char* getActiveId() {return m_activeId.c_str();}
00566
00591 bool haveConfig(const char* config_id)
00592 {
00593 return (m_configsets.hasKey(config_id) == NULL) ? false : true;
00594 }
00595
00616 bool isActive()
00617 {
00618 return m_active;
00619 }
00620
00621
00622
00623
00643 const std::vector<Properties*>& getConfigurationSets();
00644
00672 const Properties& getConfigurationSet(const char* config_id);
00673
00703 bool setConfigurationSetValues(const char* config_id,
00704 const RTC::Properties& configuration_set);
00705
00729 const Properties& getActiveConfigurationSet();
00730
00754 bool addConfigurationSet(const Properties& configuration_set);
00755
00783 bool removeConfigurationSet(const char* config_id);
00784
00812 bool activateConfigurationSet(const char* config_id);
00813
00814 private:
00815 ConfigAdmin(const ConfigAdmin& ca) : m_configsets(ca.m_configsets) {};
00816 ConfigAdmin& operator=(const ConfigAdmin& ca){return *this;};
00817
00818 struct find_conf
00819 {
00820 std::string m_name;
00821 find_conf(const char* name) : m_name(name) {};
00822 bool operator()(ConfigBase* conf)
00823 {
00824 return m_name == conf->name;
00825 }
00826 };
00827
00828 RTC::Properties& m_configsets;
00829 RTC::Properties m_emptyconf;
00830 std::vector<ConfigBase*> m_params;
00831 std::string m_activeId;
00832 bool m_active;
00833 bool m_changed;
00834 std::vector<std::string> m_newConfig;
00835 };
00836 };
00837 #endif // ConfigAdmin_h