Registry.h

Go to the documentation of this file.
00001 #ifndef __REGISTRY_H__
00002 #define __REGISTRY_H__
00003 
00004 /*
00005 
00006 $Header$
00007 
00008 */
00009 
00010 #include "Error.h"
00011 #include "RegistryBase.h"
00012 #include "Singleton.h"
00013 #include "Signal.h"
00014 #include "Value.h"
00015 #include "Config.h"
00016 #include "S11n.h"
00017 
00018 // Messages.
00019 static const char *const MSG_REGISTRY_TYPE_MISMATCH     = "Registry type mismatch.";
00020 static const char *const MSG_REGISTRY_HAS_STATIC_GETTER = "Registry entry '%s' already has a static getter. You set a regular one. That static getter will be removed.";
00021 static const char *const MSG_REGISTRY_HAS_GETTER        = "Registry entry '%s' already has a regular getter. You set a static one. That regular getter will be removed.";
00022 static const char *const MSG_REGISTRY_HAS_STATIC_SETTER = "Registry entry '%s' already has a static setter. You set a regular one. That static setter will be removed.";
00023 static const char *const MSG_REGISTRY_HAS_SETTER        = "Registry entry '%s' already has a regular setter. You set a static one. That regular setter will be removed.";
00024 
00025 using namespace std;
00026 
00027 // Declaration.
00028 
00029 template<class T>
00030 class Registry : public RegistryBase
00031 {
00032 
00033   PYLON_OBJECT
00034 
00035 public:
00036 
00037   typedef int (Object::*Getter)(const String &, T &);
00038   typedef int (Object::*Setter)(const String &, const T &);
00039 
00040   typedef int (*StaticGetter)(const String &, T &);
00041   typedef int (*StaticSetter)(const String &, const T &);
00042 
00043   Registry(const String &aName, const T &defaultValue = T(), const String &manual = "",
00044            const RegistryFlags flags = REG_FLAGS_DEFAULT, Validator<T> *validator = NULL);
00045 
00046   virtual ~Registry();
00047 
00048   inline virtual bool hasGetter() const;
00049   int setGetter(Getter getter);
00050   int setGetter(StaticGetter getter);
00051 
00052   inline virtual bool hasSetter() const;
00053   int setSetter(Setter setter);
00054   int setSetter(StaticSetter setter);
00055 
00056   int set(const T &value);
00057   int get(T &value);
00058 
00059   static int set(const String &name, const T &value, Object *owner = NULL);
00060   static int get(const String &name, T &value, Object *owner = NULL);
00061 
00062   virtual int toString(String &str);
00063   virtual int fromString(const String &str);
00064 
00065   // Signals.
00066   SIGNAL(set, const T &);
00067   SIGNAL(got, const T &);
00068 
00069 private:
00070 
00071   Value<T> _value;
00072 
00073   Getter _getter;
00074   Setter _setter;
00075   StaticGetter _staticGetter;
00076   StaticSetter _staticSetter;
00077 };
00078 
00079 // Implementation.
00080 
00081 // Public methods.
00082 
00083 template<class T>
00084 Registry<T>::Registry(const String &aName, const T &defaultValue, const String &manual,
00085                       const RegistryFlags flags, Validator<T> *validator) :
00086   RegistryBase(aName, manual, flags),
00087   _getter(NULL),
00088   _setter(NULL),
00089   _staticGetter(NULL),
00090   _staticSetter(NULL)
00091 {
00092   int res;
00093 
00094   if (FAILED(res = _value.setValidator(validator))) {
00095     failure(ERROR_BACKTRACE(res));
00096     return;
00097   }
00098 
00099   if (persistency() == REG_PERSISTENCY_YES) {
00100     T value;
00101 
00102     if (FAILED(res = CONFIG()->get<T>(name(), value, defaultValue))) {
00103       failure(ERROR_BACKTRACE(res));
00104       return;
00105     }
00106     if (FAILED(res = _value.set(value))) {
00107       failure(ERROR_BACKTRACE(res));
00108       return;
00109     }
00110   } else
00111       if (FAILED(res = _value.set(defaultValue))) {
00112         failure(ERROR_BACKTRACE(res));
00113         return;
00114       }
00115 }
00116 
00117 template<class T>
00118 Registry<T>::~Registry()
00119 {
00120 }
00121 
00122 template<class T>
00123 inline bool Registry<T>::hasGetter() const
00124 {
00125   return _staticGetter || (owner() && _getter);
00126 }
00127 
00128 template<class T>
00129 int Registry<T>::setGetter(Getter getter)
00130 {
00131   if (_staticGetter)
00132     WARNING(MSG_REGISTRY_HAS_STATIC_GETTER, CSTRING(name()));
00133   _staticGetter = NULL;
00134   _getter = getter;
00135   return OK;
00136 }
00137 
00138 template<class T>
00139 int Registry<T>::setGetter(StaticGetter getter)
00140 {
00141   if (_getter)
00142     WARNING(MSG_REGISTRY_HAS_GETTER, CSTRING(name()));
00143   _getter = NULL;
00144   _staticGetter = getter;
00145   return OK;
00146 }
00147 
00148 template<class T>
00149 inline bool Registry<T>::hasSetter() const
00150 {
00151   return _staticSetter || (owner() && _setter);
00152 }
00153 
00154 template<class T>
00155 int Registry<T>::setSetter(Setter setter)
00156 {
00157   if (_staticSetter)
00158     WARNING(MSG_REGISTRY_HAS_STATIC_SETTER, CSTRING(name()));
00159   _staticSetter = NULL;
00160   _setter = setter;
00161   return OK;
00162 }
00163 
00164 template<class T>
00165 int Registry<T>::setSetter(StaticSetter setter)
00166 {
00167   if (_setter)
00168     WARNING(MSG_REGISTRY_HAS_SETTER, CSTRING(name()));
00169   _setter = NULL;
00170   _staticSetter = setter;
00171   return OK;
00172 }
00173 
00174 template<class T>
00175 int Registry<T>::set(const T &value)
00176 {
00177   int res;
00178 
00179   if (_staticSetter) {
00180     if (FAILED(res = _staticSetter(name(), value)))
00181       return ERROR_BACKTRACE(res);
00182   } else
00183       if (owner() && _setter)
00184         if (FAILED(res = (owner()->*_setter)(name(), value)))
00185           return ERROR_BACKTRACE(res);
00186 
00187   _value.set(value);
00188   if (persistency() == REG_PERSISTENCY_YES)
00189     if (FAILED(res = CONFIG()->set<T>(name(), value)))
00190       return ERROR_BACKTRACE(res);
00191 
00192   EMIT(set, value);
00193   return OK;
00194 }
00195 
00196 template<class T>
00197 int Registry<T>::get(T &value)
00198 {
00199   bool fromGetter = false;
00200   int res;
00201 
00202   if (_staticGetter) {
00203     if (FAILED(res = _staticGetter(name(), value)))
00204       return ERROR_BACKTRACE(res);
00205     else
00206       fromGetter = true;
00207   } else
00208       if (owner() && _getter)
00209         if (FAILED(res = (owner()->*_getter)(name(), value)))
00210           return ERROR_BACKTRACE(res);
00211         else
00212           fromGetter = true;
00213 
00214   if (fromGetter) {
00215     _value.set(value);
00216     if (persistency() == REG_PERSISTENCY_YES)
00217       if (FAILED(res = CONFIG()->set<T>(name(), value)))
00218         return ERROR_BACKTRACE(res);
00219   } else
00220       value = _value.get();
00221 
00222   EMIT(got, value);
00223   return OK;
00224 }
00225 
00226 template<class T>
00227 int Registry<T>::set(const String &name, const T &value, Object *owner)
00228 {
00229   RegistryBase *regBase;
00230   Registry<T> *reg;
00231   int res;
00232 
00233   if (FAILED(res = RegistryBase::get(name, &regBase, owner)))
00234     return ERROR_BACKTRACE(res);
00235 
00236   if (!(reg = dynamic_cast<Registry<T> *>(regBase)))
00237     return ERROR(MSG_REGISTRY_TYPE_MISMATCH);
00238 
00239   if (FAILED(res = reg->set(value)))
00240     return ERROR_BACKTRACE(res);
00241 
00242   return OK;
00243 }
00244 
00245 template<class T>
00246 int Registry<T>::get(const String &name, T &value, Object *owner)
00247 {
00248   RegistryBase *regBase;
00249   Registry<T> *reg;
00250   int res;
00251 
00252   if (FAILED(res = RegistryBase::get(name, &regBase, owner)))
00253     return ERROR_BACKTRACE(res);
00254 
00255   if (!(reg = dynamic_cast<Registry<T> *>(regBase)))
00256     return ERROR(MSG_REGISTRY_TYPE_MISMATCH);
00257 
00258   if (FAILED(res = reg->get(value)))
00259     return ERROR_BACKTRACE(res);
00260 
00261   return OK;
00262 }
00263 
00264 template<class T>
00265 int Registry<T>::toString(String &str)
00266 {
00267   T value;
00268   int res;
00269 
00270   if (FAILED(res = get(value)))
00271     return ERROR_BACKTRACE(res);
00272   if (FAILED(res = S11n<T>::toString(value, str)))
00273     return ERROR_BACKTRACE(res);
00274 
00275   return OK;
00276 }
00277 
00278 template<class T>
00279 int Registry<T>::fromString(const String &str)
00280 {
00281   T value;
00282   int res;
00283 
00284   if (FAILED(res = S11n<T>::fromString(str, value)))
00285     return ERROR_BACKTRACE(res);
00286   if (FAILED(res = set(value)))
00287     return ERROR_BACKTRACE(res);
00288 
00289   return OK;
00290 }
00291 
00292 #define _REGISTRY(isPriv, valueType, entryName, defaultValue, ...) \
00293 static String registry_##entryName##_ownerClassName() \
00294 { \
00295   return CLASS_NAME(); \
00296 } \
00297 class Registry_##entryName : public Registry<valueType> \
00298 { \
00299   PYLON_OBJECT \
00300 public: \
00301   Registry_##entryName() : Registry<valueType>(registry_##entryName##_ownerClassName() + "." #entryName, defaultValue, ##__VA_ARGS__) {} \
00302   virtual ~Registry_##entryName() {}; \
00303   bool isPrivate() const \
00304   { \
00305     return isPriv; \
00306   } \
00307 };
00308 
00309 #define REGISTRY(valueType, entryName, defaultValue, ...) \
00310 _REGISTRY(false, valueType, entryName, defaultValue, ##__VA_ARGS__) \
00311 class Registry_##entryName##_Singleton : public Object, \
00312                                          public Singleton<Registry_##entryName> \
00313 { \
00314   PYLON_OBJECT \
00315   friend class Singleton<Registry_##entryName>; \
00316 private: \
00317   Registry_##entryName##_Singleton() : Object(), Singleton<Registry_##entryName>() {} \
00318   virtual ~Registry_##entryName##_Singleton() {} \
00319 }; \
00320 inline \
00321 Registry_##entryName *entryName() \
00322 { \
00323   return Registry_##entryName##_Singleton::instance(); \
00324 }
00325 
00326 #define REGISTRY_PRIVATE(valueType, entryName, defaultValue, ...) \
00327 _REGISTRY(true, valueType, entryName, defaultValue, ##__VA_ARGS__) \
00328 Registry_##entryName _registry_##entryName; \
00329 inline \
00330 Registry_##entryName *entryName() \
00331 { \
00332   return &_registry_##entryName; \
00333 }
00334 
00335 #define REGISTRY_TYPE(className, entryName) className::Registry_##entryName
00336 
00337 #define BIND(entryName) \
00338 { \
00339   int res; \
00340   if (FAILED(res = entryName()->isPrivate() ? entryName()->bind(this) : entryName()->bind())) { \
00341     failure(ERROR_BACKTRACE(res)); \
00342     return; \
00343   } \
00344 }
00345 
00346 #define REGISTRY_SET(className, entryName, value, ...) className::Registry_##entryName::set(#className "." #entryName, value, ##__VA_ARGS__)
00347 #define REGISTRY_GET(className, entryName, value, ...) className::Registry_##entryName::get(#className "." #entryName, value, ##__VA_ARGS__)
00348 
00349 #define SET_GETTER(entryName, ptr) \
00350   entryName()->setGetter(static_cast<Registry_##entryName::Getter>(&ptr))
00351 
00352 #define SET_STATIC_GETTER(entryName, ptr) \
00353   entryName()->setGetter(static_cast<Registry_##entryName::StaticGetter>(&ptr))
00354 
00355 #define SET_SETTER(entryName, ptr) \
00356   entryName()->setSetter(static_cast<Registry_##entryName::Setter>(&ptr))
00357 
00358 #define SET_STATIC_SETTER(entryName, ptr) \
00359   entryName()->setSetter(static_cast<Registry_##entryName::StaticSetter>(&ptr))
00360 
00361 #endif

Generated on Thu Sep 6 20:11:25 2007 for Pylon Application Platform by  doxygen 1.5.1