mgmt: TablesConfigSection constructor accepts Forwarder&
refs #2181
Change-Id: I1a87dd573f25f3806bc1b1f3c60ec9eef0eca4e3
diff --git a/core/config-file.cpp b/core/config-file.cpp
index 19bf1cb..b9f40b1 100644
--- a/core/config-file.cpp
+++ b/core/config-file.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2015, Regents of the University of California,
+ * Copyright (c) 2014-2016, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -59,10 +59,10 @@
}
bool
-ConfigFile::parseYesNo(const ConfigSection::value_type& option,
+ConfigFile::parseYesNo(const ConfigSection& node, const std::string& key,
const std::string& sectionName)
{
- auto value = option.second.get_value<std::string>();
+ auto value = node.get_value<std::string>();
if (value == "yes") {
return true;
@@ -72,7 +72,7 @@
}
BOOST_THROW_EXCEPTION(Error("Invalid value \"" + value + "\" for option \"" +
- option.first + "\" in \"" + sectionName + "\" section"));
+ key + "\" in \"" + sectionName + "\" section"));
}
void
@@ -85,14 +85,10 @@
void
ConfigFile::parse(const std::string& filename, bool isDryRun)
{
- std::ifstream inputFile;
- inputFile.open(filename.c_str());
- if (!inputFile.good() || !inputFile.is_open())
- {
- std::string msg = "Failed to read configuration file: ";
- msg += filename;
- BOOST_THROW_EXCEPTION(Error(msg));
- }
+ std::ifstream inputFile(filename);
+ if (!inputFile.good() || !inputFile.is_open()) {
+ BOOST_THROW_EXCEPTION(Error("Failed to read configuration file: " + filename));
+ }
parse(inputFile, isDryRun, filename);
inputFile.close();
}
@@ -107,18 +103,16 @@
void
ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
{
- try
- {
- boost::property_tree::read_info(input, m_global);
- }
- catch (const boost::property_tree::info_parser_error& error)
- {
- std::stringstream msg;
- msg << "Failed to parse configuration file";
- msg << " " << filename;
- msg << " " << error.message() << " line " << error.line();
- BOOST_THROW_EXCEPTION(Error(msg.str()));
- }
+ try {
+ boost::property_tree::read_info(input, m_global);
+ }
+ catch (const boost::property_tree::info_parser_error& error) {
+ std::stringstream msg;
+ msg << "Failed to parse configuration file";
+ msg << " " << filename;
+ msg << " " << error.message() << " line " << error.line();
+ BOOST_THROW_EXCEPTION(Error(msg.str()));
+ }
process(isDryRun, filename);
}
@@ -144,7 +138,7 @@
for (const auto& i : m_global) {
try {
- ConfigSectionHandler subscriber = m_subscriptions.at(i.first);
+ const ConfigSectionHandler& subscriber = m_subscriptions.at(i.first);
subscriber(i.second, isDryRun, filename);
}
catch (const std::out_of_range&) {
@@ -153,4 +147,4 @@
}
}
-}
+} // namespace nfd
diff --git a/core/config-file.hpp b/core/config-file.hpp
index f6e4aaf..890a22c 100644
--- a/core/config-file.hpp
+++ b/core/config-file.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2014-2015, Regents of the University of California,
+ * Copyright (c) 2014-2016, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -45,6 +45,8 @@
const ConfigSection& /*section*/,
bool /*isDryRun*/)> UnknownConfigSectionHandler;
+/** \brief configuration file parsing utility
+ */
class ConfigFile : noncopyable
{
public:
@@ -61,6 +63,7 @@
explicit
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback = throwErrorOnUnknownSection);
+public: // unknown section handlers
static void
throwErrorOnUnknownSection(const std::string& filename,
const std::string& sectionName,
@@ -73,27 +76,50 @@
const ConfigSection& section,
bool isDryRun);
- /**
- * \brief parse a config option that can be either "yes" or "no"
- *
- * \return true if "yes", false if "no"
- * \throw Error the value is neither "yes" nor "no"
+public: // parse helpers
+ /** \brief parse a config option that can be either "yes" or "no"
+ * \retval true "yes"
+ * \retval false "no"
+ * \throw Error the value is neither "yes" nor "no"
*/
static bool
- parseYesNo(const ConfigSection::value_type& option,
- const std::string& sectionName);
+ parseYesNo(const ConfigSection& node, const std::string& key, const std::string& sectionName);
+
+ static bool
+ parseYesNo(const ConfigSection::value_type& option, const std::string& sectionName)
+ {
+ return parseYesNo(option.second, option.first, sectionName);
+ }
/**
* \brief parse a numeric (integral or floating point) config option
+ * \tparam T an arithmetic type
*
* \return the numeric value of the parsed option
* \throw Error the value cannot be converted to the specified type
*/
- template <typename T>
- static typename std::enable_if<std::is_arithmetic<T>::value, T>::type
- parseNumber(const ConfigSection::value_type& option,
- const std::string& sectionName);
+ template<typename T>
+ static T
+ parseNumber(const ConfigSection& node, const std::string& key, const std::string& sectionName)
+ {
+ static_assert(std::is_arithmetic<T>::value, "T must be an arithmetic type");
+ boost::optional<T> value = node.get_value_optional<T>();
+ if (value) {
+ return *value;
+ }
+ BOOST_THROW_EXCEPTION(Error("Invalid value \"" + node.get_value<std::string>() +
+ "\" for option \"" + key + "\" in \"" + sectionName + "\" section"));
+ }
+
+ template <typename T>
+ static T
+ parseNumber(const ConfigSection::value_type& option, const std::string& sectionName)
+ {
+ return parseNumber<T>(option.second, option.first, sectionName);
+ }
+
+public: // setup and parsing
/// \brief setup notification of configuration file sections
void
addSectionHandler(const std::string& sectionName,
@@ -146,22 +172,6 @@
ConfigSection m_global;
};
-template <typename T>
-inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
-ConfigFile::parseNumber(const ConfigSection::value_type& option,
- const std::string& sectionName)
-{
- auto value = option.second.get_value<std::string>();
-
- try {
- return boost::lexical_cast<T>(value);
- }
- catch (const boost::bad_lexical_cast&) {
- BOOST_THROW_EXCEPTION(Error("Invalid value \"" + value + "\" for option \"" +
- option.first + "\" in \"" + sectionName + "\" section"));
- }
-}
-
} // namespace nfd
#endif // NFD_CORE_CONFIG_FILE_HPP