blob: f6e4aaf65352a4f0a28f30f45c663b40b501cddc [file] [log] [blame]
Steve DiBenedettobb75b552014-02-08 12:12:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento1d7e7af2015-10-10 23:54:08 +02003 * Copyright (c) 2014-2015, 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020024 */
Steve DiBenedettobb75b552014-02-08 12:12:11 -070025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_CORE_CONFIG_FILE_HPP
27#define NFD_CORE_CONFIG_FILE_HPP
Steve DiBenedettobb75b552014-02-08 12:12:11 -070028
29#include "common.hpp"
30
31#include <boost/property_tree/ptree.hpp>
32
33namespace nfd {
34
35typedef boost::property_tree::ptree ConfigSection;
36
37/// \brief callback for config file sections
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060038typedef function<void(const ConfigSection& /*section*/,
39 bool /*isDryRun*/,
40 const std::string& /*filename*/)> ConfigSectionHandler;
41
42/// \brief callback for config file sections without a subscribed handler
43typedef function<void(const std::string& /*filename*/,
44 const std::string& /*sectionName*/,
45 const ConfigSection& /*section*/,
46 bool /*isDryRun*/)> UnknownConfigSectionHandler;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070047
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060048class ConfigFile : noncopyable
Steve DiBenedettobb75b552014-02-08 12:12:11 -070049{
50public:
Steve DiBenedettobb75b552014-02-08 12:12:11 -070051 class Error : public std::runtime_error
52 {
53 public:
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060054 explicit
Steve DiBenedettobb75b552014-02-08 12:12:11 -070055 Error(const std::string& what)
56 : std::runtime_error(what)
57 {
Steve DiBenedettobb75b552014-02-08 12:12:11 -070058 }
59 };
60
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020061 explicit
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060062 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);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070075
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020076 /**
77 * \brief parse a config option that can be either "yes" or "no"
Yanbiao Li698f4fe2015-08-19 16:30:16 -070078 *
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020079 * \return true if "yes", false if "no"
80 * \throw Error the value is neither "yes" nor "no"
Yanbiao Li698f4fe2015-08-19 16:30:16 -070081 */
82 static bool
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020083 parseYesNo(const ConfigSection::value_type& option,
Yanbiao Li698f4fe2015-08-19 16:30:16 -070084 const std::string& sectionName);
85
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020086 /**
87 * \brief parse a numeric (integral or floating point) config option
88 *
89 * \return the numeric value of the parsed option
90 * \throw Error the value cannot be converted to the specified type
91 */
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
Steve DiBenedettobb75b552014-02-08 12:12:11 -070097 /// \brief setup notification of configuration file sections
98 void
99 addSectionHandler(const std::string& sectionName,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600100 ConfigSectionHandler subscriber);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700101
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700102 /**
103 * \param filename file to parse
104 * \param isDryRun true if performing a dry run of configuration, false otherwise
105 * \throws ConfigFile::Error if file not found
106 * \throws ConfigFile::Error if parse error
107 */
108 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600109 parse(const std::string& filename, bool isDryRun);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700110
111 /**
112 * \param input configuration (as a string) to parse
113 * \param isDryRun true if performing a dry run of configuration, false otherwise
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800114 * \param filename logical filename of the config file, can appear in error messages
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700115 * \throws ConfigFile::Error if file not found
116 * \throws ConfigFile::Error if parse error
117 */
118 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600119 parse(const std::string& input, bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700120
121 /**
122 * \param input stream to parse
123 * \param isDryRun true if performing a dry run of configuration, false otherwise
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800124 * \param filename logical filename of the config file, can appear in error messages
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700125 * \throws ConfigFile::Error if parse error
126 */
127 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600128 parse(std::istream& input, bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700129
Alexander Afanasyevc0273e32015-01-06 13:05:50 -0800130 /**
131 * \param config ConfigSection that needs to be processed
132 * \param isDryRun true if performing a dry run of configuration, false otherwise
133 * \param filename logical filename of the config file, can appear in error messages
134 * \throws ConfigFile::Error if parse error
135 */
136 void
137 parse(const ConfigSection& config, bool isDryRun, const std::string& filename);
138
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700139private:
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700140 void
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200141 process(bool isDryRun, const std::string& filename) const;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700142
143private:
Steve DiBenedetto34c95f72014-04-17 20:56:00 -0600144 UnknownConfigSectionHandler m_unknownSectionCallback;
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200145 std::map<std::string, ConfigSectionHandler> m_subscriptions;
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700146 ConfigSection m_global;
147};
148
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200149template <typename T>
150inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
151ConfigFile::parseNumber(const ConfigSection::value_type& option,
152 const std::string& sectionName)
153{
154 auto value = option.second.get_value<std::string>();
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700155
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200156 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
Steve DiBenedettobb75b552014-02-08 12:12:11 -0700166
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700167#endif // NFD_CORE_CONFIG_FILE_HPP