NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
config-file.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #ifndef NFD_CORE_CONFIG_FILE_HPP
27 #define NFD_CORE_CONFIG_FILE_HPP
28 
29 #include "common.hpp"
30 
31 #include <boost/property_tree/ptree.hpp>
32 
33 namespace nfd {
34 
37 typedef boost::property_tree::ptree ConfigSection;
38 
41 typedef boost::optional<const ConfigSection&> OptionalConfigSection;
42 
45 typedef function<void(const ConfigSection& section,
46  bool isDryRun,
47  const std::string& filename)> ConfigSectionHandler;
48 
51 typedef function<void(const std::string& filename,
52  const std::string& sectionName,
53  const ConfigSection& section,
54  bool isDryRun)> UnknownConfigSectionHandler;
55 
58 class ConfigFile : noncopyable
59 {
60 public:
61  class Error : public std::runtime_error
62  {
63  public:
64  explicit
65  Error(const std::string& what)
66  : std::runtime_error(what)
67  {
68  }
69  };
70 
71  explicit
72  ConfigFile(UnknownConfigSectionHandler unknownSectionCallback = throwErrorOnUnknownSection);
73 
74 public: // unknown section handlers
75  static void
76  throwErrorOnUnknownSection(const std::string& filename,
77  const std::string& sectionName,
78  const ConfigSection& section,
79  bool isDryRun);
80 
81  static void
82  ignoreUnknownSection(const std::string& filename,
83  const std::string& sectionName,
84  const ConfigSection& section,
85  bool isDryRun);
86 
87 public: // parse helpers
93  static bool
94  parseYesNo(const ConfigSection& node, const std::string& key, const std::string& sectionName);
95 
96  static bool
97  parseYesNo(const ConfigSection::value_type& option, const std::string& sectionName)
98  {
99  return parseYesNo(option.second, option.first, sectionName);
100  }
101 
109  template<typename T>
110  static T
111  parseNumber(const ConfigSection& node, const std::string& key, const std::string& sectionName)
112  {
113  static_assert(std::is_arithmetic<T>::value, "T must be an arithmetic type");
114 
115  boost::optional<T> value = node.get_value_optional<T>();
116  if (value) {
117  return *value;
118  }
119  BOOST_THROW_EXCEPTION(Error("Invalid value \"" + node.get_value<std::string>() +
120  "\" for option \"" + key + "\" in \"" + sectionName + "\" section"));
121  }
122 
123  template <typename T>
124  static T
125  parseNumber(const ConfigSection::value_type& option, const std::string& sectionName)
126  {
127  return parseNumber<T>(option.second, option.first, sectionName);
128  }
129 
130 public: // setup and parsing
132  void
133  addSectionHandler(const std::string& sectionName,
134  ConfigSectionHandler subscriber);
135 
142  void
143  parse(const std::string& filename, bool isDryRun);
144 
152  void
153  parse(const std::string& input, bool isDryRun, const std::string& filename);
154 
161  void
162  parse(std::istream& input, bool isDryRun, const std::string& filename);
163 
170  void
171  parse(const ConfigSection& config, bool isDryRun, const std::string& filename);
172 
173 private:
174  void
175  process(bool isDryRun, const std::string& filename) const;
176 
177 private:
178  UnknownConfigSectionHandler m_unknownSectionCallback;
179  std::map<std::string, ConfigSectionHandler> m_subscriptions;
180  ConfigSection m_global;
181 };
182 
183 } // namespace nfd
184 
185 #endif // NFD_CORE_CONFIG_FILE_HPP
static bool parseYesNo(const ConfigSection &node, const std::string &key, const std::string &sectionName)
parse a config option that can be either "yes" or "no"
Definition: config-file.cpp:59
static bool parseYesNo(const ConfigSection::value_type &option, const std::string &sectionName)
Definition: config-file.hpp:97
configuration file parsing utility
Definition: config-file.hpp:58
STL namespace.
function< void(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)> UnknownConfigSectionHandler
callback to process a config file section without a ConfigSectionHandler
Definition: config-file.hpp:54
Error(const std::string &what)
Definition: config-file.hpp:65
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:40
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:76
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
Definition: config-file.hpp:41
boost::property_tree::ptree ConfigSection
a config file section
function< void(const ConfigSection &section, bool isDryRun, const std::string &filename)> ConfigSectionHandler
callback to process a config file section
Definition: config-file.hpp:47
static T parseNumber(const ConfigSection &node, const std::string &key, const std::string &sectionName)
parse a numeric (integral or floating point) config option
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback=throwErrorOnUnknownSection)
Definition: config-file.cpp:34
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:50
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:83
static T parseNumber(const ConfigSection::value_type &option, const std::string &sectionName)