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-2021, 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  // Unsigned logic is workaround for https://redmine.named-data.net/issues/4489
112  if (value &&
113  (std::is_signed<T>() || node.get_value<std::string>().find("-") == std::string::npos)) {
114  return *value;
115  }
116  NDN_THROW(Error("Invalid value '" + node.get_value<std::string>() +
117  "' for option '" + key + "' in section '" + sectionName + "'"));
118  }
119 
120  template<typename T>
121  static T
122  parseNumber(const ConfigSection::value_type& option, const std::string& sectionName)
123  {
124  return parseNumber<T>(option.second, option.first, sectionName);
125  }
126 
131  template<typename T>
132  static void
133  checkRange(T value, T min, T max, const std::string& key, const std::string& sectionName)
134  {
135  static_assert(std::is_integral<T>::value, "T must be an integral type");
136 
137  if (value < min || value > max) {
138  NDN_THROW(Error("Invalid value '" + to_string(value) + "' for option '" + key +
139  "' in section '" + sectionName + "': out of acceptable range [" +
140  to_string(min) + ", " + to_string(max) + "]"));
141  }
142  }
143 
144 public: // setup and parsing
146  void
147  addSectionHandler(const std::string& sectionName,
148  ConfigSectionHandler subscriber);
149 
156  void
157  parse(const std::string& filename, bool isDryRun);
158 
166  void
167  parse(const std::string& input, bool isDryRun, const std::string& filename);
168 
175  void
176  parse(std::istream& input, bool isDryRun, const std::string& filename);
177 
184  void
185  parse(const ConfigSection& config, bool isDryRun, const std::string& filename);
186 
187 private:
188  void
189  process(bool isDryRun, const std::string& filename) const;
190 
191 private:
192  UnknownConfigSectionHandler m_unknownSectionCallback;
193  std::map<std::string, ConfigSectionHandler> m_subscriptions;
194  ConfigSection m_global;
195 };
196 
197 } // namespace nfd
198 
199 #endif // NFD_DAEMON_COMMON_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:60
std::string to_string(const T &val)
Definition: backports.hpp:86
static bool parseYesNo(const ConfigSection::value_type &option, const std::string &sectionName)
Definition: config-file.hpp:92
static void checkRange(T value, T min, T max, const std::string &key, const std::string &sectionName)
check that a value is within the inclusive range [min, max]
configuration file parsing utility
Definition: config-file.hpp:57
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
Definition: config-file.hpp:41
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
#define NDN_THROW(e)
Definition: exception.hpp:61
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:41
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:39
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:77
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
boost::property_tree::ptree ConfigSection
a config file section
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:35
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:51
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:84
static T parseNumber(const ConfigSection::value_type &option, const std::string &sectionName)