Leosac  0.8.0
Open Source Access Control
PropertyTreeExtractor.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
21 #include "Colorize.hpp"
23 #include "log.hpp"
24 #include <boost/property_tree/ptree.hpp>
25 
26 using namespace Leosac::Tools;
27 
28 PropertyTreeExtractor::PropertyTreeExtractor(const boost::property_tree::ptree &tree,
29  const std::string &config_for)
30  : tree_(tree)
31  , text_(config_for)
32 {
33 }
34 
35 template <typename T>
36 T PropertyTreeExtractor::get(const std::string &node_name)
37 {
38  using namespace Colorize;
39  try
40  {
41  return tree_.get<T>(node_name);
42  }
43  catch (const boost::property_tree::ptree_bad_path &)
44  {
45  Ex::Config e(text_, node_name, true);
46  ERROR(e.what());
47  std::throw_with_nested(e);
48  }
49  catch (const boost::property_tree::ptree_bad_data &)
50  {
51  Ex::Config e(text_, node_name, false);
52  ERROR(e.what());
53  std::throw_with_nested(e);
54  }
55 }
56 
57 template <typename T>
58 T PropertyTreeExtractor::get(const std::string &node_name, const T &default_value)
59 {
60  return tree_.get<T>(node_name, default_value);
61 }
62 
63 // Instantiate the template for a few basic types.
64 
65 template std::string PropertyTreeExtractor::get<std::string>(const std::string &);
66 template int PropertyTreeExtractor::get<int>(const std::string &);
67 template uint64_t PropertyTreeExtractor::get<uint64_t>(const std::string &);
68 
69 // Ditto, but for accessor with default value.
70 
71 template std::string PropertyTreeExtractor::get<std::string>(const std::string &,
72  const std::string &);
73 template int PropertyTreeExtractor::get<int>(const std::string &, const int &);
74 template uint64_t PropertyTreeExtractor::get<uint64_t>(const std::string &,
75  const uint64_t &);
Leosac::Tools::PropertyTreeExtractor::PropertyTreeExtractor
PropertyTreeExtractor(const boost::property_tree::ptree &tree, const std::string &config_for)
Construct the extract.
Definition: PropertyTreeExtractor.cpp:28
LEOSACException::what
virtual const char * what() const noexcept final
Definition: leosacexception.hpp:53
Leosac::Tools::PropertyTreeExtractor::get
T get(const std::string &node_name)
Extract a mandatory property from the tree.
Definition: PropertyTreeExtractor.cpp:36
ERROR
@ ERROR
Definition: log.hpp:32
Leosac::Tools::PropertyTreeExtractor::text_
std::string text_
Definition: PropertyTreeExtractor.hpp:70
Colorize.hpp
PropertyTreeExtractor.hpp
configexception.hpp
log.hpp
Leosac::Tools::PropertyTreeExtractor::tree_
const boost::property_tree::ptree & tree_
Definition: PropertyTreeExtractor.hpp:69
Leosac::Ex::Config
An exception related to configuration.
Definition: configexception.hpp:33
Leosac::Tools
Definition: DatabaseLogSink.hpp:27