1 /* Copyright (C) 2015 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
19 CConfigDB - Load, access and store configuration variables
21 TDD : http://www.wildfiregames.com/forum/index.php?showtopic=1125
24 JavaScript: Check this documentation: http://trac.wildfiregames.com/wiki/Exposed_ConfigDB_Functions
27 #ifndef INCLUDED_CONFIGDB
28 #define INCLUDED_CONFIGDB
30 #include "lib/file/vfs/vfs_path.h"
32 #include "ps/Singleton.h"
34 // Namespace priorities: User supersedes mod supersedes system.
35 // Command-line arguments override everything.
47 typedef std::vector
<CStr
> CConfigValueSet
;
49 #define g_ConfigDB CConfigDB::GetSingleton()
51 class CConfigDB
: public Singleton
<CConfigDB
>
53 static std::map
<CStr
, CConfigValueSet
> m_Map
[];
54 static VfsPath m_ConfigFile
[];
55 static bool m_HasChanges
[];
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
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
);
76 * Returns true if changed with respect to last write on file
78 bool HasChanges(EConfigNamespace ns
) const;
80 void SetChanges(EConfigNamespace ns
, bool value
);
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
87 void GetValues(EConfigNamespace ns
, const CStr
& name
, CConfigValueSet
& values
) const;
90 * Returns the namespace that the value returned by GetValues was defined in,
91 * or CFG_LAST if it wasn't defined at all.
93 EConfigNamespace
GetValueNamespace(EConfigNamespace ns
, const CStr
& name
) const;
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.
100 std::map
<CStr
, CConfigValueSet
> GetValuesWithPrefix(EConfigNamespace ns
, const CStr
& prefix
) const;
103 * Save a config value in the specified namespace. If the config variable
104 * existed the value is replaced.
106 void SetValueString(EConfigNamespace ns
, const CStr
& name
, const CStr
& value
);
108 void SetValueBool(EConfigNamespace ns
, const CStr
& name
, const bool value
);
111 * Remove a config value in the specified namespace.
113 void RemoveValue(EConfigNamespace ns
, const CStr
& name
);
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.
120 * 'path': The path to the config file.
122 void SetConfigFile(EConfigNamespace ns
, const VfsPath
& path
);
125 * Reload the config file associated with the specified config namespace
126 * (the last config file path set with SetConfigFile)
129 * true: if the reload succeeded,
130 * false: if the reload failed
132 bool Reload(EConfigNamespace
);
135 * Write the current state of the specified config namespace to the file
136 * specified by 'path'
139 * true: if the config namespace was successfully written to the file
140 * false: if an error occurred
142 bool WriteFile(EConfigNamespace ns
, const VfsPath
& path
) const;
145 * Write the current state of the specified config namespace to the file
146 * it was originally loaded from.
149 * true: if the config namespace was successfully written to the file
150 * false: if an error occurred
152 bool WriteFile(EConfigNamespace ns
) const;
155 * Write a config value to the file specified by 'path'
158 * true: if the config value was successfully saved and written to the file
159 * false: if an error occurred
161 bool WriteValueToFile(EConfigNamespace ns
, const CStr
& name
, const CStr
& value
, const VfsPath
& path
);
163 bool WriteValueToFile(EConfigNamespace ns
, const CStr
& name
, const CStr
& value
);
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)
172 #endif // INCLUDED_CONFIGDB