Pyrogenesis  trunk
ConfigDB.h
Go to the documentation of this file.
1 /* Copyright (C) 2015 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  CConfigDB - Load, access and store configuration variables
20 
21  TDD : http://www.wildfiregames.com/forum/index.php?showtopic=1125
22  OVERVIEW:
23 
24  JavaScript: Check this documentation: http://trac.wildfiregames.com/wiki/Exposed_ConfigDB_Functions
25 */
26 
27 #ifndef INCLUDED_CONFIGDB
28 #define INCLUDED_CONFIGDB
29 
30 #include "lib/file/vfs/vfs_path.h"
31 #include "ps/CStr.h"
32 #include "ps/Singleton.h"
33 
34 // Namespace priorities: User supersedes mod supersedes system.
35 // Command-line arguments override everything.
36 
38 {
45 };
46 
47 typedef std::vector<CStr> CConfigValueSet;
48 
49 #define g_ConfigDB CConfigDB::GetSingleton()
50 
51 class CConfigDB: public Singleton<CConfigDB>
52 {
53  static std::map<CStr, CConfigValueSet> m_Map[];
55  static bool m_HasChanges[];
56 
57 public:
58  CConfigDB();
59 
60  /**
61  * Attempt to retrieve the value of a config variable with the given name;
62  * will search CFG_COMMAND first, and then all namespaces from the specified
63  * namespace down.
64  */
65  void GetValue(EConfigNamespace ns, const CStr& name, bool& value);
66  ///@copydoc CConfigDB::GetValue
67  void GetValue(EConfigNamespace ns, const CStr& name, int& value);
68  ///@copydoc CConfigDB::GetValue
69  void GetValue(EConfigNamespace ns, const CStr& name, float& value);
70  ///@copydoc CConfigDB::GetValue
71  void GetValue(EConfigNamespace ns, const CStr& name, double& value);
72  ///@copydoc CConfigDB::GetValue
73  void GetValue(EConfigNamespace ns, const CStr& name, std::string& value);
74 
75  /**
76  * Returns true if changed with respect to last write on file
77  */
78  bool HasChanges(EConfigNamespace ns) const;
79 
80  void SetChanges(EConfigNamespace ns, bool value);
81 
82  /**
83  * Attempt to retrieve a vector of values corresponding to the given setting;
84  * will search CFG_COMMAND first, and then all namespaces from the specified
85  * namespace down.
86  */
87  void GetValues(EConfigNamespace ns, const CStr& name, CConfigValueSet& values) const;
88 
89  /**
90  * Returns the namespace that the value returned by GetValues was defined in,
91  * or CFG_LAST if it wasn't defined at all.
92  */
93  EConfigNamespace GetValueNamespace(EConfigNamespace ns, const CStr& name) const;
94 
95  /**
96  * Retrieve a map of values corresponding to settings whose names begin
97  * with the given prefix;
98  * will search all namespaces from default up to the specified namespace.
99  */
100  std::map<CStr, CConfigValueSet> GetValuesWithPrefix(EConfigNamespace ns, const CStr& prefix) const;
101 
102  /**
103  * Save a config value in the specified namespace. If the config variable
104  * existed the value is replaced.
105  */
106  void SetValueString(EConfigNamespace ns, const CStr& name, const CStr& value);
107 
108  void SetValueBool(EConfigNamespace ns, const CStr& name, const bool value);
109 
110  /**
111  * Remove a config value in the specified namespace.
112  */
113  void RemoveValue(EConfigNamespace ns, const CStr& name);
114 
115  /**
116  * Set the path to the config file used to populate the specified namespace
117  * Note that this function does not actually load the config file. Use
118  * the Reload() method if you want to read the config file at the same time.
119  *
120  * 'path': The path to the config file.
121  */
122  void SetConfigFile(EConfigNamespace ns, const VfsPath& path);
123 
124  /**
125  * Reload the config file associated with the specified config namespace
126  * (the last config file path set with SetConfigFile)
127  *
128  * Returns:
129  * true: if the reload succeeded,
130  * false: if the reload failed
131  */
132  bool Reload(EConfigNamespace);
133 
134  /**
135  * Write the current state of the specified config namespace to the file
136  * specified by 'path'
137  *
138  * Returns:
139  * true: if the config namespace was successfully written to the file
140  * false: if an error occurred
141  */
142  bool WriteFile(EConfigNamespace ns, const VfsPath& path) const;
143 
144  /**
145  * Write the current state of the specified config namespace to the file
146  * it was originally loaded from.
147  *
148  * Returns:
149  * true: if the config namespace was successfully written to the file
150  * false: if an error occurred
151  */
152  bool WriteFile(EConfigNamespace ns) const;
153 
154  /**
155  * Write a config value to the file specified by 'path'
156  *
157  * Returns:
158  * true: if the config value was successfully saved and written to the file
159  * false: if an error occurred
160  */
161  bool WriteValueToFile(EConfigNamespace ns, const CStr& name, const CStr& value, const VfsPath& path);
162 
163  bool WriteValueToFile(EConfigNamespace ns, const CStr& name, const CStr& value);
164 };
165 
166 
167 // stores the value of the given key into <destination>. this quasi-template
168 // convenience wrapper on top of GetValue simplifies user code
169 #define CFG_GET_VAL(name, destination)\
170  g_ConfigDB.GetValue(CFG_USER, name, destination)
171 
172 #endif // INCLUDED_CONFIGDB
static CStr prefix
Definition: DllLoader.cpp:46
static VfsPath m_ConfigFile[]
Definition: ConfigDB.h:54
bool HasChanges(EConfigNamespace ns) const
Returns true if changed with respect to last write on file.
Definition: ConfigDB.cpp:118
Definition: ConfigDB.h:40
static bool m_HasChanges[]
Definition: ConfigDB.h:55
void GetValue(EConfigNamespace ns, const CStr &name, bool &value)
Attempt to retrieve the value of a config variable with the given name; will search CFG_COMMAND first...
Definition: ConfigDB.h:42
void SetValueString(EConfigNamespace ns, const CStr &name, const CStr &value)
Save a config value in the specified namespace.
Definition: ConfigDB.cpp:197
Definition: path.h:77
bool Reload(EConfigNamespace)
Reload the config file associated with the specified config namespace (the last config file path set ...
Definition: ConfigDB.cpp:234
void GetValues(EConfigNamespace ns, const CStr &name, CConfigValueSet &values) const
Attempt to retrieve a vector of values corresponding to the given setting; will search CFG_COMMAND fi...
Definition: ConfigDB.cpp:134
std::map< CStr, CConfigValueSet > GetValuesWithPrefix(EConfigNamespace ns, const CStr &prefix) const
Retrieve a map of values corresponding to settings whose names begin with the given prefix; will sear...
Definition: ConfigDB.cpp:176
Definition: ConfigDB.h:44
void RemoveValue(EConfigNamespace ns, const CStr &name)
Remove a config value in the specified namespace.
Definition: ConfigDB.cpp:215
EConfigNamespace GetValueNamespace(EConfigNamespace ns, const CStr &name) const
Returns the namespace that the value returned by GetValues was defined in, or CFG_LAST if it wasn&#39;t d...
Definition: ConfigDB.cpp:157
void SetChanges(EConfigNamespace ns, bool value)
Definition: ConfigDB.cpp:126
Definition: ConfigDB.h:39
bool WriteValueToFile(EConfigNamespace ns, const CStr &name, const CStr &value, const VfsPath &path)
Write a config value to the file specified by &#39;path&#39;.
Definition: ConfigDB.cpp:441
static std::map< CStr, CConfigValueSet > m_Map[]
Definition: ConfigDB.h:53
void SetValueBool(EConfigNamespace ns, const CStr &name, const bool value)
Definition: ConfigDB.cpp:209
EConfigNamespace
Definition: ConfigDB.h:37
Definition: Singleton.h:34
void SetConfigFile(EConfigNamespace ns, const VfsPath &path)
Set the path to the config file used to populate the specified namespace Note that this function does...
Definition: ConfigDB.cpp:226
Definition: ConfigDB.h:51
CConfigDB()
Definition: ConfigDB.cpp:36
Definition: ConfigDB.h:43
bool WriteFile(EConfigNamespace ns, const VfsPath &path) const
Write the current state of the specified config namespace to the file specified by &#39;path&#39;...
Definition: ConfigDB.cpp:405
Definition: ConfigDB.h:41
std::vector< CStr > CConfigValueSet
Definition: ConfigDB.h:47