So here's a chunk of code. Today I give you my Settings Manager:
.h
/* -------------------------------------------------------------- File: AXSettings.h Description: This file defines a class that manages the application's settings. It can parse settings from a file as well as save the current settings to a file. It keeps track of different types of settings so that you can group settings together. It comes with two types, User and System, which define a kind of higher/lower level relationship. However, you can easily define your own types. If you wanted to group setting for the terrain together, you could easily create a SETTING_TERRAIN type as long as its integer value is unique. When writing to a file, you must specify the file name as well as the type of setting that you want to write. This way you can write the user settings in the general file but write the terrain settings in its own secret file. It uses boost::any to store a generic setting and templates to call functions on each setting. For now it only supports the following types, but it can easily be extended: - Int - Float - String This object has not been profiled yet, and since access involves a lookup into a Map, frequent accesses may cause performance to slow. It is recommended to cache settings if possible. This object is not intended to be used during application run time anyway. You should extract the settings at startup and save them at shutdown; do not abuse it. Date: July 1, 2005 Author: James Long-------------------------------------------------------------- */#if !defined(AXSETTINGS_INCLUDE)#define AXSETTINGS_INCLUDEtypedef int SettingType;static const SettingType SETTING_SYSTEM = 1;static const SettingType SETTING_USER = 2;class AXSettings : public AXSingleton {public: AXSettings(); virtual ~AXSettings(); template<class T> void RegisterSetting(string name, T value, SettingType type) { _SettingStruct Temp_Setting; Temp_Setting.value = value; Temp_Setting.type = type; SettingsMap[name] = Temp_Setting; } template<class T> void ChangeSetting(string name, T value) { map::iterator it; if((it = SettingsMap.find(name)) != SettingsMap.end()) (*it).second.value = value; else { AXAppLog->Write(MESSAGE_SYS, "Warning: \"%s\" setting not found!", name.c_str()); return static_cast(-1); } } template<class T> T GetSetting(string name) { map::iterator it; try { if((it = SettingsMap.find(name)) != SettingsMap.end()) return any_cast((*it).second.value); else { AXAppLog->Write(MESSAGE_SYS, "Warning: \"%s\" setting not found!", name.c_str()); return static_cast(0); } } catch (bad_any_cast &) { AXAppLog->Write(MESSAGE_SYS, "Warning: \"%s\" setting could not be casted to requested type.", name.c_str()); return static_cast(0); } } AXResult ParseFile(string filename, SettingType type); void WriteToFile(string filename, SettingType type);private: struct _SettingStruct { boost::any value; SettingType type; }; map SettingsMap;};#endif
.cpp
/* -------------------------------------------------------------- File: AXSettings.cpp Description: Implementation for the AXSettings class. See AXSettings.h for details. Date: July 1, 2005 Author: James Long-------------------------------------------------------------- */#include "..\\AXCore.h"#include "..\\AXUtilities\\AXStringFunctions.h"AXSettings::AXSettings() : AXSingleton() {}AXSettings::~AXSettings() {}AXResult AXSettings::ParseFile(string filename, SettingType type) { ifstream in(filename.c_str()); string Name; boost::any Value; if(!in.is_open()) { AXAppLog->Write(MESSAGE_APP, "Could not open settings file: %s", filename.c_str()); return AXFAILURE; } while(!in.eof()) { char szBuf[1024]; in.getline(szBuf, 1024); string line(szBuf); if(line == "") continue; string::size_type eqPos = line.find_first_of('='); if(eqPos != string::npos) { Name = line.substr(0, eqPos); Value = AXStringConvertToAny(line.substr(eqPos + 1)); } else { Name = line; Value = 0; } //set the variable RegisterSetting(Name, Value, type); } in.close(); return AXSUCCESS;}void AXSettings::WriteToFile(string filename, SettingType type) { ofstream out(filename.c_str()); map::iterator it; for(it = SettingsMap.begin(); it != SettingsMap.end(); it++) { if((*it).second.type == type) out << (*it).first << "=" << AXStringConvertFromAny((*it).second.value ) << "\n"; } out.close();}