blob: f3128c41439b4445c1b57a1c7b97a655c0f64013 [file] [log] [blame]
Steve DiBenedettobb75b552014-02-08 12:12:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Steve DiBenedettobb75b552014-02-08 12:12:11 -070024
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070025#ifndef NFD_CORE_CONFIG_FILE_HPP
26#define NFD_CORE_CONFIG_FILE_HPP
Steve DiBenedettobb75b552014-02-08 12:12:11 -070027
28#include "common.hpp"
29
30#include <boost/property_tree/ptree.hpp>
31
32namespace nfd {
33
34typedef boost::property_tree::ptree ConfigSection;
35
36/// \brief callback for config file sections
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060037typedef function<void(const ConfigSection&, bool, const std::string&)> ConfigSectionHandler;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070038
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060039class ConfigFile : noncopyable
Steve DiBenedettobb75b552014-02-08 12:12:11 -070040{
41public:
42
43 class Error : public std::runtime_error
44 {
45 public:
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060046 explicit
Steve DiBenedettobb75b552014-02-08 12:12:11 -070047 Error(const std::string& what)
48 : std::runtime_error(what)
49 {
50
51 }
52 };
53
54 ConfigFile();
55
56 /// \brief setup notification of configuration file sections
57 void
58 addSectionHandler(const std::string& sectionName,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060059 ConfigSectionHandler subscriber);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070060
61
62 /**
63 * \param filename file to parse
64 * \param isDryRun true if performing a dry run of configuration, false otherwise
65 * \throws ConfigFile::Error if file not found
66 * \throws ConfigFile::Error if parse error
67 */
68 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060069 parse(const std::string& filename, bool isDryRun);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070070
71 /**
72 * \param input configuration (as a string) to parse
73 * \param isDryRun true if performing a dry run of configuration, false otherwise
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060074 * \param filename optional convenience argument to provide more detailed error messages
Steve DiBenedettobb75b552014-02-08 12:12:11 -070075 * \throws ConfigFile::Error if file not found
76 * \throws ConfigFile::Error if parse error
77 */
78 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060079 parse(const std::string& input, bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070080
81 /**
82 * \param input stream to parse
83 * \param isDryRun true if performing a dry run of configuration, false otherwise
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060084 * \param filename optional convenience argument to provide more detailed error messages
Steve DiBenedettobb75b552014-02-08 12:12:11 -070085 * \throws ConfigFile::Error if parse error
86 */
87 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060088 parse(std::istream& input, bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070089
90private:
91
92 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060093 process(bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070094
95private:
96
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060097 typedef std::map<std::string, ConfigSectionHandler> SubscriptionTable;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070098
99 SubscriptionTable m_subscriptions;
100
101 ConfigSection m_global;
102};
103
104} // namespace nfd
105
106
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700107#endif // NFD_CORE_CONFIG_FILE_HPP