Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ConfigDB.h
Go to the documentation of this file.
1 /* Copyright (C) 2013 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 "Parser.h"
31 #include "CStr.h"
32 #include "Singleton.h"
33 
34 #include "lib/file/vfs/vfs_path.h"
35 
36 // Namespace priorities: User supersedes mod supersedes system.
37 // Command-line arguments override everything.
38 
40 {
47 };
48 
50 typedef std::vector<CParserValue> CConfigValueSet;
51 
52 #define g_ConfigDB CConfigDB::GetSingleton()
53 
54 class CConfigDB: public Singleton<CConfigDB>
55 {
56  static std::map <CStr, CConfigValueSet> m_Map[];
58 
59 public:
60  CConfigDB();
61 
62  /**
63  * Attempt to find a config variable with the given name; will search
64  * CFG_COMMAND first, and then all namespaces from the specified namespace
65  * down to system.
66  *
67  * Returns a pointer to the config value structure for the variable, or
68  * NULL if such a variable could not be found.
69  */
70  CConfigValue *GetValue(EConfigNamespace ns, const CStr& name);
71 
72  /**
73  * Attempt to retrieve a vector of values corresponding to the given setting;
74  * will search CFG_COMMAND first, and then all namespaces from the specified
75  * namespace down to system.
76  *
77  * Returns a pointer to the vector, or NULL if the setting could not be found.
78  */
79  CConfigValueSet *GetValues(EConfigNamespace ns, const CStr& name);
80 
81  /**
82  * Returns the namespace that the value returned by GetValues was defined in,
83  * or CFG_LAST if it wasn't defined at all.
84  */
86 
87  /**
88  * Retrieve a map of values corresponding to settings whose names begin
89  * with the given prefix;
90  * will search all namespaces from system up to the specified namespace.
91  */
92  std::map<CStr, CConfigValueSet> GetValuesWithPrefix(EConfigNamespace ns, const CStr& prefix);
93 
94  /**
95  * Create a new config value in the specified namespace. If such a
96  * variable already exists in this namespace, the old value is returned.
97  *
98  * Returns a pointer to the value of the newly created config variable, or
99  * that of the already existing config variable.
100  */
101  CConfigValue *CreateValue(EConfigNamespace ns, const CStr& name);
102 
103  /**
104  * Set the path to the config file used to populate the specified namespace
105  * Note that this function does not actually load the config file. Use
106  * the Reload() method if you want to read the config file at the same time.
107  *
108  * 'path': The path to the config file.
109  */
110  void SetConfigFile(EConfigNamespace ns, const VfsPath& path);
111 
112  /**
113  * Reload the config file associated with the specified config namespace
114  * (the last config file path set with SetConfigFile)
115  *
116  * Returns:
117  * true: if the reload succeeded,
118  * false: if the reload failed
119  */
120  bool Reload(EConfigNamespace);
121 
122  /**
123  * Write the current state of the specified config namespace to the file
124  * specified by 'path'
125  *
126  * Returns:
127  * true: if the config namespace was successfully written to the file
128  * false: if an error occurred
129  */
130  bool WriteFile(EConfigNamespace ns, const VfsPath& path);
131 
132  /**
133  * Write the current state of the specified config namespace to the file
134  * it was originally loaded from.
135  *
136  * Returns:
137  * true: if the config namespace was successfully written to the file
138  * false: if an error occurred
139  */
140  bool WriteFile(EConfigNamespace ns);
141 };
142 
143 
144 // stores the value of the given key into <destination>. this quasi-template
145 // convenience wrapper on top of CConfigValue::Get* simplifies user code and
146 // avoids "assignment within condition expression" warnings.
147 #define CFG_GET_VAL(name, type, destination)\
148 STMT(\
149  CConfigValue* val = g_ConfigDB.GetValue(CFG_USER, name);\
150  if(val)\
151  val->Get##type(destination);\
152 )
153 
154 
155 #endif
std::vector< CParserValue > CConfigValueSet
Definition: ConfigDB.h:50
std::map< CStr, CConfigValueSet > GetValuesWithPrefix(EConfigNamespace ns, const CStr &prefix)
Retrieve a map of values corresponding to settings whose names begin with the given prefix; will sear...
Definition: ConfigDB.cpp:89
static CStr prefix
Definition: DllLoader.cpp:47
static VfsPath m_ConfigFile[]
Definition: ConfigDB.h:57
EConfigNamespace GetValueNamespace(EConfigNamespace ns, const CStr &name)
Returns the namespace that the value returned by GetValues was defined in, or CFG_LAST if it wasn&#39;t d...
Definition: ConfigDB.cpp:67
Definition: path.h:75
bool Reload(EConfigNamespace)
Reload the config file associated with the specified config namespace (the last config file path set ...
Definition: ConfigDB.cpp:140
CParserValue CConfigValue
Definition: ConfigDB.h:49
bool WriteFile(EConfigNamespace ns, const VfsPath &path)
Write the current state of the specified config namespace to the file specified by &#39;path&#39;...
Definition: ConfigDB.cpp:236
CConfigValueSet * GetValues(EConfigNamespace ns, const CStr &name)
Attempt to retrieve a vector of values corresponding to the given setting; will search CFG_COMMAND fi...
Definition: ConfigDB.cpp:45
CConfigValue * GetValue(EConfigNamespace ns, const CStr &name)
Attempt to find a config variable with the given name; will search CFG_COMMAND first, and then all namespaces from the specified namespace down to system.
Definition: ConfigDB.cpp:37
static std::map< CStr, CConfigValueSet > m_Map[]
Definition: ConfigDB.h:56
EConfigNamespace
Definition: ConfigDB.h:39
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:129
CConfigValue * CreateValue(EConfigNamespace ns, const CStr &name)
Create a new config value in the specified namespace.
Definition: ConfigDB.cpp:113