00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef CONFIGURATION_H
00023 #define CONFIGURATION_H
00024
00025 #include "utils/stringutils.h"
00026
00027 #include <libxml/xmlwriter.h>
00028
00029 #include <cassert>
00030 #include <list>
00031 #include <map>
00032 #include <string>
00033
00034 class ConfigListener;
00035 class ConfigurationObject;
00036
00044 template <class T, class CONT>
00045 class ConfigurationListManager
00046 {
00047 public:
00056 virtual ConfigurationObject *writeConfigItem(T value,
00057 ConfigurationObject *obj) = 0;
00058
00065 virtual CONT readConfigItem(ConfigurationObject *obj,
00066 CONT container) = 0;
00067 };
00068
00075 class ConfigurationObject
00076 {
00077 friend class Configuration;
00078
00079 public:
00080 virtual ~ConfigurationObject();
00081
00088 virtual void setValue(const std::string &key,
00089 const std::string &value);
00090
00097 std::string getValue(const std::string &key,
00098 const std::string &deflt) const;
00099
00100 int getValue(const std::string &key, int deflt) const;
00101
00102 unsigned getValue(const std::string &key, unsigned deflt) const;
00103
00104 double getValue(const std::string &key, double deflt) const;
00105
00109 virtual void clear();
00110
00123 template <class IT, class T, class CONT>
00124 void setList(const std::string &name, IT begin, IT end,
00125 ConfigurationListManager<T, CONT> *manager)
00126 {
00127 ConfigurationObject *nextobj = new ConfigurationObject;
00128 deleteList(name);
00129 ConfigurationList *list = &(mContainerOptions[name]);
00130
00131 for (IT it = begin; it != end; it++) {
00132 ConfigurationObject *wrobj = manager->writeConfigItem(*it, nextobj);
00133 if (wrobj) {
00134 assert (wrobj == nextobj);
00135 nextobj = new ConfigurationObject;
00136 list->push_back(wrobj);
00137 } else
00138 nextobj->clear();
00139 }
00140
00141 delete nextobj;
00142 }
00143
00155 template<class T, class CONT>
00156 CONT getList(const std::string &name, CONT empty, ConfigurationListManager<T, CONT> *manager)
00157 {
00158 ConfigurationList *list = &(mContainerOptions[name]);
00159 CONT container = empty;
00160
00161 for (ConfigurationList::const_iterator it = list->begin(); it != list->end(); it++)
00162 container = manager->readConfigItem(*it, container);
00163
00164 return container;
00165 }
00166
00167 protected:
00168 virtual void initFromXML(xmlNodePtr node);
00169 virtual void writeToXML(xmlTextWriterPtr writer);
00170
00171 void deleteList(const std::string &name);
00172
00173 typedef std::map<std::string, std::string> Options;
00174 Options mOptions;
00175
00176 typedef std::list<ConfigurationObject *> ConfigurationList;
00177 std::map<std::string, ConfigurationList> mContainerOptions;
00178 };
00179
00185 class Configuration : public ConfigurationObject
00186 {
00187 public:
00188 virtual ~Configuration() {}
00189
00195 void init(const std::string &filename);
00196
00200 void write();
00201
00205 void addListener(const std::string &key, ConfigListener *listener);
00206
00211 void removeListener(const std::string &key, ConfigListener *listener);
00212
00213 void setValue(const std::string &key, const std::string &value);
00214
00215 inline void setValue(const std::string &key, float value)
00216 { setValue(key, toString(value)); }
00217
00218 inline void setValue(const std::string &key, double value)
00219 { setValue(key, toString(value)); }
00220
00221 inline void setValue(const std::string &key, int value)
00222 { setValue(key, toString(value)); }
00223
00224 inline void setValue(const std::string &key, unsigned value)
00225 { setValue(key, toString(value)); }
00226
00227 inline void setValue(const std::string &key, bool value)
00228 { setValue(key, value ? std::string("1") : std::string("0")); }
00229
00230 private:
00231 typedef std::list<ConfigListener*> Listeners;
00232 typedef Listeners::iterator ListenerIterator;
00233 typedef std::map<std::string, Listeners> ListenerMap;
00234 typedef ListenerMap::iterator ListenerMapIterator;
00235 ListenerMap mListenerMap;
00236
00237 std::string mConfigPath;
00238 };
00239
00240 extern Configuration branding;
00241 extern Configuration config;
00242
00243 #endif