Nano
A C++ template metaprogramming library
runtime_converter.hpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------------------------------------
2 /// @file runtime_converter.hpp
3 /// @brief Header file for the runtime_converter metaclass which can return runtime containers (like
4 /// std::vector) from nano containers (mainly lists)
5 // ----------------------------------------------------------------------------------------------------------
6 
7 /*
8  * ----------------------------------------------------------------------------------------------------------
9  * runtime_converter header file for nano library.
10  * Copyright (C) 2015 Rob Clucas
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  * ----------------------------------------------------------------------------------------------------------
26  */
27 
28 #ifndef NANO_RUNTIME_CONVERTER_HPP
29 #define NANO_RUNTIME_CONVERTER_HPP
30 
31 #include <nano/list.hpp>
32 
33 #include <array>
34 #include <vector>
35 #include <type_traits>
36 
37 namespace nano {
38 
39  // For 'behind the scenes' or more involved implementations
40  namespace detail {
41 
42  // --------------------------------------------------------------------------------------------------
43  /// @struct array_convert
44  /// @brief Converts an element to a std type, like nano::int_t to inr or a nano::list into a
45  /// std::array with N elements
46  /// @tparam Elements The element(s) to convert to a std type or std::array
47  // --------------------------------------------------------------------------------------------------
48  template <typename Element>
50  {
51  using type = typename Element::type;
52 
53  static constexpr type result() { return Element::value; };
54  };
55 
56  // Specialization
57  template <typename First, typename... Rest>
58  struct array_convert<list<First, Rest...>>
59  {
60  static constexpr std::size_t size = sizeof...(Rest) + 1;
61 
62  using type = typename std::array<typename First::type, size>;
63 
64  static constexpr type result() {
65  return type{ {First::value, Rest::value...} };
66  }
67  };
68 
69  // --------------------------------------------------------------------------------------------------
70  /// @struct vector_convert
71  /// @brief Same as convert, but uses vector's instead of arrays, so the performance is worse
72  /// @tparam Element The element(s) to convert to a std type or std::vector
73  // --------------------------------------------------------------------------------------------------
74  template <typename Element>
76  {
77  using type = typename Element::type;
78 
79  static constexpr type result() { return Element::value; };
80  };
81 
82  // Specialization for lists
83  template <typename First, typename... Rest>
84  struct vector_convert<list<First, Rest...>>
85  {
86  using type = typename std::vector<typename First::type>;
87 
88  static constexpr type result() {
89  return type{ {First::value, Rest::value...} };
90  }
91  };
92  }
93 
94  // -------------------------------------------------------------------------------------------------------
95  /// @struct runtime_converter
96  /// @brief Wrapper class to provide the conversion functions for converting to runtime containers
97  /// @tparam List The nano::list from which to create the runtime container
98  // -------------------------------------------------------------------------------------------------------
99  template <typename List>
101 
102  // Specialization for list inputs
103  template <typename Head, typename... Tail>
104  struct runtime_converter<list<Head, Tail...>>
105  {
106  // Number of elements in the external array - for readability of below function
107  static constexpr std::size_t num_elements = sizeof...(Tail) + 1;
108 
110  using array_type = typename std::array<array_internal_type, num_elements>;
111 
112  // --------------------------------------------------------------------------------------------------
113  /// @brief Converts a nano::list to a std::array, if the nano::list just contains elements. If
114  /// the nano::list contains nano::lists, then a std::array of std::array's is created
115  /// @return A std::array of elements, where the element type depends on the elements of the
116  /// nano::list from which it is created.
117  // --------------------------------------------------------------------------------------------------
118  static constexpr array_type to_array() {
121  }
122 
124  using vector_type = typename std::vector<vector_internal_type>;
125 
126  // --------------------------------------------------------------------------------------------------
127  /// @brief Converts a nano::list to a std::vector, if the nano::list just contains elements. If
128  /// the nano::list contains nano::lists, then a std::vector of std::vector's is created.
129  /// @return A std::vector of elements, where the element type depends on the elements of the
130  /// nano::list from which it is created.
131  // --------------------------------------------------------------------------------------------------
132  static constexpr vector_type to_vector() {
135  }
136  };
137 
138 } // End namespace nano
139 
140 #endif // NANO_RUNTIME_VECTOR_HPP
141 
static constexpr type result()
Definition: runtime_converter.hpp:88
Header file for the list metaclass to provide compile time lists.
static constexpr type result()
Definition: runtime_converter.hpp:79
static constexpr array_type to_array()
Converts a nano::list to a std::array, if the nano::list just contains elements. If the nano::list co...
Definition: runtime_converter.hpp:118
Wrapper class to provide the conversion functions for converting to runtime containers.
Definition: runtime_converter.hpp:100
static constexpr type result()
Definition: runtime_converter.hpp:53
Definition: containers.hpp:34
static constexpr vector_type to_vector()
Converts a nano::list to a std::vector, if the nano::list just contains elements. If the nano::list c...
Definition: runtime_converter.hpp:132
Same as convert, but uses vector's instead of arrays, so the performance is worse.
Definition: runtime_converter.hpp:75
typename std::array< typename First::type, size > type
Definition: runtime_converter.hpp:62
typename detail::vector_convert< Head >::type vector_internal_type
Definition: runtime_converter.hpp:123
typename Element::type type
Definition: runtime_converter.hpp:77
typename std::vector< vector_internal_type > vector_type
Definition: runtime_converter.hpp:124
Converts an element to a std type, like nano::int_t to inr or a nano::list into a std::array with N e...
Definition: runtime_converter.hpp:49
Meta class that holds types, and allows functions to be applied to the elements of the list using the...
Definition: list.hpp:51
typename detail::array_convert< Head >::type array_internal_type
Definition: runtime_converter.hpp:109
typename std::array< array_internal_type, num_elements > array_type
Definition: runtime_converter.hpp:110
static constexpr type result()
Definition: runtime_converter.hpp:64
typename std::vector< typename First::type > type
Definition: runtime_converter.hpp:86
typename Element::type type
Definition: runtime_converter.hpp:51