Core/Config: Refactored ConfigMgr

* Loading initial configuration files is now separate from loading any additional custom configs
This commit is contained in:
Shauren
2013-07-15 17:31:44 +02:00
parent baeecba6c9
commit 779a59e7e2
16 changed files with 470 additions and 424 deletions

View File

@@ -17,72 +17,79 @@
*/
#include "Config.h"
#include <ace/Auto_Ptr.h>
#include <ace/Configuration_Import_Export.h>
#include <ace/Thread_Mutex.h>
#include "Errors.h"
namespace ConfigMgr
// Defined here as it must not be exposed to end-users.
bool ConfigMgr::GetValueHelper(const char* name, ACE_TString &result)
{
GuardType guard(_configLock);
namespace
{
typedef ACE_Thread_Mutex LockType;
typedef ACE_Guard<LockType> GuardType;
std::string _filename;
ACE_Auto_Ptr<ACE_Configuration_Heap> _config;
LockType m_configLock;
// Defined here as it must not be exposed to end-users.
bool GetValueHelper(const char* name, ACE_TString &result)
{
GuardType guard(m_configLock);
if (_config.get() == 0)
return false;
ACE_TString section_name;
ACE_Configuration_Section_Key section_key;
const ACE_Configuration_Section_Key &root_key = _config->root_section();
int i = 0;
while (_config->enumerate_sections(root_key, i, section_name) == 0)
{
_config->open_section(root_key, section_name.c_str(), 0, section_key);
if (_config->get_string_value(section_key, name, result) == 0)
return true;
++i;
}
if (_config.get() == 0)
return false;
ACE_TString section_name;
ACE_Configuration_Section_Key section_key;
const ACE_Configuration_Section_Key &root_key = _config->root_section();
int i = 0;
while (_config->enumerate_sections(root_key, i, section_name) == 0)
{
_config->open_section(root_key, section_name.c_str(), 0, section_key);
if (_config->get_string_value(section_key, name, result) == 0)
return true;
++i;
}
return false;
}
bool Load(const char* file)
bool ConfigMgr::LoadInitial(char const* file)
{
GuardType guard(m_configLock);
ASSERT(file);
if (file)
_filename = file;
GuardType guard(_configLock);
_config.reset(new ACE_Configuration_Heap);
_filename = file;
_config.reset(new ACE_Configuration_Heap());
if (_config->open() == 0)
{
ACE_Ini_ImpExp config_importer(*_config.get());
if (config_importer.import_config(_filename.c_str()) == 0)
if (LoadData(_filename.c_str()))
return true;
}
_config.reset();
return false;
}
std::string GetStringDefault(const char* name, const std::string &def)
bool ConfigMgr::LoadMore(char const* file)
{
ASSERT(file);
ASSERT(_config);
GuardType guard(_configLock);
return LoadData(file);
}
bool ConfigMgr::Reload()
{
return LoadInitial(_filename.c_str());
}
bool ConfigMgr::LoadData(char const* file)
{
ACE_Ini_ImpExp config_importer(*_config.get());
if (config_importer.import_config(file) == 0)
return true;
return false;
}
std::string ConfigMgr::GetStringDefault(const char* name, const std::string &def)
{
ACE_TString val;
return GetValueHelper(name, val) ? val.c_str() : def;
}
bool GetBoolDefault(const char* name, bool def)
bool ConfigMgr::GetBoolDefault(const char* name, bool def)
{
ACE_TString val;
@@ -93,22 +100,20 @@ bool GetBoolDefault(const char* name, bool def)
val == "1");
}
int GetIntDefault(const char* name, int def)
int ConfigMgr::GetIntDefault(const char* name, int def)
{
ACE_TString val;
return GetValueHelper(name, val) ? atoi(val.c_str()) : def;
}
float GetFloatDefault(const char* name, float def)
float ConfigMgr::GetFloatDefault(const char* name, float def)
{
ACE_TString val;
return GetValueHelper(name, val) ? (float)atof(val.c_str()) : def;
}
const std::string & GetFilename()
std::string const& ConfigMgr::GetFilename()
{
GuardType guard(m_configLock);
GuardType guard(_configLock);
return _filename;
}
} // namespace