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-2020, 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 
127 public: // setup and parsing
129  void
130  addSectionHandler(const std::string& sectionName,
131  ConfigSectionHandler subscriber);
132 
139  void
140  parse(const std::string& filename, bool isDryRun);
141 
149  void
150  parse(const std::string& input, bool isDryRun, const std::string& filename);
151 
158  void
159  parse(std::istream& input, bool isDryRun, const std::string& filename);
160 
167  void
168  parse(const ConfigSection& config, bool isDryRun, const std::string& filename);
169 
170 private:
171  void
172  process(bool isDryRun, const std::string& filename) const;
173 
174 private:
175  UnknownConfigSectionHandler m_unknownSectionCallback;
176  std::map<std::string, ConfigSectionHandler> m_subscriptions;
177  ConfigSection m_global;
178 };
179 
180 } // namespace nfd
181 
182 #endif // NFD_DAEMON_COMMON_CONFIG_FILE_HPP
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:77
configuration file parsing utility
Definition: config-file.hpp:57
static T parseNumber(const ConfigSection::value_type &option, const std::string &sectionName)
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
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
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback=throwErrorOnUnknownSection)
Definition: config-file.cpp:35
static T parseNumber(const ConfigSection &node, const std::string &key, const std::string &sectionName)
parse a numeric (integral or floating point) config option
boost::property_tree::ptree ConfigSection
a config file section
Definition: config-file.hpp:37
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:84
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
static bool parseYesNo(const ConfigSection::value_type &option, const std::string &sectionName)
Definition: config-file.hpp:92
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:41
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:51