Configuration.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "Configuration.hpp"
  2. #include <boost/property_tree/ptree.hpp>
  3. #include <boost/property_tree/ini_parser.hpp>
  4. #include <boost/format.hpp>
  5. using namespace config;
  6. namespace config {
  7. struct ConfigurationImpl {
  8. boost::property_tree::ptree ptree;
  9. };
  10. template<typename TYPE>
  11. TYPE get(boost::property_tree::ptree tree, const std::string &property) {
  12. try {
  13. return tree.get<TYPE>(property);
  14. } catch (boost::property_tree::ptree_bad_path) {
  15. throw ConfigException((boost::format("Configuration option '%s' (type: %s) not found.")
  16. % property % typeid(TYPE).name()).str());
  17. }
  18. }
  19. }
  20. config::Configuration::Configuration(const std::string fileName)
  21. : impl(new ConfigurationImpl()) {
  22. boost::property_tree::read_ini(fileName, impl->ptree);
  23. }
  24. config::Configuration::Configuration(std::vector<std::string> const fileNames)
  25. : impl(new ConfigurationImpl()) {
  26. for (auto &name : fileNames) {
  27. boost::property_tree::read_ini(name, impl->ptree);
  28. }
  29. }
  30. config::Configuration::~Configuration() {
  31. delete impl;
  32. }
  33. int config::Configuration::getInt(const std::string &property) {
  34. return get<int>(impl->ptree, property);
  35. }
  36. bool config::Configuration::getBool(const std::string &property) {
  37. return get<bool>(impl->ptree, property);
  38. }
  39. std::string config::Configuration::getString(const std::string &property) {
  40. return get<std::string>(impl->ptree, property);
  41. }