26 #ifndef NFD_CORE_CONFIG_FILE_HPP 27 #define NFD_CORE_CONFIG_FILE_HPP 31 #include <boost/property_tree/ptree.hpp> 45 typedef function<void(
const ConfigSection& section,
51 typedef function<void(
const std::string& filename,
52 const std::string& sectionName,
53 const ConfigSection& section,
61 class Error :
public std::runtime_error
66 :
std::runtime_error(what)
77 const std::string& sectionName,
78 const ConfigSection& section,
83 const std::string& sectionName,
84 const ConfigSection& section,
94 parseYesNo(
const ConfigSection& node,
const std::string& key,
const std::string& sectionName);
97 parseYesNo(
const ConfigSection::value_type& option,
const std::string& sectionName)
99 return parseYesNo(option.second, option.first, sectionName);
111 parseNumber(
const ConfigSection& node,
const std::string& key,
const std::string& sectionName)
113 static_assert(std::is_arithmetic<T>::value,
"T must be an arithmetic type");
115 boost::optional<T> value = node.get_value_optional<T>();
119 BOOST_THROW_EXCEPTION(
Error(
"Invalid value \"" + node.get_value<std::string>() +
120 "\" for option \"" + key +
"\" in \"" + sectionName +
"\" section"));
123 template <
typename T>
125 parseNumber(
const ConfigSection::value_type& option,
const std::string& sectionName)
127 return parseNumber<T>(option.second, option.first, sectionName);
134 ConfigSectionHandler subscriber);
143 parse(
const std::string& filename,
bool isDryRun);
153 parse(
const std::string& input,
bool isDryRun,
const std::string& filename);
162 parse(std::istream& input,
bool isDryRun,
const std::string& filename);
171 parse(
const ConfigSection& config,
bool isDryRun,
const std::string& filename);
175 process(
bool isDryRun,
const std::string& filename)
const;
178 UnknownConfigSectionHandler m_unknownSectionCallback;
179 std::map<std::string, ConfigSectionHandler> m_subscriptions;
180 ConfigSection m_global;
185 #endif // NFD_CORE_CONFIG_FILE_HPP void addSectionHandler(const std::string §ionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
configuration file parsing utility
static T parseNumber(const ConfigSection::value_type &option, const std::string §ionName)
static bool parseYesNo(const ConfigSection &node, const std::string &key, const std::string §ionName)
parse a config option that can be either "yes" or "no"
function< void(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)> UnknownConfigSectionHandler
callback to process a config file section without a ConfigSectionHandler
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback=throwErrorOnUnknownSection)
static T parseNumber(const ConfigSection &node, const std::string &key, const std::string §ionName)
parse a numeric (integral or floating point) config option
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
void parse(const std::string &filename, bool isDryRun)
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
boost::property_tree::ptree ConfigSection
a config file section
function< void(const ConfigSection §ion, bool isDryRun, const std::string &filename)> ConfigSectionHandler
callback to process a config file section
static bool parseYesNo(const ConfigSection::value_type &option, const std::string §ionName)
static void throwErrorOnUnknownSection(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)
Error(const std::string &what)
static void ignoreUnknownSection(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)