| 1 |
/* |
| 2 |
* The Mana World |
| 3 |
* Copyright (C) 2004 The Mana World Development Team |
| 4 |
* |
| 5 |
* This file is part of The Mana World. |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef CONFIGURATION_H |
| 23 |
#define CONFIGURATION_H |
| 24 |
|
| 25 |
#include "utils/stringutils.h" |
| 26 |
|
| 27 |
#include <libxml/xmlwriter.h> |
| 28 |
|
| 29 |
#include <cassert> |
| 30 |
#include <list> |
| 31 |
#include <map> |
| 32 |
#include <string> |
| 33 |
|
| 34 |
class ConfigListener; |
| 35 |
class ConfigurationObject; |
| 36 |
|
| 37 |
/** |
| 38 |
* Configuration list manager interface; responsible for |
| 39 |
* serializing/deserializing configuration choices in containers. |
| 40 |
* |
| 41 |
* \param T Type of the container elements to serialise |
| 42 |
* \param CONT Type of the container we (de)serialise |
| 43 |
*/ |
| 44 |
template <class T, class CONT> |
| 45 |
class ConfigurationListManager |
| 46 |
{ |
| 47 |
public: |
| 48 |
/** |
| 49 |
* Writes a value into a configuration object |
| 50 |
* |
| 51 |
* \param value The value to write out |
| 52 |
* \param obj The configuation object to write to |
| 53 |
* \return obj, or otherwise NULL to indicate that this option should |
| 54 |
* be skipped |
| 55 |
*/ |
| 56 |
virtual ConfigurationObject *writeConfigItem(T value, |
| 57 |
ConfigurationObject *obj) = 0; |
| 58 |
|
| 59 |
/** |
| 60 |
* Reads a value from a configuration object |
| 61 |
* |
| 62 |
* \param obj The configuration object to read from |
| 63 |
* \param container The container to insert the object to |
| 64 |
*/ |
| 65 |
virtual CONT readConfigItem(ConfigurationObject *obj, |
| 66 |
CONT container) = 0; |
| 67 |
}; |
| 68 |
|
| 69 |
/** |
| 70 |
* Configuration object, mapping values to names and possibly containing |
| 71 |
* lists of further configuration objects |
| 72 |
* |
| 73 |
* \ingroup CORE |
| 74 |
*/ |
| 75 |
class ConfigurationObject |
| 76 |
{ |
| 77 |
friend class Configuration; |
| 78 |
|
| 79 |
public: |
| 80 |
virtual ~ConfigurationObject(); |
| 81 |
|
| 82 |
/** |
| 83 |
* Sets an option using a string value. |
| 84 |
* |
| 85 |
* \param key Option identifier. |
| 86 |
* \param value Value. |
| 87 |
*/ |
| 88 |
virtual void setValue(const std::string &key, |
| 89 |
const std::string &value); |
| 90 |
|
| 91 |
/** |
| 92 |
* Gets a value as string. |
| 93 |
* |
| 94 |
* \param key Option identifier. |
| 95 |
* \param deflt Default option if not there or error. |
| 96 |
*/ |
| 97 |
std::string getValue(const std::string &key, |
| 98 |
const std::string &deflt) const; |
| 99 |
|
| 100 |
int getValue(const std::string &key, int deflt) const; |
| 101 |
|
| 102 |
unsigned getValue(const std::string &key, unsigned deflt) const; |
| 103 |
|
| 104 |
double getValue(const std::string &key, double deflt) const; |
| 105 |
|
| 106 |
/** |
| 107 |
* Re-sets all data in the configuration |
| 108 |
*/ |
| 109 |
virtual void clear(); |
| 110 |
|
| 111 |
/** |
| 112 |
* Serialises a container into a list of configuration options |
| 113 |
* |
| 114 |
* \param IT Iterator type over CONT |
| 115 |
* \param T Elements that IT iterates over |
| 116 |
* \param CONT The associated container type |
| 117 |
* |
| 118 |
* \param name Name of the list the elements should be stored under |
| 119 |
* \param begin Iterator start |
| 120 |
* \param end Iterator end |
| 121 |
* \param manager An object capable of serialising T items |
| 122 |
*/ |
| 123 |
template <class IT, class T, class CONT> |
| 124 |
void setList(const std::string &name, IT begin, IT end, |
| 125 |
ConfigurationListManager<T, CONT> *manager) |
| 126 |
{ |
| 127 |
ConfigurationObject *nextobj = new ConfigurationObject; |
| 128 |
deleteList(name); |
| 129 |
ConfigurationList *list = &(mContainerOptions[name]); |
| 130 |
|
| 131 |
for (IT it = begin; it != end; it++) { |
| 132 |
ConfigurationObject *wrobj = manager->writeConfigItem(*it, nextobj); |
| 133 |
if (wrobj) { // wrote something |
| 134 |
assert (wrobj == nextobj); |
| 135 |
nextobj = new ConfigurationObject; |
| 136 |
list->push_back(wrobj); |
| 137 |
} else |
| 138 |
nextobj->clear(); // you never know... |
| 139 |
} |
| 140 |
|
| 141 |
delete nextobj; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Serialises a container into a list of configuration options |
| 146 |
* |
| 147 |
* \param IT Iterator type over CONT |
| 148 |
* \param T Elements that IT iterates over |
| 149 |
* \param CONT The associated container type |
| 150 |
* |
| 151 |
* \param name Name of the list the elements should be read from under |
| 152 |
* \param empty Initial (empty) container to write to |
| 153 |
* \param manager An object capable of deserialising items into CONT |
| 154 |
*/ |
| 155 |
template<class T, class CONT> |
| 156 |
CONT getList(const std::string &name, CONT empty, ConfigurationListManager<T, CONT> *manager) |
| 157 |
{ |
| 158 |
ConfigurationList *list = &(mContainerOptions[name]); |
| 159 |
CONT container = empty; |
| 160 |
|
| 161 |
for (ConfigurationList::const_iterator it = list->begin(); it != list->end(); it++) |
| 162 |
container = manager->readConfigItem(*it, container); |
| 163 |
|
| 164 |
return container; |
| 165 |
} |
| 166 |
|
| 167 |
protected: |
| 168 |
virtual void initFromXML(xmlNodePtr node); |
| 169 |
virtual void writeToXML(xmlTextWriterPtr writer); |
| 170 |
|
| 171 |
void deleteList(const std::string &name); |
| 172 |
|
| 173 |
typedef std::map<std::string, std::string> Options; |
| 174 |
Options mOptions; |
| 175 |
|
| 176 |
typedef std::list<ConfigurationObject *> ConfigurationList; |
| 177 |
std::map<std::string, ConfigurationList> mContainerOptions; |
| 178 |
}; |
| 179 |
|
| 180 |
/** |
| 181 |
* Configuration handler for reading (and writing). |
| 182 |
* |
| 183 |
* \ingroup CORE |
| 184 |
*/ |
| 185 |
class Configuration : public ConfigurationObject |
| 186 |
{ |
| 187 |
public: |
| 188 |
virtual ~Configuration() {} |
| 189 |
|
| 190 |
/** |
| 191 |
* Reads config file and parse all options into memory. |
| 192 |
* |
| 193 |
* \param filename path to config file |
| 194 |
*/ |
| 195 |
void init(const std::string &filename); |
| 196 |
|
| 197 |
/** |
| 198 |
* Writes the current settings back to the config file. |
| 199 |
*/ |
| 200 |
void write(); |
| 201 |
|
| 202 |
/** |
| 203 |
* Adds a listener to the listen list of the specified config option. |
| 204 |
*/ |
| 205 |
void addListener(const std::string &key, ConfigListener *listener); |
| 206 |
|
| 207 |
/** |
| 208 |
* Removes a listener from the listen list of the specified config |
| 209 |
* option. |
| 210 |
*/ |
| 211 |
void removeListener(const std::string &key, ConfigListener *listener); |
| 212 |
|
| 213 |
void setValue(const std::string &key, const std::string &value); |
| 214 |
|
| 215 |
inline void setValue(const std::string &key, float value) |
| 216 |
{ setValue(key, toString(value)); } |
| 217 |
|
| 218 |
inline void setValue(const std::string &key, double value) |
| 219 |
{ setValue(key, toString(value)); } |
| 220 |
|
| 221 |
inline void setValue(const std::string &key, int value) |
| 222 |
{ setValue(key, toString(value)); } |
| 223 |
|
| 224 |
inline void setValue(const std::string &key, unsigned value) |
| 225 |
{ setValue(key, toString(value)); } |
| 226 |
|
| 227 |
inline void setValue(const std::string &key, bool value) |
| 228 |
{ setValue(key, value ? std::string("1") : std::string("0")); } |
| 229 |
|
| 230 |
private: |
| 231 |
typedef std::list<ConfigListener*> Listeners; |
| 232 |
typedef Listeners::iterator ListenerIterator; |
| 233 |
typedef std::map<std::string, Listeners> ListenerMap; |
| 234 |
typedef ListenerMap::iterator ListenerMapIterator; |
| 235 |
ListenerMap mListenerMap; |
| 236 |
|
| 237 |
std::string mConfigPath; /**< Location of config file */ |
| 238 |
}; |
| 239 |
|
| 240 |
extern Configuration branding; |
| 241 |
extern Configuration config; |
| 242 |
|
| 243 |
#endif |