26 #ifndef NFD_DAEMON_COMMON_CONFIG_FILE_HPP
27 #define NFD_DAEMON_COMMON_CONFIG_FILE_HPP
31 #include <boost/property_tree/ptree.hpp>
52 const std::string& filename)>;
58 const std::string& sectionName,
68 class Error :
public std::runtime_error
71 using std::runtime_error::runtime_error;
80 const std::string& sectionName,
86 const std::string& sectionName,
100 parseYesNo(
const ConfigSection::value_type& option,
const std::string& sectionName)
102 return parseYesNo(option.second, option.first, sectionName);
116 static_assert(std::is_arithmetic_v<T>);
118 auto value = node.get_value_optional<T>();
121 (std::is_signed_v<T> || node.get_value<std::string>().find(
"-") == std::string::npos)) {
124 NDN_THROW(
Error(
"Invalid value '" + node.get_value<std::string>() +
125 "' for option '" + key +
"' in section '" + sectionName +
"'"));
130 parseNumber(
const ConfigSection::value_type& option,
const std::string& sectionName)
132 return parseNumber<T>(option.second, option.first, sectionName);
141 checkRange(T value, T min, T max,
const std::string& key,
const std::string& sectionName)
143 static_assert(std::is_integral_v<T>);
145 if (value < min || value > max) {
146 NDN_THROW(
Error(
"Invalid value '" + std::to_string(value) +
"' for option '" + key +
147 "' in section '" + sectionName +
"': out of acceptable range [" +
148 std::to_string(min) +
", " + std::to_string(max) +
"]"));
165 parse(
const std::string& filename,
bool isDryRun);
175 parse(
const std::string& input,
bool isDryRun,
const std::string& filename);
184 parse(std::istream& input,
bool isDryRun,
const std::string& filename);
197 process(
bool isDryRun,
const std::string& filename)
const;
201 std::map<std::string, ConfigSectionHandler> m_subscriptions;
Configuration file parsing utility.
static T parseNumber(const ConfigSection &node, const std::string &key, const std::string §ionName)
Parse a numeric (integral or floating point) config option.
static void throwErrorOnUnknownSection(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)
void parse(const std::string &filename, bool isDryRun)
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback=throwErrorOnUnknownSection)
static void checkRange(T value, T min, T max, const std::string &key, const std::string §ionName)
Check that a value is within the inclusive range [min, max].
static T parseNumber(const ConfigSection::value_type &option, const std::string §ionName)
static void ignoreUnknownSection(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)
void addSectionHandler(const std::string §ionName, ConfigSectionHandler subscriber)
Setup notification of configuration file sections.
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)
boost::optional< const ConfigSection & > OptionalConfigSection
An optional configuration file section.
std::function< void(const std::string &filename, const std::string §ionName, const ConfigSection §ion, bool isDryRun)> UnknownConfigSectionHandler
Callback to process a configuration file section without a ConfigSectionHandler.
boost::property_tree::ptree ConfigSection
A configuration file section.
std::function< void(const ConfigSection §ion, bool isDryRun, const std::string &filename)> ConfigSectionHandler
Callback to process a configuration file section.