3 /////////////////////////////////////////////////////////////////////////////
5 // Copyright (c) 2002 Iain Murray
7 /////////////////////////////////////////////////////////////////////////////
9 #include "../Common/Common.h"
11 #include "SettingsStore.h"
13 #include "EventHandler.h"
19 // Track memory leaks on Windows to the line that new'd the memory
22 #define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
25 static char THIS_FILE
[] = __FILE__
;
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
) {
43 if(s_oParamTables
.BoolParamTable
[i
].persistent
)
44 if(LoadSetting(s_oParamTables
.BoolParamTable
[i
].regName
, &bValue
))
45 s_oParamTables
.BoolParamTable
[i
].value
= bValue
;
47 SaveSetting(s_oParamTables
.BoolParamTable
[i
].regName
, s_oParamTables
.BoolParamTable
[i
].value
);
50 for(int j(0); j
< NUM_OF_LPS
; ++j
) {
52 if(s_oParamTables
.LongParamTable
[j
].persistent
)
53 if(LoadSetting(s_oParamTables
.LongParamTable
[j
].regName
, &lValue
))
54 s_oParamTables
.LongParamTable
[j
].value
= lValue
;
56 SaveSetting(s_oParamTables
.LongParamTable
[j
].regName
, s_oParamTables
.LongParamTable
[j
].value
);
59 for(int k(0); k
< NUM_OF_SPS
; ++k
) {
61 if(s_oParamTables
.StringParamTable
[k
].persistent
)
62 if(LoadSetting(s_oParamTables
.StringParamTable
[k
].regName
, &strValue
))
63 s_oParamTables
.StringParamTable
[k
].value
= strValue
;
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
))
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
);
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
))
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
))
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
);
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
);
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
);
154 return s_oParamTables
.StringParamTable
[iParameter
- FIRST_SP
].value
;
157 void CSettingsStore::ResetParameter(int iParameter
) {
158 switch(GetParameterType(iParameter
)) {
160 SetBoolParameter(iParameter
, boolparamtable
[iParameter
-FIRST_BP
].defaultValue
);
163 SetLongParameter(iParameter
, longparamtable
[iParameter
-FIRST_LP
].defaultValue
);
166 SetStringParameter(iParameter
, stringparamtable
[iParameter
-FIRST_SP
].defaultValue
);
169 // TODO: Error handling?
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
))
179 if ((iParameter
>= FIRST_LP
) && (iParameter
< FIRST_SP
))
181 if ((iParameter
>= FIRST_SP
) && (iParameter
< END_OF_SPS
))
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
))
195 DASHER_ASSERT(iParameter
== s_oParamTables
.BoolParamTable
[iParameter
- FIRST_BP
].key
);
196 return s_oParamTables
.BoolParamTable
[iParameter
- FIRST_BP
].regName
;
201 DASHER_ASSERT(iParameter
== s_oParamTables
.LongParamTable
[iParameter
- FIRST_LP
].key
);
202 return s_oParamTables
.LongParamTable
[iParameter
- FIRST_LP
].regName
;
207 DASHER_ASSERT(iParameter
== s_oParamTables
.StringParamTable
[iParameter
- FIRST_SP
].key
);
208 return s_oParamTables
.StringParamTable
[iParameter
- FIRST_SP
].regName
;
212 // TODO: Error handling?
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
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
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
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
);
271 // This means the key passed in a string was not found in the new table
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
);
284 // This means the key passed in a string was not found in the new table
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
);
297 // This means the key passed in a string was not found in the new table
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
) {
309 bool CSettingsStore::LoadSetting(const std::string
&Key
, long *Value
) {
313 bool CSettingsStore::LoadSetting(const std::string
&Key
, std::string
*Value
) {
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
) {