NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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; -*- */
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 
35 typedef boost::property_tree::ptree ConfigSection;
36 
38 typedef function<void(const ConfigSection& /*section*/,
39  bool /*isDryRun*/,
40  const std::string& /*filename*/)> ConfigSectionHandler;
41 
43 typedef function<void(const std::string& /*filename*/,
44  const std::string& /*sectionName*/,
45  const ConfigSection& /*section*/,
46  bool /*isDryRun*/)> UnknownConfigSectionHandler;
47 
48 class ConfigFile : noncopyable
49 {
50 public:
51  class Error : public std::runtime_error
52  {
53  public:
54  explicit
55  Error(const std::string& what)
56  : std::runtime_error(what)
57  {
58  }
59  };
60 
61  explicit
62  ConfigFile(UnknownConfigSectionHandler unknownSectionCallback = throwErrorOnUnknownSection);
63 
64  static void
65  throwErrorOnUnknownSection(const std::string& filename,
66  const std::string& sectionName,
67  const ConfigSection& section,
68  bool isDryRun);
69 
70  static void
71  ignoreUnknownSection(const std::string& filename,
72  const std::string& sectionName,
73  const ConfigSection& section,
74  bool isDryRun);
75 
82  static bool
83  parseYesNo(const ConfigSection::value_type& option,
84  const std::string& sectionName);
85 
92  template <typename T>
93  static typename std::enable_if<std::is_arithmetic<T>::value, T>::type
94  parseNumber(const ConfigSection::value_type& option,
95  const std::string& sectionName);
96 
98  void
99  addSectionHandler(const std::string& sectionName,
100  ConfigSectionHandler subscriber);
101 
108  void
109  parse(const std::string& filename, bool isDryRun);
110 
118  void
119  parse(const std::string& input, bool isDryRun, const std::string& filename);
120 
127  void
128  parse(std::istream& input, bool isDryRun, const std::string& filename);
129 
136  void
137  parse(const ConfigSection& config, bool isDryRun, const std::string& filename);
138 
139 private:
140  void
141  process(bool isDryRun, const std::string& filename) const;
142 
143 private:
144  UnknownConfigSectionHandler m_unknownSectionCallback;
145  std::map<std::string, ConfigSectionHandler> m_subscriptions;
146  ConfigSection m_global;
147 };
148 
149 template <typename T>
150 inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
151 ConfigFile::parseNumber(const ConfigSection::value_type& option,
152  const std::string& sectionName)
153 {
154  auto value = option.second.get_value<std::string>();
155 
156  try {
157  return boost::lexical_cast<T>(value);
158  }
159  catch (const boost::bad_lexical_cast&) {
160  BOOST_THROW_EXCEPTION(Error("Invalid value \"" + value + "\" for option \"" +
161  option.first + "\" in \"" + sectionName + "\" section"));
162  }
163 }
164 
165 } // namespace nfd
166 
167 #endif // NFD_CORE_CONFIG_FILE_HPP
function< void(const ConfigSection &, bool, const std::string &)> ConfigSectionHandler
callback for config file sections
Definition: config-file.hpp:40
STL namespace.
Error(const std::string &what)
Definition: config-file.hpp:55
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:79
function< void(const std::string &, const std::string &, const ConfigSection &, bool)> UnknownConfigSectionHandler
callback for config file sections without a subscribed handler
Definition: config-file.hpp:46
boost::property_tree::ptree ConfigSection
static std::enable_if< std::is_arithmetic< T >::value, T >::type parseNumber(const ConfigSection::value_type &option, const std::string &sectionName)
parse a numeric (integral or floating point) config option
static bool parseYesNo(const ConfigSection::value_type &option, const std::string &sectionName)
parse a config option that can be either "yes" or "no"
Definition: config-file.cpp:62
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:53
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:86