face+mgmt: simplify factories and FaceManager using C++11 features

Change-Id: I113a4c15ef82bff705f61f31b170f49727bc3794
Refs: #3258
diff --git a/core/config-file.cpp b/core/config-file.cpp
index d989455..19bf1cb 100644
--- a/core/config-file.cpp
+++ b/core/config-file.cpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014-2015,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #include "config-file.hpp"
 #include "logger.hpp"
@@ -30,6 +31,11 @@
 
 namespace nfd {
 
+ConfigFile::ConfigFile(UnknownConfigSectionHandler unknownSectionCallback)
+  : m_unknownSectionCallback(unknownSectionCallback)
+{
+}
+
 void
 ConfigFile::throwErrorOnUnknownSection(const std::string& filename,
                                        const std::string& sectionName,
@@ -53,27 +59,20 @@
 }
 
 bool
-ConfigFile::parseYesNo(const ConfigSection::const_iterator& i,
-                        const std::string& optionName,
-                        const std::string& sectionName)
+ConfigFile::parseYesNo(const ConfigSection::value_type& option,
+                       const std::string& sectionName)
 {
-  const std::string value = i->second.get_value<std::string>();
+  auto value = option.second.get_value<std::string>();
+
   if (value == "yes") {
     return true;
   }
-
-  if (value == "no") {
+  else if (value == "no") {
     return false;
   }
 
-  BOOST_THROW_EXCEPTION(ConfigFile::Error("Invalid value for option \"" +
-                                          optionName + "\" in \"" +
-                                          sectionName + "\" section"));
-}
-
-ConfigFile::ConfigFile(UnknownConfigSectionHandler unknownSectionCallback)
-  : m_unknownSectionCallback(unknownSectionCallback)
-{
+  BOOST_THROW_EXCEPTION(Error("Invalid value \"" + value + "\" for option \"" +
+                              option.first + "\" in \"" + sectionName + "\" section"));
 }
 
 void
@@ -105,7 +104,6 @@
   parse(inputStream, isDryRun, filename);
 }
 
-
 void
 ConfigFile::parse(std::istream& input, bool isDryRun, const std::string& filename)
 {
@@ -133,35 +131,26 @@
 }
 
 void
-ConfigFile::process(bool isDryRun, const std::string& filename)
+ConfigFile::process(bool isDryRun, const std::string& filename) const
 {
   BOOST_ASSERT(!filename.empty());
-  // NFD_LOG_DEBUG("processing..." << ((isDryRun)?("dry run"):("")));
 
-  if (m_global.begin() == m_global.end())
-    {
-      std::string msg = "Error processing configuration file: ";
-      msg += filename;
-      msg += " no data";
-      BOOST_THROW_EXCEPTION(Error(msg));
+  if (m_global.begin() == m_global.end()) {
+    std::string msg = "Error processing configuration file: ";
+    msg += filename;
+    msg += " no data";
+    BOOST_THROW_EXCEPTION(Error(msg));
+  }
+
+  for (const auto& i : m_global) {
+    try {
+      ConfigSectionHandler subscriber = m_subscriptions.at(i.first);
+      subscriber(i.second, isDryRun, filename);
     }
-
-  for (ConfigSection::const_iterator i = m_global.begin(); i != m_global.end(); ++i)
-    {
-      const std::string& sectionName = i->first;
-      const ConfigSection& section = i->second;
-
-      SubscriptionTable::iterator subscriberIt = m_subscriptions.find(sectionName);
-      if (subscriberIt != m_subscriptions.end())
-        {
-          ConfigSectionHandler subscriber = subscriberIt->second;
-          subscriber(section, isDryRun, filename);
-        }
-      else
-        {
-          m_unknownSectionCallback(filename, sectionName, section, isDryRun);
-        }
+    catch (const std::out_of_range&) {
+      m_unknownSectionCallback(filename, i.first, i.second, isDryRun);
     }
+  }
 }
 
 }
diff --git a/core/config-file.hpp b/core/config-file.hpp
index ae9102e..f6e4aaf 100644
--- a/core/config-file.hpp
+++ b/core/config-file.hpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014-2015,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #ifndef NFD_CORE_CONFIG_FILE_HPP
 #define NFD_CORE_CONFIG_FILE_HPP
@@ -47,7 +48,6 @@
 class ConfigFile : noncopyable
 {
 public:
-
   class Error : public std::runtime_error
   {
   public:
@@ -55,10 +55,10 @@
     Error(const std::string& what)
       : std::runtime_error(what)
     {
-
     }
   };
 
+  explicit
   ConfigFile(UnknownConfigSectionHandler unknownSectionCallback = throwErrorOnUnknownSection);
 
   static void
@@ -73,22 +73,32 @@
                        const ConfigSection& section,
                        bool isDryRun);
 
-  /** @brief parse a config option that can be either "yes" or "no"
+  /**
+   * \brief parse a config option that can be either "yes" or "no"
    *
-   *  @throw ConfigFile::Error value is neither "yes" nor "no"
-   *  @return true if "yes", false if "no"
+   * \return true if "yes", false if "no"
+   * \throw Error the value is neither "yes" nor "no"
    */
   static bool
-  parseYesNo(const ConfigSection::const_iterator& i,
-             const std::string& optionName,
+  parseYesNo(const ConfigSection::value_type& option,
              const std::string& sectionName);
 
+  /**
+   * \brief parse a numeric (integral or floating point) config option
+   *
+   * \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);
+
   /// \brief setup notification of configuration file sections
   void
   addSectionHandler(const std::string& sectionName,
                     ConfigSectionHandler subscriber);
 
-
   /**
    * \param filename file to parse
    * \param isDryRun true if performing a dry run of configuration, false otherwise
@@ -127,21 +137,31 @@
   parse(const ConfigSection& config, bool isDryRun, const std::string& filename);
 
 private:
-
   void
-  process(bool isDryRun, const std::string& filename);
+  process(bool isDryRun, const std::string& filename) const;
 
 private:
   UnknownConfigSectionHandler m_unknownSectionCallback;
-
-  typedef std::map<std::string, ConfigSectionHandler> SubscriptionTable;
-
-  SubscriptionTable m_subscriptions;
-
+  std::map<std::string, ConfigSectionHandler> m_subscriptions;
   ConfigSection m_global;
 };
 
-} // namespace nfd
+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