ManaPlus
configuration.h
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef CONFIGURATION_H
25 #define CONFIGURATION_H
26 
27 #include "utils/stringutils.h"
28 #ifdef ENABLE_PUGIXML
29 #include "utils/xmlwriter.h"
30 #else
31 #include "utils/xml.h"
32 #endif // ENABLE_PUGIXML
33 
34 #include "defaults.h"
35 #include "localconsts.h"
36 
37 class ConfigListener;
39 
47 template <class T, class CONT>
49 {
50  public:
52  { }
53 
55 
56 
64  virtual ConfigurationObject *writeConfigItem(const T &value,
66  *const obj) const = 0;
67 
74  virtual CONT readConfigItem(const ConfigurationObject *const obj,
75  CONT container)
76  const A_WARN_UNUSED = 0;
77 
79  { }
80 };
81 
89 {
90  friend class Configuration;
91 
92  public:
94 
95  virtual ~ConfigurationObject();
96 
103  virtual void setValue(const std::string &key,
104  const std::string &value);
105 
106  void deleteKey(const std::string &key);
107 
114  std::string getValue(const std::string &key,
115  const std::string &deflt) const A_WARN_UNUSED;
116 
117  int getValue(const std::string &key,
118  const int deflt) const A_WARN_UNUSED;
119 
120  int getValueInt(const std::string &key,
121  const int deflt) const A_WARN_UNUSED;
122 
123  bool getValueBool(const std::string &key,
124  const bool deflt) const A_WARN_UNUSED;
125 
126  unsigned getValue(const std::string &key,
127  const unsigned deflt) const A_WARN_UNUSED;
128 
129  double getValue(const std::string &key,
130  const double deflt) const A_WARN_UNUSED;
131 
135  void clear();
136 
145  template <class IT, class T, class CONT>
146  void setList(const std::string &name, IT begin, IT end,
148  {
149  if (!manager)
150  return;
151 
153  deleteList(name);
154  ConfigurationList *list = &(mContainerOptions[name]);
155 
156  for (IT it = begin; it != end; it++)
157  {
158  ConfigurationObject *const wrobj
159  = manager->writeConfigItem(*it, nextobj);
160  if (wrobj)
161  { // wrote something
162  nextobj = new ConfigurationObject;
163  list->push_back(wrobj);
164  }
165  else
166  {
167  nextobj->clear(); // you never know...
168  }
169  }
170 
171  delete nextobj;
172  }
173 
181  template<class T, class CONT>
182  CONT getList(const std::string &name, CONT empty,
184  {
185  ConfigurationList *const list = &(mContainerOptions[name]);
186  CONT container = empty;
187 
188  if (!manager)
189  return container;
190 
191  for (ConfigurationList::const_iterator it = list->begin();
192  it != list->end();
193  ++ it)
194  {
195  container = manager->readConfigItem(*it, container);
196  }
197 
198  return container;
199  }
200 
201 #ifdef DEBUG_CONFIG
202  void enableKeyLogging()
203  { mLogKeys = true; }
204 
205  void setIsMain(bool b)
206  { mIsMain = b; }
207 #endif // DEBUG_CONFIG
208 
209  protected:
211 
212  virtual void initFromXML(XmlNodeConstPtrConst parentNode);
213  virtual void writeToXML(XmlTextWriterPtr writer);
214 
215  void deleteList(const std::string &name);
216 
217  typedef std::map<std::string, std::string> Options;
219 
220  typedef std::list<ConfigurationObject *> ConfigurationList;
221  std::map<std::string, ConfigurationList> mContainerOptions;
222 
223 #ifdef DEBUG_CONFIG
224  bool mLogKeys;
225  bool mIsMain;
226 #endif // DEBUG_CONFIG
227 };
228 
229 #define valTest(num) mStatsRe##num
230 
237 {
238  public:
239  Configuration();
240 
242 
243  ~Configuration() override final;
244 
251  void init(const std::string &filename,
252  const UseVirtFs useResManager,
253  const SkipError skipError);
254 
255  void reInit();
256 
257  void unload();
258 
260  { return mDefaultsData; }
261 
265  void write();
266 
270  void addListener(const std::string &key,
271  ConfigListener *const listener);
272 
277  void removeListener(const std::string &key,
278  ConfigListener *const listener);
279 
281 
282 #ifdef ENABLE_CHECKS
283  void checkListeners(ConfigListener *const listener,
284  const char *const file,
285  const unsigned line);
286 #endif // ENABLE_CHECKS
287 
288  void setValue(const std::string &key,
289  const std::string &value) override;
290 
291  void incValue(const std::string &key);
292 
293  void setSilent(const std::string &key,
294  const std::string &value);
295 
296  inline void setValue(const std::string &key,
297  const char *const value)
298  { if (value != nullptr) setValue(key, std::string(value)); }
299 
300  inline void setSilent(const std::string &key,
301  const char *const value)
302  { if (value != nullptr) setSilent(key, std::string(value)); }
303 
304  inline void setValue(const std::string &key,
305  const float value)
306  { setValue(key, toString(value)); }
307 
308  inline void setValue(const std::string &key,
309  const double value)
310  { setValue(key, toString(value)); }
311 
312  inline void setValue(const std::string &key,
313  const int value)
314  { setValue(key, toString(value)); }
315 
316  inline void setValueInt(const std::string &key,
317  const int value)
318  { setValue(key, toString(value)); }
319 
320  inline void setValue(const std::string &key,
321  const unsigned value)
322  { setValue(key, toString(value)); }
323 
324  inline void setValue(const std::string &key,
325  const bool value)
326  { setValue(key, value ? "1" : "0"); }
327 
328  inline void setSilent(const std::string &key,
329  const bool value)
330  { setSilent(key, value ? "1" : "0"); }
331 
332  int resetIntValue(const std::string &key);
333 
334  bool resetBoolValue(const std::string &key);
335 
336  const std::string &getConfigPath() const noexcept2 A_WARN_UNUSED
337  { return mConfigPath; }
338 
344  int getIntValue(const std::string &key) const A_WARN_UNUSED_NON_TESTS;
345 
346  float getFloatValue(const std::string &key) const
348 
349  std::string getStringValue(const std::string &key) const
351 
352  bool getBoolValue(const std::string &key) const
354 
356  { return mDirectory; }
357 
358  void removeOldKeys();
359 
360  std::string getFileName() const noexcept2 A_WARN_UNUSED
361  { return mFilename; }
362 
363  void writeUpdated();
364 
368  void cleanDefaults();
369 
370  private:
371  typedef std::list<ConfigListener*> Listeners;
372  typedef Listeners::iterator ListenerIterator;
373  typedef std::map<std::string, Listeners> ListenerMap;
374  typedef ListenerMap::iterator ListenerMapIterator;
376 
377  // Location of config file
378  std::string mConfigPath;
381  std::string mDirectory;
382  std::string mFilename;
384  bool mUpdated;
385 };
386 
387 extern Configuration branding;
388 extern Configuration config;
390 extern Configuration paths;
391 extern Configuration features;
392 
393 #endif // CONFIGURATION_H
virtual CONT readConfigItem(const ConfigurationObject *const obj, CONT container) const =0
virtual ConfigurationObject * writeConfigItem(const T &value, ConfigurationObject *const obj) const =0
virtual void initFromXML(const xmlNode *const parentNode)
int getValueInt(const std::string &key, const int deflt) const
std::string getValue(const std::string &key, const std::string &deflt) const
virtual ~ConfigurationObject()
bool getValueBool(const std::string &key, const bool deflt) const
void deleteList(const std::string &name)
void setList(const std::string &name, IT begin, IT end, ConfigurationListManager< T, CONT > *manager)
std::map< std::string, std::string > Options
virtual void writeToXML(const xmlTextWriterPtr writer)
std::list< ConfigurationObject * > ConfigurationList
void deleteKey(const std::string &key)
virtual void setValue(const std::string &key, const std::string &value)
std::map< std::string, ConfigurationList > mContainerOptions
CONT getList(const std::string &name, CONT empty, ConfigurationListManager< T, CONT > *manager)
bool getBoolValue(const std::string &key) const
std::string getStringValue(const std::string &key) const
float getFloatValue(const std::string &key) const
void addListener(const std::string &key, ConfigListener *const listener)
const std::string & getConfigPath() const
std::string mDirectory
std::string mFilename
void setSilent(const std::string &key, const bool value)
std::map< std::string, Listeners > ListenerMap
void setValue(const std::string &key, const char *const value)
std::string mConfigPath
void setValue(const std::string &key, const std::string &value)
bool resetBoolValue(const std::string &key)
DefaultsData & getDefaultValues()
void incValue(const std::string &key)
void setValue(const std::string &key, const unsigned value)
Listeners::iterator ListenerIterator
std::string getFileName() const
UseVirtFs mUseResManager
ListenerMap::iterator ListenerMapIterator
void removeListener(const std::string &key, ConfigListener *const listener)
void setValueInt(const std::string &key, const int value)
int resetIntValue(const std::string &key)
std::list< ConfigListener * > Listeners
ListenerMap mListenerMap
void setSilent(const std::string &key, const std::string &value)
void setValue(const std::string &key, const bool value)
void setValue(const std::string &key, const int value)
void setSilent(const std::string &key, const char *const value)
void setValue(const std::string &key, const float value)
void removeListeners(ConfigListener *const listener)
void init(const std::string &filename, const UseVirtFs useResManager, const SkipError skipError)
DefaultsData mDefaultsData
Defaults of value for a given key.
std::string getDirectory() const
int getIntValue(const std::string &key) const
void setValue(const std::string &key, const double value)
Configuration config
Configuration features
Configuration paths
Configuration serverConfig
Configuration branding
std::map< std::string, VariableData * > DefaultsData
Definition: defaults.h:32
#define A_WARN_UNUSED
Definition: localconsts.h:161
#define notfinal
Definition: localconsts.h:261
#define noexcept2
Definition: localconsts.h:50
#define final
Definition: localconsts.h:46
#define A_DELETE_COPY(func)
Definition: localconsts.h:53
#define A_WARN_UNUSED_NON_TESTS
Definition: localconsts.h:162
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
std::string empty
Definition: podict.cpp:26
bool SkipError
Definition: skiperror.h:30
bool UseVirtFs
Definition: usevirtfs.h:30