Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Chunk of code

Sign in to follow this  


Until I can get to the point where screenshots are available, I thought I should post something productive.

So here's a chunk of code. Today I give you my Settings Manager:


/* --------------------------------------------------------------
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

-------------------------------------------------------------- */


typedef int SettingType;

static const SettingType SETTING_SYSTEM = 1;
static const SettingType SETTING_USER = 2;

class AXSettings : public AXSingleton {
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);


struct _SettingStruct {
boost::any value;
SettingType type;

map SettingsMap;



/* --------------------------------------------------------------
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());

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);



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";


Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!