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-2022, 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 
38 using ConfigSection = boost::property_tree::ptree;
39 
43 using OptionalConfigSection = boost::optional<const ConfigSection&>;
44 
48 using ConfigSectionHandler = std::function<void(const ConfigSection& section, bool isDryRun,
49  const std::string& filename)>;
50 
54 using UnknownConfigSectionHandler = std::function<void(const std::string& filename,
55  const std::string& sectionName,
56  const ConfigSection& section,
57  bool isDryRun)>;
58 
62 class ConfigFile : noncopyable
63 {
64 public:
65  class Error : public std::runtime_error
66  {
67  public:
68  using std::runtime_error::runtime_error;
69  };
70 
71  explicit
73 
74 public: // unknown section handlers
75  static void
76  throwErrorOnUnknownSection(const std::string& filename,
77  const std::string& sectionName,
78  const ConfigSection& section,
79  bool isDryRun);
80 
81  static void
82  ignoreUnknownSection(const std::string& filename,
83  const std::string& sectionName,
84  const ConfigSection& section,
85  bool isDryRun);
86 
87 public: // parse helpers
93  static bool
94  parseYesNo(const ConfigSection& node, const std::string& key, const std::string& sectionName);
95 
96  static bool
97  parseYesNo(const ConfigSection::value_type& option, const std::string& sectionName)
98  {
99  return parseYesNo(option.second, option.first, sectionName);
100  }
101 
109  template<typename T>
110  static T
111  parseNumber(const ConfigSection& node, const std::string& key, const std::string& sectionName)
112  {
113  static_assert(std::is_arithmetic_v<T>);
114 
115  auto value = node.get_value_optional<T>();
116  // Unsigned logic is workaround for https://redmine.named-data.net/issues/4489
117  if (value &&
118  (std::is_signed<T>() || node.get_value<std::string>().find("-") == std::string::npos)) {
119  return *value;
120  }
121  NDN_THROW(Error("Invalid value '" + node.get_value<std::string>() +
122  "' for option '" + key + "' in section '" + sectionName + "'"));
123  }
124 
125  template<typename T>
126  static T
127  parseNumber(const ConfigSection::value_type& option, const std::string& sectionName)
128  {
129  return parseNumber<T>(option.second, option.first, sectionName);
130  }
131 
136  template<typename T>
137  static void
138  checkRange(T value, T min, T max, const std::string& key, const std::string& sectionName)
139  {
140  static_assert(std::is_integral_v<T>);
141 
142  if (value < min || value > max) {
143  NDN_THROW(Error("Invalid value '" + to_string(value) + "' for option '" + key +
144  "' in section '" + sectionName + "': out of acceptable range [" +
145  to_string(min) + ", " + to_string(max) + "]"));
146  }
147  }
148 
149 public: // setup and parsing
151  void
152  addSectionHandler(const std::string& sectionName,
153  ConfigSectionHandler subscriber);
154 
161  void
162  parse(const std::string& filename, bool isDryRun);
163 
171  void
172  parse(const std::string& input, bool isDryRun, const std::string& filename);
173 
180  void
181  parse(std::istream& input, bool isDryRun, const std::string& filename);
182 
189  void
190  parse(const ConfigSection& config, bool isDryRun, const std::string& filename);
191 
192 private:
193  void
194  process(bool isDryRun, const std::string& filename) const;
195 
196 private:
197  UnknownConfigSectionHandler m_unknownSectionCallback;
198  std::map<std::string, ConfigSectionHandler> m_subscriptions;
199  ConfigSection m_global;
200 };
201 
202 } // namespace nfd
203 
204 #endif // NFD_DAEMON_COMMON_CONFIG_FILE_HPP
Configuration file parsing utility.
Definition: config-file.hpp:63
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)
Definition: config-file.hpp:97
Definition: common.hpp:77
boost::optional< const ConfigSection & > OptionalConfigSection
An optional configuration file section.
Definition: config-file.hpp:43
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:57
boost::property_tree::ptree ConfigSection
A configuration file section.
Definition: config-file.hpp:38
std::function< void(const ConfigSection &section, bool isDryRun, const std::string &filename)> ConfigSectionHandler
Callback to process a configuration file section.
Definition: config-file.hpp:49