TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ConfigMgr Class Reference

#include <Config.h>

Public Member Functions

bool LoadInitial (std::string const &file, std::string &error)
 Method used only for loading main configuration files. More...
 
bool Reload (std::string &error)
 
std::string GetStringDefault (std::string const &name, const std::string &def) const
 
bool GetBoolDefault (std::string const &name, bool def) const
 
int GetIntDefault (std::string const &name, int def) const
 
float GetFloatDefault (std::string const &name, float def) const
 
std::string constGetFilename ()
 
std::list< std::string > GetKeysByString (std::string const &name)
 

Static Public Member Functions

static ConfigMgrinstance ()
 

Private Member Functions

 ConfigMgr ()=default
 
 ConfigMgr (ConfigMgr const &)=delete
 
ConfigMgroperator= (ConfigMgr const &)=delete
 
 ~ConfigMgr ()=default
 
template<class T >
GetValueDefault (std::string const &name, T def) const
 

Private Attributes

std::string _filename
 
boost::property_tree::ptree _config
 
std::mutex _configLock
 

Constructor & Destructor Documentation

ConfigMgr::ConfigMgr ( )
privatedefault
ConfigMgr::ConfigMgr ( ConfigMgr const )
privatedelete
ConfigMgr::~ConfigMgr ( )
privatedefault

Member Function Documentation

bool ConfigMgr::GetBoolDefault ( std::string const name,
bool  def 
) const
122 {
123  std::string val = GetValueDefault(name, std::string(def ? "1" : "0"));
124  val.erase(std::remove(val.begin(), val.end(), '"'), val.end());
125  return (val == "1" || val == "true" || val == "TRUE" || val == "yes" || val == "YES");
126 }
T GetValueDefault(std::string const &name, T def) const
Definition: Config.cpp:73
std::string const & ConfigMgr::GetFilename ( )
139 {
140  std::lock_guard<std::mutex> lock(_configLock);
141  return _filename;
142 }
std::mutex _configLock
Definition: Config.h:55
std::string _filename
Definition: Config.h:53
float ConfigMgr::GetFloatDefault ( std::string const name,
float  def 
) const
134 {
135  return GetValueDefault(name, def);
136 }
T GetValueDefault(std::string const &name, T def) const
Definition: Config.cpp:73
int ConfigMgr::GetIntDefault ( std::string const name,
int  def 
) const
129 {
130  return GetValueDefault(name, def);
131 }
T GetValueDefault(std::string const &name, T def) const
Definition: Config.cpp:73
std::list< std::string > ConfigMgr::GetKeysByString ( std::string const name)
145 {
146  std::lock_guard<std::mutex> lock(_configLock);
147 
148  std::list<std::string> keys;
149 
150  for (const ptree::value_type& child : _config)
151  if (child.first.compare(0, name.length(), name) == 0)
152  keys.push_back(child.first);
153 
154  return keys;
155 }
std::mutex _configLock
Definition: Config.h:55
boost::property_tree::ptree _config
Definition: Config.h:54
std::string ConfigMgr::GetStringDefault ( std::string const name,
const std::string &  def 
) const
115 {
116  std::string val = GetValueDefault(name, def);
117  val.erase(std::remove(val.begin(), val.end(), '"'), val.end());
118  return val;
119 }
T GetValueDefault(std::string const &name, T def) const
Definition: Config.cpp:73
template<class T >
T ConfigMgr::GetValueDefault ( std::string const name,
def 
) const
private
74 {
75  try
76  {
77  return _config.get<T>(ptree::path_type(name, '/'));
78  }
79  catch (boost::property_tree::ptree_bad_path)
80  {
81  TC_LOG_WARN("server.loading", "Missing name %s in config file %s, add \"%s = %s\" to this file",
82  name.c_str(), _filename.c_str(), name.c_str(), std::to_string(def).c_str());
83  }
84  catch (boost::property_tree::ptree_bad_data)
85  {
86  TC_LOG_ERROR("server.loading", "Bad value defined for name %s in config file %s, going to use %s instead",
87  name.c_str(), _filename.c_str(), std::to_string(def).c_str());
88  }
89 
90  return def;
91 }
std::string _filename
Definition: Config.h:53
boost::property_tree::ptree _config
Definition: Config.h:54
#define TC_LOG_WARN(filterType__,...)
Definition: Log.h:204
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:207
ConfigMgr * ConfigMgr::instance ( )
static
62 {
63  static ConfigMgr instance;
64  return &instance;
65 }
Definition: Config.h:29
static ConfigMgr * instance()
Definition: Config.cpp:61
bool ConfigMgr::LoadInitial ( std::string const file,
std::string &  error 
)

Method used only for loading main configuration files.

30 {
31  std::lock_guard<std::mutex> lock(_configLock);
32 
33  _filename = file;
34 
35  try
36  {
37  ptree fullTree;
38  ini_parser::read_ini(file, fullTree);
39 
40  if (fullTree.empty())
41  {
42  error = "empty file (" + file + ")";
43  return false;
44  }
45 
46  // Since we're using only one section per config file, we skip the section and have direct property access
47  _config = fullTree.begin()->second;
48  }
49  catch (ini_parser::ini_parser_error const& e)
50  {
51  if (e.line() == 0)
52  error = e.message() + " (" + e.filename() + ")";
53  else
54  error = e.message() + " (" + e.filename() + ":" + std::to_string(e.line()) + ")";
55  return false;
56  }
57 
58  return true;
59 }
std::mutex _configLock
Definition: Config.h:55
std::string _filename
Definition: Config.h:53
boost::property_tree::ptree _config
Definition: Config.h:54
ConfigMgr& ConfigMgr::operator= ( ConfigMgr const )
privatedelete
bool ConfigMgr::Reload ( std::string &  error)
68 {
69  return LoadInitial(_filename, error);
70 }
std::string _filename
Definition: Config.h:53
bool LoadInitial(std::string const &file, std::string &error)
Method used only for loading main configuration files.
Definition: Config.cpp:29

Member Data Documentation

boost::property_tree::ptree ConfigMgr::_config
private
std::mutex ConfigMgr::_configLock
private
std::string ConfigMgr::_filename
private

The documentation for this class was generated from the following files: