blob: dd5a43c66963ba8e3b6200911656e15edce49d50 [file] [log] [blame]
Steve DiBenedettobb75b552014-02-08 12:12:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NFD_MGMT_CONFIG_FILE_HPP
8#define NFD_MGMT_CONFIG_FILE_HPP
9
10#include "common.hpp"
11
12#include <boost/property_tree/ptree.hpp>
13
14namespace nfd {
15
16typedef boost::property_tree::ptree ConfigSection;
17
18/// \brief callback for config file sections
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060019typedef function<void(const ConfigSection&, bool, const std::string&)> ConfigSectionHandler;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070020
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060021class ConfigFile : noncopyable
Steve DiBenedettobb75b552014-02-08 12:12:11 -070022{
23public:
24
25 class Error : public std::runtime_error
26 {
27 public:
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060028 explicit
Steve DiBenedettobb75b552014-02-08 12:12:11 -070029 Error(const std::string& what)
30 : std::runtime_error(what)
31 {
32
33 }
34 };
35
36 ConfigFile();
37
38 /// \brief setup notification of configuration file sections
39 void
40 addSectionHandler(const std::string& sectionName,
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060041 ConfigSectionHandler subscriber);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070042
43
44 /**
45 * \param filename file to parse
46 * \param isDryRun true if performing a dry run of configuration, false otherwise
47 * \throws ConfigFile::Error if file not found
48 * \throws ConfigFile::Error if parse error
49 */
50 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060051 parse(const std::string& filename, bool isDryRun);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070052
53 /**
54 * \param input configuration (as a string) to parse
55 * \param isDryRun true if performing a dry run of configuration, false otherwise
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060056 * \param filename optional convenience argument to provide more detailed error messages
Steve DiBenedettobb75b552014-02-08 12:12:11 -070057 * \throws ConfigFile::Error if file not found
58 * \throws ConfigFile::Error if parse error
59 */
60 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060061 parse(const std::string& input, bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070062
63 /**
64 * \param input stream to parse
65 * \param isDryRun true if performing a dry run of configuration, false otherwise
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060066 * \param filename optional convenience argument to provide more detailed error messages
Steve DiBenedettobb75b552014-02-08 12:12:11 -070067 * \throws ConfigFile::Error if parse error
68 */
69 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060070 parse(std::istream& input, bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070071
72private:
73
74 void
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060075 process(bool isDryRun, const std::string& filename);
Steve DiBenedettobb75b552014-02-08 12:12:11 -070076
77private:
78
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -060079 typedef std::map<std::string, ConfigSectionHandler> SubscriptionTable;
Steve DiBenedettobb75b552014-02-08 12:12:11 -070080
81 SubscriptionTable m_subscriptions;
82
83 ConfigSection m_global;
84};
85
86} // namespace nfd
87
88
89#endif // NFD_MGMT_CONFIG_FILE_HPP