Stride Reference Manual  - generated for commit 9643b11
PtreeUtils.h
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2017, 2018 Willem L, Kuylen E, Stijven S & Broeckhove J
14  *
15  * This code is adapted for this project from code by
16  * Renato Florentino Garcia. His copyright also applies.
17  * Copyright (C) 2011 Renato Florentino Garcia
18  *
19  * Distributed under the Boost Software License, Version 1.0. (See
20  * accompanying file BOOST_LICENSE_1_0.txt or copy at
21  * http://www.boost.org/LICENSE_1_0.txt)
22  * For more information, see http://www.boost.org
23  */
24 
25 #pragma once
26 
27 //#include <boost/foreach.hpp>
28 #include <boost/iterator/transform_iterator.hpp>
29 #include <boost/property_tree/ptree.hpp>
30 #include <algorithm>
31 #include <array>
32 #include <functional>
33 #include <stdexcept>
34 
35 namespace stride {
36 namespace util {
37 
38 namespace detail {
39 
40 template <class T, class Ptree>
41 struct pair2data : public std::unary_function<const typename Ptree::value_type, T>
42 {
43  T operator()(const typename Ptree::value_type& pair) const { return pair.second.template get_value<T>(); }
44 };
45 
46 } // namespace detail
47 
48 template <class Sequence>
49 Sequence ToSequence(const boost::property_tree::ptree& ptree)
50 {
51  namespace bpt = boost::property_tree;
53  using Iterator = boost::iterators::transform_iterator<Concrete_pair2data, bpt::ptree::const_iterator>;
54 
55  const Iterator begin(ptree.begin(), Concrete_pair2data());
56  const Iterator end(ptree.end(), Concrete_pair2data());
57  return Sequence(begin, end);
58 }
59 
60 template <class T, std::size_t N>
61 std::array<T, N> ToArray(const boost::property_tree::ptree& ptree)
62 {
63  namespace bpt = boost::property_tree;
64  using Concrete_pair2data = detail::pair2data<T, bpt::ptree>;
65  using Iterator = boost::iterators::transform_iterator<Concrete_pair2data, bpt::ptree::const_iterator>;
66 
67  if (ptree.size() != N) {
68  throw std::range_error("Array size error.");
69  }
70  const Iterator begin(ptree.begin(), Concrete_pair2data());
71  const Iterator end(ptree.end(), Concrete_pair2data());
72  std::array<T, N> tmpArray;
73  std::copy(begin, end, tmpArray.begin());
74  return tmpArray;
75 }
76 
77 template <class Ptree>
78 Ptree Merge(const Ptree& pt1, const Ptree& pt2)
79 {
80  Ptree result;
81  for (const typename Ptree::value_type& v : pt1) {
82  result.add_child(v.first, v.second);
83  }
84  for (const typename Ptree::value_type& v : pt2) {
85  result.add_child(v.first, v.second);
86  }
87  return result;
88 }
89 
90 } // namespace util
91 } // namespace stride
Sequence ToSequence(const boost::property_tree::ptree &ptree)
Definition: PtreeUtils.h:49
Ptree Merge(const Ptree &pt1, const Ptree &pt2)
Definition: PtreeUtils.h:78
std::array< T, N > ToArray(const boost::property_tree::ptree &ptree)
Definition: PtreeUtils.h:61
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
T operator()(const typename Ptree::value_type &pair) const
Definition: PtreeUtils.h:43