NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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; -*- */
2 /*
3  * Copyright (c) 2014-2019, Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #ifndef NFD_DAEMON_COMMON_CONFIG_FILE_HPP
27 #define NFD_DAEMON_COMMON_CONFIG_FILE_HPP
28 
29 #include "core/common.hpp"
30 
31 #include <boost/property_tree/ptree.hpp>
32 
33 namespace nfd {
34 
37 using ConfigSection = boost::property_tree::ptree;
38 
41 using OptionalConfigSection = boost::optional<const ConfigSection&>;
42 
45 using ConfigSectionHandler = std::function<void(const ConfigSection& section, bool isDryRun,
46  const std::string& filename)>;
47 
50 using UnknownConfigSectionHandler = std::function<void(const std::string& filename,
51  const std::string& sectionName,
52  const ConfigSection& section,
53  bool isDryRun)>;
54 
57 class ConfigFile : noncopyable
58 {
59 public:
60  class Error : public std::runtime_error
61  {
62  public:
63  using std::runtime_error::runtime_error;
64  };
65 
66  explicit
68 
69 public: // unknown section handlers
70  static void
71  throwErrorOnUnknownSection(const std::string& filename,
72  const std::string& sectionName,
73  const ConfigSection& section,
74  bool isDryRun);
75 
76  static void
77  ignoreUnknownSection(const std::string& filename,
78  const std::string& sectionName,
79  const ConfigSection& section,
80  bool isDryRun);
81 
82 public: // parse helpers
88  static bool
89  parseYesNo(const ConfigSection& node, const std::string& key, const std::string& sectionName);
90 
91  static bool
92  parseYesNo(const ConfigSection::value_type& option, const std::string& sectionName)
93  {
94  return parseYesNo(option.second, option.first, sectionName);
95  }
96 
104  template<typename T>
105  static T
106  parseNumber(const ConfigSection& node, const std::string& key, const std::string& sectionName)
107  {
108  static_assert(std::is_arithmetic<T>::value, "T must be an arithmetic type");
109 
110  boost::optional<T> value = node.get_value_optional<T>();
111  if (value) {
112  return *value;
113  }
114  NDN_THROW(Error("Invalid value '" + node.get_value<std::string>() +
115  "' for option '" + key + "' in section '" + sectionName + "'"));
116  }
117 
118  template <typename T>
119  static T
120  parseNumber(const ConfigSection::value_type& option, const std::string& sectionName)
121  {
122  return parseNumber<T>(option.second, option.first, sectionName);
123  }
124 
125 public: // setup and parsing
127  void
128  addSectionHandler(const std::string& sectionName,
129  ConfigSectionHandler subscriber);
130 
137  void
138  parse(const std::string& filename, bool isDryRun);
139 
147  void
148  parse(const std::string& input, bool isDryRun, const std::string& filename);
149 
156  void
157  parse(std::istream& input, bool isDryRun, const std::string& filename);
158 
165  void
166  parse(const ConfigSection& config, bool isDryRun, const std::string& filename);
167 
168 private:
169  void
170  process(bool isDryRun, const std::string& filename) const;
171 
172 private:
173  UnknownConfigSectionHandler m_unknownSectionCallback;
174  std::map<std::string, ConfigSectionHandler> m_subscriptions;
175  ConfigSection m_global;
176 };
177 
178 } // namespace nfd
179 
180 #endif // NFD_DAEMON_COMMON_CONFIG_FILE_HPP
nfd::UnknownConfigSectionHandler
std::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:53
nfd::ConfigFile::parseYesNo
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:60
nfd::ConfigFile::ignoreUnknownSection
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:51
nfd::ConfigFile::parseYesNo
static bool parseYesNo(const ConfigSection::value_type &option, const std::string &sectionName)
Definition: config-file.hpp:92
nfd::ConfigSectionHandler
std::function< void(const ConfigSection &section, bool isDryRun, const std::string &filename)> ConfigSectionHandler
callback to process a config file section
Definition: config-file.hpp:46
nfd::ConfigFile::parseNumber
static T parseNumber(const ConfigSection::value_type &option, const std::string &sectionName)
Definition: config-file.hpp:120
nfd::ConfigFile::addSectionHandler
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:77
nfd::OptionalConfigSection
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
Definition: config-file.hpp:41
common.hpp
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
nfd::ConfigFile
configuration file parsing utility
Definition: config-file.hpp:58
nfd::ConfigFile::parse
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:84
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
nfd::ConfigFile::throwErrorOnUnknownSection
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:41
nfd::ConfigSection
boost::property_tree::ptree ConfigSection
a config file section
Definition: ndn-l3-protocol.hpp:39
nfd::ConfigFile::ConfigFile
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback=throwErrorOnUnknownSection)
Definition: config-file.cpp:35
nfd::ConfigFile::Error
Definition: config-file.hpp:61
nfd::ConfigFile::parseNumber
static T parseNumber(const ConfigSection &node, const std::string &key, const std::string &sectionName)
parse a numeric (integral or floating point) config option
Definition: config-file.hpp:106