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-2024, 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 #include <functional>
34 #include <map>
35 
36 namespace nfd {
37 
41 using ConfigSection = boost::property_tree::ptree;
42 
46 using OptionalConfigSection = boost::optional<const ConfigSection&>;
47 
51 using ConfigSectionHandler = std::function<void(const ConfigSection& section, bool isDryRun,
52  const std::string& filename)>;
53 
57 using UnknownConfigSectionHandler = std::function<void(const std::string& filename,
58  const std::string& sectionName,
59  const ConfigSection& section,
60  bool isDryRun)>;
61 
65 class ConfigFile : noncopyable
66 {
67 public:
68  class Error : public std::runtime_error
69  {
70  public:
71  using std::runtime_error::runtime_error;
72  };
73 
74  explicit
76 
77 public: // unknown section handlers
78  static void
79  throwErrorOnUnknownSection(const std::string& filename,
80  const std::string& sectionName,
81  const ConfigSection& section,
82  bool isDryRun);
83 
84  static void
85  ignoreUnknownSection(const std::string& filename,
86  const std::string& sectionName,
87  const ConfigSection& section,
88  bool isDryRun);
89 
90 public: // parse helpers
96  static bool
97  parseYesNo(const ConfigSection& node, const std::string& key, const std::string& sectionName);
98 
99  static bool
100  parseYesNo(const ConfigSection::value_type& option, const std::string& sectionName)
101  {
102  return parseYesNo(option.second, option.first, sectionName);
103  }
104 
112  template<typename T>
113  static T
114  parseNumber(const ConfigSection& node, const std::string& key, const std::string& sectionName)
115  {
116  static_assert(std::is_arithmetic_v<T>);
117 
118  auto value = node.get_value_optional<T>();
119  // Unsigned logic is workaround for https://redmine.named-data.net/issues/4489
120  if (value &&
121  (std::is_signed_v<T> || node.get_value<std::string>().find("-") == std::string::npos)) {
122  return *value;
123  }
124  NDN_THROW(Error("Invalid value '" + node.get_value<std::string>() +
125  "' for option '" + key + "' in section '" + sectionName + "'"));
126  }
127 
128  template<typename T>
129  static T
130  parseNumber(const ConfigSection::value_type& option, const std::string& sectionName)
131  {
132  return parseNumber<T>(option.second, option.first, sectionName);
133  }
134 
139  template<typename T>
140  static void
141  checkRange(T value, T min, T max, const std::string& key, const std::string& sectionName)
142  {
143  static_assert(std::is_integral_v<T>);
144 
145  if (value < min || value > max) {
146  NDN_THROW(Error("Invalid value '" + std::to_string(value) + "' for option '" + key +
147  "' in section '" + sectionName + "': out of acceptable range [" +
148  std::to_string(min) + ", " + std::to_string(max) + "]"));
149  }
150  }
151 
152 public: // setup and parsing
154  void
155  addSectionHandler(const std::string& sectionName,
156  ConfigSectionHandler subscriber);
157 
164  void
165  parse(const std::string& filename, bool isDryRun);
166 
174  void
175  parse(const std::string& input, bool isDryRun, const std::string& filename);
176 
183  void
184  parse(std::istream& input, bool isDryRun, const std::string& filename);
185 
192  void
193  parse(const ConfigSection& config, bool isDryRun, const std::string& filename);
194 
195 private:
196  void
197  process(bool isDryRun, const std::string& filename) const;
198 
199 private:
200  UnknownConfigSectionHandler m_unknownSectionCallback;
201  std::map<std::string, ConfigSectionHandler> m_subscriptions;
202  ConfigSection m_global;
203 };
204 
205 } // namespace nfd
206 
207 #endif // NFD_DAEMON_COMMON_CONFIG_FILE_HPP
Configuration file parsing utility.
Definition: config-file.hpp:66
static T parseNumber(const ConfigSection &node, const std::string &key, const std::string &sectionName)
Parse a numeric (integral or floating point) config option.
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:41
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:84
ConfigFile(UnknownConfigSectionHandler unknownSectionCallback=throwErrorOnUnknownSection)
Definition: config-file.cpp:35
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].
static T parseNumber(const ConfigSection::value_type &option, const std::string &sectionName)
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:51
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
Setup notification of configuration file sections.
Definition: config-file.cpp:77
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
static bool parseYesNo(const ConfigSection::value_type &option, const std::string &sectionName)
-status-http-server
Definition: common.hpp:71
boost::optional< const ConfigSection & > OptionalConfigSection
An optional configuration file section.
Definition: config-file.hpp:46
std::function< void(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)> UnknownConfigSectionHandler
Callback to process a configuration file section without a ConfigSectionHandler.
Definition: config-file.hpp:60
boost::property_tree::ptree ConfigSection
A configuration file section.
Definition: config-file.hpp:41
std::function< void(const ConfigSection &section, bool isDryRun, const std::string &filename)> ConfigSectionHandler
Callback to process a configuration file section.
Definition: config-file.hpp:52