Configuration.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <fstream>
  2. #include <boost/property_tree/ptree.hpp>
  3. #include <boost/property_tree/ini_parser.hpp>
  4. #include "Configuration.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(std::string(typeid(TYPE).name()) + " key \'" + property + "\' not found");
  16. }
  17. }
  18. }
  19. config::Configuration::Configuration(const std::string fileName)
  20. : impl(new ConfigurationImpl()) {
  21. boost::property_tree::read_ini(fileName, impl->ptree);
  22. }
  23. config::Configuration::Configuration(std::vector<std::string> const fileNames)
  24. : impl(new ConfigurationImpl()) {
  25. for (auto &name : fileNames) {
  26. boost::property_tree::read_ini(name, impl->ptree);
  27. }
  28. }
  29. config::Configuration::~Configuration() {
  30. delete impl;
  31. }
  32. int config::Configuration::getInt(const std::string &property) {
  33. return get<int>(impl->ptree, property);
  34. }
  35. bool config::Configuration::getBool(const std::string &property) {
  36. return get<bool>(impl->ptree, property);
  37. }
  38. std::string config::Configuration::getString(const std::string &property) {
  39. return get<std::string>(impl->ptree, property);
  40. }