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-2018, 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_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 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  explicit
64  Error(const std::string& what)
65  : std::runtime_error(what)
66  {
67  }
68  };
69 
70  explicit
72 
73 public: // unknown section handlers
74  static void
75  throwErrorOnUnknownSection(const std::string& filename,
76  const std::string& sectionName,
77  const ConfigSection& section,
78  bool isDryRun);
79 
80  static void
81  ignoreUnknownSection(const std::string& filename,
82  const std::string& sectionName,
83  const ConfigSection& section,
84  bool isDryRun);
85 
86 public: // parse helpers
92  static bool
93  parseYesNo(const ConfigSection& node, const std::string& key, const std::string& sectionName);
94 
95  static bool
96  parseYesNo(const ConfigSection::value_type& option, const std::string& sectionName)
97  {
98  return parseYesNo(option.second, option.first, sectionName);
99  }
100 
108  template<typename T>
109  static T
110  parseNumber(const ConfigSection& node, const std::string& key, const std::string& sectionName)
111  {
112  static_assert(std::is_arithmetic<T>::value, "T must be an arithmetic type");
113 
114  boost::optional<T> value = node.get_value_optional<T>();
115  if (value) {
116  return *value;
117  }
118  BOOST_THROW_EXCEPTION(Error("Invalid value \"" + node.get_value<std::string>() +
119  "\" for option \"" + key + "\" in \"" + sectionName + "\" section"));
120  }
121 
122  template <typename T>
123  static T
124  parseNumber(const ConfigSection::value_type& option, const std::string& sectionName)
125  {
126  return parseNumber<T>(option.second, option.first, sectionName);
127  }
128 
129 public: // setup and parsing
131  void
132  addSectionHandler(const std::string& sectionName,
133  ConfigSectionHandler subscriber);
134 
141  void
142  parse(const std::string& filename, bool isDryRun);
143 
151  void
152  parse(const std::string& input, bool isDryRun, const std::string& filename);
153 
160  void
161  parse(std::istream& input, bool isDryRun, const std::string& filename);
162 
169  void
170  parse(const ConfigSection& config, bool isDryRun, const std::string& filename);
171 
172 private:
173  void
174  process(bool isDryRun, const std::string& filename) const;
175 
176 private:
177  UnknownConfigSectionHandler m_unknownSectionCallback;
178  std::map<std::string, ConfigSectionHandler> m_subscriptions;
179  ConfigSection m_global;
180 };
181 
182 } // namespace nfd
183 
184 #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:96
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
Error(const std::string &what)
Definition: config-file.hpp:64
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
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: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)