Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ------------------
- // CConfig.cpp - VrEngine configuration file management
- // Author: EpicLegion (C) 2011
- // ------------------
- #include "CConfig.hpp"
- namespace VrEngine {
- // Constructor/destructor
- CConfig::CConfig()
- {
- m_keyToType = new std::map<const std::string, ConfigType>;
- m_keyToPointer = new std::map<const std::string, void*>;
- }
- CConfig::~CConfig()
- {
- // Iterate key-pointer map
- std::map<const std::string, void*>::iterator iter;
- for(iter = m_keyToPointer->begin(); iter != m_keyToPointer->end(); iter++)
- {
- // Free
- if(iter->second != NULL)
- {
- delete iter->second;
- }
- }
- // Delete maps
- delete m_keyToType;
- delete m_keyToPointer;
- }
- // General stuff
- bool CConfig::RegisterNewCvar(const std::string& key, ConfigType type)
- {
- // Already exists?
- if(this->GetType(key) != CONFIG_NONE)
- {
- return false;
- }
- // Invalid type?
- if(type == CONFIG_NONE)
- {
- return false;
- }
- // Add to map
- (*m_keyToType)[key] = type;
- (*m_keyToPointer)[key] = NULL;
- // Done
- return true;
- }
- ConfigType CConfig::GetType(const std::string& key) const
- {
- // Find
- std::map<const std::string, ConfigType>::const_iterator iter;
- iter = m_keyToType->find(key);
- // Not found
- if(iter == m_keyToType->end())
- {
- return CONFIG_NONE;
- }
- // Found
- return iter->second;
- }
- // Getters
- Int32 CConfig::GetInt(const std::string& key, Int32 defaultValue) const
- {
- // Type check
- ConfigType type = this->GetType(key);
- if(type != CONFIG_INT32 && type != CONFIG_UINT32)
- {
- return defaultValue;
- }
- // Ptr
- Int32* ptr = (Int32*) (*m_keyToPointer)[key];
- // Null?
- if(ptr == NULL)
- {
- return defaultValue;
- }
- // Return
- return *ptr;
- }
- const std::string& CConfig::GetString(const std::string& key, const std::string& defaultValue) const
- {
- // Oops
- if(this->GetType(key) != CONFIG_STRING)
- {
- return defaultValue;
- }
- // Ptr
- std::string* ptr = (std::string*) (*m_keyToPointer)[key];
- // Null?
- if(ptr == NULL)
- {
- return defaultValue;
- }
- // Return
- return *ptr;
- }
- bool CConfig::GetBool(const std::string& key, bool defaultValue) const
- {
- // Oops
- if(this->GetType(key) != CONFIG_BOOL)
- {
- return defaultValue;
- }
- // Ptr
- bool* ptr = (bool*) (*m_keyToPointer)[key];
- // Null?
- if(ptr == NULL)
- {
- return defaultValue;
- }
- // Return
- return *ptr;
- }
- float CConfig::GetFloat(const std::string& key, float defaultValue) const
- {
- // Oops
- if(this->GetType(key) != CONFIG_FLOAT)
- {
- return defaultValue;
- }
- // Ptr
- float* ptr = (float*) (*m_keyToPointer)[key];
- // Null?
- if(ptr == NULL)
- {
- return defaultValue;
- }
- // Return
- return *ptr;
- }
- // Setters
- void CConfig::SetInt(const std::string& key, Int32 value)
- {
- // Type check
- ConfigType type = this->GetType(key);
- if(type != CONFIG_INT32 && type != CONFIG_UINT32)
- {
- return;
- }
- if(type == CONFIG_UINT32 && value < 0)
- {
- return;
- }
- // Ptr
- Int32* ptr = (Int32*) (*m_keyToPointer)[key];
- // Null?
- if(ptr == NULL)
- {
- // Allocate memory
- ptr = new Int32;
- // Set value
- *ptr = value;
- // Set
- (*m_keyToPointer)[key] = (void*) ptr;
- }
- else
- {
- *ptr = value;
- }
- }
- void CConfig::SetString(const std::string& key, const std::string& value)
- {
- // Oops
- if(this->GetType(key) != CONFIG_STRING)
- {
- return;
- }
- // Cleanup
- std::string* ptr = (std::string*) (*m_keyToPointer)[key];
- if(ptr != NULL)
- {
- delete ptr;
- ptr = NULL;
- }
- // Allocate memory
- ptr = new std::string(value);
- // Set
- (*m_keyToPointer)[key] = (void*) ptr;
- }
- void CConfig::SetBool(const std::string& key, bool value)
- {
- // Oops
- if(this->GetType(key) != CONFIG_BOOL)
- {
- return;
- }
- // Ptr
- bool* ptr = (bool*) (*m_keyToPointer)[key];
- // Null?
- if(ptr == NULL)
- {
- // Allocate memory
- ptr = new bool;
- // Set value
- *ptr = value;
- // Set
- (*m_keyToPointer)[key] = (void*) ptr;
- }
- else
- {
- *ptr = value;
- }
- }
- void CConfig::SetFloat(const std::string& key, float value)
- {
- // Oops
- if(this->GetType(key) != CONFIG_FLOAT)
- {
- return;
- }
- // Ptr
- float* ptr = (float*) (*m_keyToPointer)[key];
- // Null?
- if(ptr == NULL)
- {
- // Allocate memory
- ptr = new float;
- // Set value
- *ptr = value;
- // Set
- (*m_keyToPointer)[key] = (void*) ptr;
- }
- else
- {
- *ptr = value;
- }
- }
- };
Add Comment
Please, Sign In to add comment