xoreos  0.0.5
configman.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos 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 xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 // Inspired by ScummVM's config file and manager code
22 
27 #ifndef COMMON_CONFIGMAN_H
28 #define COMMON_CONFIGMAN_H
29 
30 #include "src/common/types.h"
31 #include "src/common/error.h"
32 #include "src/common/scopedptr.h"
33 #include "src/common/singleton.h"
34 #include "src/common/ustring.h"
35 
36 namespace Common {
37 
38 class ConfigFile;
39 class ConfigDomain;
40 
41 class SeekableReadStream;
42 class WriteStream;
43 
48 };
49 
51 class ConfigManager : public Singleton<ConfigManager> {
52 public:
53  ConfigManager();
55 
57  UString getConfigFile() const;
58 
60  void setConfigFile(const UString &file = "");
61 
63  void clear();
65  void clearCommandline();
66 
68  bool fileExists() const;
69 
71  bool changed() const;
72 
74  bool load();
76  void load(SeekableReadStream &stream);
77 
79  bool save();
81  void save(WriteStream &stream, bool clearChanged = false);
82 
84  void create();
85 
87  UString findGame(const UString &path);
89  UString createGame(const UString &path, UString target = "");
90 
92  bool hasGame(const UString &gameID);
93 
95  bool setGame(const UString &gameID = "");
96 
98  bool isInGame() const;
99 
100  bool hasKey(const UString &key) const;
101  bool getKey(const UString &key, UString &value) const;
102 
103  // Specialized getters
104  UString getString(const UString &key) const;
105  bool getBool (const UString &key) const;
106  int getInt (const UString &key) const;
107  double getDouble(const UString &key) const;
108 
109  UString getString(const UString &key, const UString &def) const;
110  bool getBool (const UString &key, bool def) const;
111  int getInt (const UString &key, int def) const;
112  double getDouble(const UString &key, double def) const;
113 
114  void setKey(const UString &key, const UString &value, bool update = false);
115 
116  // Specialized setters
117  void setString(const UString &key, const UString &value, bool update = false);
118  void setBool (const UString &key, bool value, bool update = false);
119  void setInt (const UString &key, int value, bool update = false);
120  void setDouble(const UString &key, double value, bool update = false);
121 
123  void setKey(ConfigRealm realm, const UString &key, const UString &value);
124 
125  // Specialized realm setters
126  void setString(ConfigRealm realm, const UString &key, const UString &value);
127  void setBool (ConfigRealm realm, const UString &key, bool value);
128  void setInt (ConfigRealm realm, const UString &key, int value);
129  void setDouble(ConfigRealm realm, const UString &key, double value);
130 
132  void setDefaults();
133 
134  bool hasDefaultKey(const UString &key) const;
135  UString getDefaultKey(const UString &key) const;
136 
137  // Specialized defaults getters
138  UString getDefaultString(const UString &key) const;
139  bool getDefaultBool (const UString &key) const;
140  int getDefaultInt (const UString &key) const;
141  double getDefaultDouble(const UString &key) const;
142 
144  void setCommandlineKey(const UString &key, const UString &value);
145 
146 private:
147  static const char *kDomainApp;
148 
150 
151  bool _changed;
152 
154 
159 
162 
163  static UString getDefaultConfigFile();
164 
165  UString createGameID(const UString &path);
166 
167  // Helpers
168  bool hasKey(const ConfigDomain *domain, const UString &key) const;
169  bool getKey(const ConfigDomain *domain, const UString &key, UString &value) const;
170  bool setKey(ConfigDomain *domain, const UString &key, const UString &value);
171 };
172 
173 } // End of namespace Common
174 
176 #define ConfigMan Common::ConfigManager::instance()
177 
178 #endif // COMMON_CONFIGMAN_H
Class and macro for implementing singletons.
void setDefaults()
Overwrite the current config with the defaults.
Definition: configman.cpp:456
Definition: 2dafile.h:39
bool hasKey(const UString &key) const
Definition: configman.cpp:273
void setString(const UString &key, const UString &value, bool update=false)
Definition: configman.cpp:406
A class holding an UTF-8 string.
Definition: ustring.h:48
Temporary game settings/properties.
Definition: configman.h:47
ScopedPtr< ConfigDomain > _domainDefaultGame
Game defaults domain.
Definition: configman.h:156
UString createGame(const UString &path, UString target="")
Create the game domain with this path and target.
Definition: configman.cpp:217
bool load()
Load from the default config file.
Definition: configman.cpp:79
bool fileExists() const
Does the config file exist?
Definition: configman.cpp:71
bool setGame(const UString &gameID="")
Set the game domain to gameID.
Definition: configman.cpp:241
int getDefaultInt(const UString &key) const
Definition: configman.cpp:495
void clear()
Clear everything except the command line options.
Definition: configman.cpp:54
bool getKey(const UString &key, UString &value) const
Definition: configman.cpp:280
UString getDefaultKey(const UString &key) const
Definition: configman.cpp:473
double getDefaultDouble(const UString &key) const
Definition: configman.cpp:502
ScopedPtr< ConfigDomain > _domainDefaultApp
Application defaults domain.
Definition: configman.h:155
The global config manager, storing all config keys.
Definition: configman.h:51
void setConfigFile(const UString &file="")
Set the config file to use.
Definition: configman.cpp:50
void setBool(const UString &key, bool value, bool update=false)
Definition: configman.cpp:410
UString createGameID(const UString &path)
Definition: configman.cpp:187
A simple scoped smart pointer template.
Basic exceptions to throw.
Generic template base class for implementing the singleton design pattern.
Definition: singleton.h:61
ConfigDomain * _domainApp
Application domain, pointer into the config file.
Definition: configman.h:160
static const char * kDomainApp
The name of the application domain.
Definition: configman.h:147
UString _configFile
The config file to use.
Definition: configman.h:149
bool getDefaultBool(const UString &key) const
Definition: configman.cpp:488
ConfigRealm
Special config realms.
Definition: configman.h:45
ConfigDomain * _domainGame
Game domain, pointer into the config file.
Definition: configman.h:161
Low-level type definitions to handle fixed width types portably.
void create()
Create a new, empty config.
Definition: configman.cpp:153
A scoped plain pointer, allowing pointer-y access and normal deletion.
Definition: scopedptr.h:120
ScopedPtr< ConfigDomain > _domainCommandline
Command line domain.
Definition: configman.h:157
UString getDefaultString(const UString &key) const
Definition: configman.cpp:484
Generic interface for a writable data stream.
Definition: writestream.h:64
Unicode string handling.
UString findGame(const UString &path)
Find the game domain using this path.
Definition: configman.cpp:161
bool changed() const
Was at least on setting changed?
Definition: configman.cpp:75
ScopedPtr< ConfigFile > _config
The actual config.
Definition: configman.h:153
Application or game defaults.
Definition: configman.h:46
void clearCommandline()
Clear the command line options.
Definition: configman.cpp:67
void setKey(const UString &key, const UString &value, bool update=false)
Definition: configman.cpp:384
Accessor for a domain (section) in a config file.
Definition: configfile.h:46
bool hasDefaultKey(const UString &key) const
Definition: configman.cpp:468
bool getBool(const UString &key) const
Definition: configman.cpp:298
UString getConfigFile() const
Return the config file that&#39;s currently in use.
Definition: configman.cpp:513
static UString getDefaultConfigFile()
Definition: configman.cpp:520
void setDouble(const UString &key, double value, bool update=false)
Definition: configman.cpp:418
double getDouble(const UString &key) const
Definition: configman.cpp:320
bool save()
Save to the default config file.
Definition: configman.cpp:114
ScopedPtr< ConfigDomain > _domainGameTemp
Temporary game settings domain.
Definition: configman.h:158
bool isInGame() const
Are we currently in a game?
Definition: configman.cpp:269
UString getString(const UString &key) const
Definition: configman.cpp:290
bool hasGame(const UString &gameID)
Does the specified game domain exist?
Definition: configman.cpp:234
Interface for a seekable & readable data stream.
Definition: readstream.h:265
void setCommandlineKey(const UString &key, const UString &value)
Set a config value that came from the command line.
Definition: configman.cpp:509
int getInt(const UString &key) const
Definition: configman.cpp:309
void setInt(const UString &key, int value, bool update=false)
Definition: configman.cpp:414