26 #ifndef NFD_CORE_CONFIG_FILE_HPP 27 #define NFD_CORE_CONFIG_FILE_HPP 31 #include <boost/property_tree/ptree.hpp> 38 typedef function<void(
const ConfigSection& ,
43 typedef function<void(
const std::string& ,
45 const ConfigSection& ,
53 class Error :
public std::runtime_error
58 :
std::runtime_error(what)
69 const std::string& sectionName,
70 const ConfigSection& section,
75 const std::string& sectionName,
76 const ConfigSection& section,
86 parseYesNo(
const ConfigSection& node,
const std::string& key,
const std::string& sectionName);
89 parseYesNo(
const ConfigSection::value_type& option,
const std::string& sectionName)
91 return parseYesNo(option.second, option.first, sectionName);
103 parseNumber(
const ConfigSection& node,
const std::string& key,
const std::string& sectionName)
105 static_assert(std::is_arithmetic<T>::value,
"T must be an arithmetic type");
107 boost::optional<T> value = node.get_value_optional<T>();
111 BOOST_THROW_EXCEPTION(
Error(
"Invalid value \"" + node.get_value<std::string>() +
112 "\" for option \"" + key +
"\" in \"" + sectionName +
"\" section"));
115 template <
typename T>
117 parseNumber(
const ConfigSection::value_type& option,
const std::string& sectionName)
119 return parseNumber<T>(option.second, option.first, sectionName);
126 ConfigSectionHandler subscriber);
135 parse(
const std::string& filename,
bool isDryRun);
145 parse(
const std::string& input,
bool isDryRun,
const std::string& filename);
154 parse(std::istream& input,
bool isDryRun,
const std::string& filename);
163 parse(
const ConfigSection& config,
bool isDryRun,
const std::string& filename);
167 process(
bool isDryRun,
const std::string& filename)
const;
170 UnknownConfigSectionHandler m_unknownSectionCallback;
171 std::map<std::string, ConfigSectionHandler> m_subscriptions;
172 ConfigSection m_global;
177 #endif // NFD_CORE_CONFIG_FILE_HPP 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"
static bool parseYesNo(const ConfigSection::value_type &option, const std::string §ionName)
configuration file parsing utility
function< void(const ConfigSection &, bool, const std::string &)> ConfigSectionHandler
callback for config file sections
Error(const std::string &what)
static void throwErrorOnUnknownSection(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)
Copyright (c) 2011-2015 Regents of the University of California.
void addSectionHandler(const std::string §ionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
function< void(const std::string &, const std::string &, const ConfigSection &, bool)> UnknownConfigSectionHandler
callback for config file sections without a subscribed handler
boost::property_tree::ptree ConfigSection
static T parseNumber(const ConfigSection &node, const std::string &key, const std::string §ionName)
parse a numeric (integral or floating point) config option
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback=throwErrorOnUnknownSection)
static void ignoreUnknownSection(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)
void parse(const std::string &filename, bool isDryRun)
static T parseNumber(const ConfigSection::value_type &option, const std::string §ionName)