tagging release
[dasher.git] / Src / DasherCore / SettingsStore.cpp
blob234c7caf37211f6c3b84f76cc06ecf5dfbdc64b9
1 // SettingsStore.cpp
2 //
3 /////////////////////////////////////////////////////////////////////////////
4 //
5 // Copyright (c) 2002 Iain Murray
6 //
7 /////////////////////////////////////////////////////////////////////////////
9 #include "../Common/Common.h"
11 #include "SettingsStore.h"
12 #include "Event.h"
13 #include "EventHandler.h"
15 #include <iostream>
17 using namespace std;
19 // Track memory leaks on Windows to the line that new'd the memory
20 #ifdef _WIN32
21 #ifdef _DEBUG
22 #define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
23 #define new DEBUG_NEW
24 #undef THIS_FILE
25 static char THIS_FILE[] = __FILE__;
26 #endif
27 #endif
29 Dasher::CParamTables CSettingsStore::s_oParamTables;
31 // TODO: Don't propagate changes which don't affect anything.
33 CSettingsStore::CSettingsStore(Dasher::CEventHandler *pEventHandler):m_pEventHandler(pEventHandler) {
36 void CSettingsStore::LoadPersistent() {
38 // Load each of the persistent parameters. If we fail loading for the store, then
39 // we'll save the settings with the default value that comes from Parameters.h
41 for(int i(0); i < NUM_OF_BPS; ++i) {
42 bool bValue;
43 if(s_oParamTables.BoolParamTable[i].persistent)
44 if(LoadSetting(s_oParamTables.BoolParamTable[i].regName, &bValue))
45 s_oParamTables.BoolParamTable[i].value = bValue;
46 else
47 SaveSetting(s_oParamTables.BoolParamTable[i].regName, s_oParamTables.BoolParamTable[i].value);
50 for(int j(0); j < NUM_OF_LPS; ++j) {
51 long lValue;
52 if(s_oParamTables.LongParamTable[j].persistent)
53 if(LoadSetting(s_oParamTables.LongParamTable[j].regName, &lValue))
54 s_oParamTables.LongParamTable[j].value = lValue;
55 else
56 SaveSetting(s_oParamTables.LongParamTable[j].regName, s_oParamTables.LongParamTable[j].value);
59 for(int k(0); k < NUM_OF_SPS; ++k) {
60 std::string strValue;
61 if(s_oParamTables.StringParamTable[k].persistent)
62 if(LoadSetting(s_oParamTables.StringParamTable[k].regName, &strValue))
63 s_oParamTables.StringParamTable[k].value = strValue;
64 else
65 SaveSetting(s_oParamTables.StringParamTable[k].regName, s_oParamTables.StringParamTable[k].value);
69 /* TODO: Consider using Template functions to make this neater. */
71 void CSettingsStore::SetBoolParameter(int iParameter, bool bValue) {
73 // Check that the parameter is in fact in the right spot in the table
74 DASHER_ASSERT(iParameter == s_oParamTables.BoolParamTable[iParameter - FIRST_BP].key);
76 if(bValue == GetBoolParameter(iParameter))
77 return;
79 // Set the value
80 s_oParamTables.BoolParamTable[iParameter - FIRST_BP].value = bValue;
82 // Initiate events for changed parameter
83 Dasher::CParameterNotificationEvent* oEvent = new Dasher::CParameterNotificationEvent(iParameter);
85 m_pEventHandler->InsertEvent(oEvent);
86 delete oEvent;
88 // Write out to permanent storage
89 if(s_oParamTables.BoolParamTable[iParameter - FIRST_BP].persistent)
90 SaveSetting(s_oParamTables.BoolParamTable[iParameter - FIRST_BP].regName, bValue);
93 void CSettingsStore::SetLongParameter(int iParameter, long lValue) {
95 // Check that the parameter is in fact in the right spot in the table
96 DASHER_ASSERT(iParameter == s_oParamTables.LongParamTable[iParameter - FIRST_LP].key);
98 if(lValue == GetLongParameter(iParameter))
99 return;
101 // Set the value
102 s_oParamTables.LongParamTable[iParameter - FIRST_LP].value = lValue;
104 // Initiate events for changed parameter
105 Dasher::CParameterNotificationEvent oEvent(iParameter);
106 m_pEventHandler->InsertEvent(&oEvent);
108 // Write out to permanent storage
109 if(s_oParamTables.LongParamTable[iParameter - FIRST_LP].persistent)
110 SaveSetting(s_oParamTables.LongParamTable[iParameter - FIRST_LP].regName, lValue);
113 void CSettingsStore::SetStringParameter(int iParameter, const std::string sValue) {
115 // Check that the parameter is in fact in the right spot in the table
116 DASHER_ASSERT(iParameter == s_oParamTables.StringParamTable[iParameter - FIRST_SP].key);
118 if(sValue == GetStringParameter(iParameter))
119 return;
121 // Set the value
122 s_oParamTables.StringParamTable[iParameter - FIRST_SP].value = sValue;
124 // Initiate events for changed parameter
125 Dasher::CParameterNotificationEvent oEvent(iParameter);
126 m_pEventHandler->InsertEvent(&oEvent);
128 // Write out to permanent storage
129 if(s_oParamTables.StringParamTable[iParameter - FIRST_SP].persistent)
130 SaveSetting(s_oParamTables.StringParamTable[iParameter - FIRST_SP].regName, sValue);
133 bool CSettingsStore::GetBoolParameter(int iParameter) {
134 // Check that the parameter is in fact in the right spot in the table
135 DASHER_ASSERT(iParameter == s_oParamTables.BoolParamTable[iParameter - FIRST_BP].key);
137 // Return the value
138 return s_oParamTables.BoolParamTable[iParameter - FIRST_BP].value;
141 long CSettingsStore::GetLongParameter(int iParameter) {
142 // Check that the parameter is in fact in the right spot in the table
143 DASHER_ASSERT(iParameter == s_oParamTables.LongParamTable[iParameter - FIRST_LP].key);
145 // Return the value
146 return s_oParamTables.LongParamTable[iParameter - FIRST_LP].value;
149 std::string CSettingsStore::GetStringParameter(int iParameter) {
150 // Check that the parameter is in fact in the right spot in the table
151 DASHER_ASSERT(iParameter == s_oParamTables.StringParamTable[iParameter - FIRST_SP].key);
153 // Return the value
154 return s_oParamTables.StringParamTable[iParameter - FIRST_SP].value;
157 void CSettingsStore::ResetParameter(int iParameter) {
158 switch(GetParameterType(iParameter)) {
159 case ParamBool:
160 SetBoolParameter(iParameter, boolparamtable[iParameter-FIRST_BP].defaultValue);
161 break;
162 case ParamLong:
163 SetLongParameter(iParameter, longparamtable[iParameter-FIRST_LP].defaultValue);
164 break;
165 case ParamString:
166 SetStringParameter(iParameter, stringparamtable[iParameter-FIRST_SP].defaultValue);
167 break;
168 case ParamInvalid:
169 // TODO: Error handling?
170 break;
174 // Used to determine what data type a given parameter ID is.
175 ParameterType CSettingsStore::GetParameterType(int iParameter)
177 if ((iParameter >= FIRST_BP) && (iParameter < FIRST_LP))
178 return ParamBool;
179 if ((iParameter >= FIRST_LP) && (iParameter < FIRST_SP))
180 return ParamLong;
181 if ((iParameter >= FIRST_SP) && (iParameter < END_OF_SPS))
182 return ParamString;
184 return ParamInvalid;
187 // Gets the string name for the given parameter
188 std::string CSettingsStore::GetParameterName(int iParameter)
190 // Pull the registry name out of the correct table depending on the parameter type
191 switch (GetParameterType(iParameter))
193 case (ParamBool):
195 DASHER_ASSERT(iParameter == s_oParamTables.BoolParamTable[iParameter - FIRST_BP].key);
196 return s_oParamTables.BoolParamTable[iParameter - FIRST_BP].regName;
197 break;
199 case (ParamLong):
201 DASHER_ASSERT(iParameter == s_oParamTables.LongParamTable[iParameter - FIRST_LP].key);
202 return s_oParamTables.LongParamTable[iParameter - FIRST_LP].regName;
203 break;
205 case (ParamString):
207 DASHER_ASSERT(iParameter == s_oParamTables.StringParamTable[iParameter - FIRST_SP].key);
208 return s_oParamTables.StringParamTable[iParameter - FIRST_SP].regName;
209 break;
211 case ParamInvalid:
212 // TODO: Error handling?
213 break;
217 return "";
221 ////////////////////////////////////////////////////////////
222 //// DEPRECATED FUNCTIONS BELOW
224 bool CSettingsStore::GetBoolOption(const std::string &Key) {
225 // Also make the call to the newer implementation
226 for(int ii = 0; ii < NUM_OF_BPS; ii++) {
227 if(s_oParamTables.BoolParamTable[ii].regName == Key) {
228 return GetBoolParameter(s_oParamTables.BoolParamTable[ii].key);
232 // This means the key passed in a string was not found in the new table
233 DASHER_ASSERT(0);
234 return false;
237 long CSettingsStore::GetLongOption(const std::string &Key) {
238 for(int ii = 0; ii < NUM_OF_LPS; ii++) {
239 if(s_oParamTables.LongParamTable[ii].regName == Key) {
240 return GetLongParameter(s_oParamTables.LongParamTable[ii].key);
243 // This means the key passed in a string was not found in the new table
244 DASHER_ASSERT(0);
245 return false;
248 std::string CSettingsStore::GetStringOption(const std::string &Key) {
249 // Also make the call to the newer implementation
250 for(int ii = 0; ii < NUM_OF_SPS; ii++) {
251 if(s_oParamTables.StringParamTable[ii].regName == Key) {
253 return GetStringParameter(s_oParamTables.StringParamTable[ii].key);
257 // This means the key passed in a string was not found in the new table
258 DASHER_ASSERT(0);
259 return false;
262 void CSettingsStore::SetBoolOption(const std::string &Key, bool Value) {
263 // Also make the call to the newer implementation
264 for(int ii = 0; ii < NUM_OF_BPS; ii++) {
265 if(s_oParamTables.BoolParamTable[ii].regName == Key) {
266 SetBoolParameter(s_oParamTables.BoolParamTable[ii].key, Value);
267 return;
271 // This means the key passed in a string was not found in the new table
272 DASHER_ASSERT(0);
275 void CSettingsStore::SetLongOption(const std::string &Key, long Value) {
276 // Also make the call to the newer implementation
277 for(int ii = 0; ii < NUM_OF_LPS; ii++) {
278 if(s_oParamTables.LongParamTable[ii].regName == Key) {
279 SetLongParameter(s_oParamTables.LongParamTable[ii].key, Value);
280 return;
284 // This means the key passed in a string was not found in the new table
285 DASHER_ASSERT(0);
288 void CSettingsStore::SetStringOption(const std::string &Key, const std::string &Value) {
289 // Also make the call to the newer implementation
290 for(int ii = 0; ii < NUM_OF_SPS; ii++) {
291 if(s_oParamTables.StringParamTable[ii].regName == Key) {
292 SetStringParameter(s_oParamTables.StringParamTable[ii].key, Value);
293 return;
297 // This means the key passed in a string was not found in the new table
298 DASHER_ASSERT(0);
301 /* Private functions -- Settings are not saved between sessions unless these
302 functions are over-ridden.
303 --------------------------------------------------------------------------*/
305 bool CSettingsStore::LoadSetting(const std::string &Key, bool *Value) {
306 return false;
309 bool CSettingsStore::LoadSetting(const std::string &Key, long *Value) {
310 return false;
313 bool CSettingsStore::LoadSetting(const std::string &Key, std::string *Value) {
314 return false;
317 void CSettingsStore::SaveSetting(const std::string &Key, bool Value) {
320 void CSettingsStore::SaveSetting(const std::string &Key, long Value) {
323 void CSettingsStore::SaveSetting(const std::string &Key, const std::string &Value) {