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 
35 typedef boost::property_tree::ptree ConfigSection;
36 
38 typedef function<void(const ConfigSection& /*section*/,
39  bool /*isDryRun*/,
40  const std::string& /*filename*/)> ConfigSectionHandler;
41 
43 typedef function<void(const std::string& /*filename*/,
44  const std::string& /*sectionName*/,
45  const ConfigSection& /*section*/,
46  bool /*isDryRun*/)> UnknownConfigSectionHandler;
47 
50 class ConfigFile : noncopyable
51 {
52 public:
53  class Error : public std::runtime_error
54  {
55  public:
56  explicit
57  Error(const std::string& what)
58  : std::runtime_error(what)
59  {
60  }
61  };
62 
63  explicit
64  ConfigFile(UnknownConfigSectionHandler unknownSectionCallback = throwErrorOnUnknownSection);
65 
66 public: // unknown section handlers
67  static void
68  throwErrorOnUnknownSection(const std::string& filename,
69  const std::string& sectionName,
70  const ConfigSection& section,
71  bool isDryRun);
72 
73  static void
74  ignoreUnknownSection(const std::string& filename,
75  const std::string& sectionName,
76  const ConfigSection& section,
77  bool isDryRun);
78 
79 public: // parse helpers
85  static bool
86  parseYesNo(const ConfigSection& node, const std::string& key, const std::string& sectionName);
87 
88  static bool
89  parseYesNo(const ConfigSection::value_type& option, const std::string& sectionName)
90  {
91  return parseYesNo(option.second, option.first, sectionName);
92  }
93 
101  template<typename T>
102  static T
103  parseNumber(const ConfigSection& node, const std::string& key, const std::string& sectionName)
104  {
105  static_assert(std::is_arithmetic<T>::value, "T must be an arithmetic type");
106 
107  boost::optional<T> value = node.get_value_optional<T>();
108  if (value) {
109  return *value;
110  }
111  BOOST_THROW_EXCEPTION(Error("Invalid value \"" + node.get_value<std::string>() +
112  "\" for option \"" + key + "\" in \"" + sectionName + "\" section"));
113  }
114 
115  template <typename T>
116  static T
117  parseNumber(const ConfigSection::value_type& option, const std::string& sectionName)
118  {
119  return parseNumber<T>(option.second, option.first, sectionName);
120  }
121 
122 public: // setup and parsing
124  void
125  addSectionHandler(const std::string& sectionName,
126  ConfigSectionHandler subscriber);
127 
134  void
135  parse(const std::string& filename, bool isDryRun);
136 
144  void
145  parse(const std::string& input, bool isDryRun, const std::string& filename);
146 
153  void
154  parse(std::istream& input, bool isDryRun, const std::string& filename);
155 
162  void
163  parse(const ConfigSection& config, bool isDryRun, const std::string& filename);
164 
165 private:
166  void
167  process(bool isDryRun, const std::string& filename) const;
168 
169 private:
170  UnknownConfigSectionHandler m_unknownSectionCallback;
171  std::map<std::string, ConfigSectionHandler> m_subscriptions;
172  ConfigSection m_global;
173 };
174 
175 } // namespace nfd
176 
177 #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:62
static bool parseYesNo(const ConfigSection::value_type &option, const std::string &sectionName)
Definition: config-file.hpp:89
configuration file parsing utility
Definition: config-file.hpp:50
function< void(const ConfigSection &, bool, const std::string &)> ConfigSectionHandler
callback for config file sections
Definition: config-file.hpp:40
STL namespace.
Error(const std::string &what)
Definition: config-file.hpp:57
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:79
function< void(const std::string &, const std::string &, const ConfigSection &, bool)> UnknownConfigSectionHandler
callback for config file sections without a subscribed handler
Definition: config-file.hpp:46
boost::property_tree::ptree ConfigSection
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:53
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:86
static T parseNumber(const ConfigSection::value_type &option, const std::string &sectionName)