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& ,
51 class Error :
public std::runtime_error
56 :
std::runtime_error(what)
66 const std::string& sectionName,
67 const ConfigSection& section,
72 const std::string& sectionName,
73 const ConfigSection& section,
83 parseYesNo(
const ConfigSection::value_type& option,
84 const std::string& sectionName);
93 static typename std::enable_if<std::is_arithmetic<T>::value, T>::type
94 parseNumber(
const ConfigSection::value_type& option,
95 const std::string& sectionName);
100 ConfigSectionHandler subscriber);
109 parse(
const std::string& filename,
bool isDryRun);
119 parse(
const std::string& input,
bool isDryRun,
const std::string& filename);
128 parse(std::istream& input,
bool isDryRun,
const std::string& filename);
137 parse(
const ConfigSection& config,
bool isDryRun,
const std::string& filename);
141 process(
bool isDryRun,
const std::string& filename)
const;
144 UnknownConfigSectionHandler m_unknownSectionCallback;
145 std::map<std::string, ConfigSectionHandler> m_subscriptions;
146 ConfigSection m_global;
149 template <
typename T>
150 inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
152 const std::string& sectionName)
154 auto value = option.second.get_value<std::string>();
157 return boost::lexical_cast<T>(value);
159 catch (
const boost::bad_lexical_cast&) {
160 BOOST_THROW_EXCEPTION(
Error(
"Invalid value \"" + value +
"\" for option \"" +
161 option.first +
"\" in \"" + sectionName +
"\" section"));
167 #endif // NFD_CORE_CONFIG_FILE_HPP
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 std::enable_if< std::is_arithmetic< T >::value, T >::type parseNumber(const ConfigSection::value_type &option, const std::string §ionName)
parse a numeric (integral or floating point) config option
static bool parseYesNo(const ConfigSection::value_type &option, const std::string §ionName)
parse a config option that can be either "yes" or "no"
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)