Nano
A C++ template metaprogramming library
containers.hpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------------------------------------
2 /// @file containers.hpp
3 /// @brief Header file for container metaclass to provide basic compile time containers
4 // ----------------------------------------------------------------------------------------------------------
5 
6 /*
7  * ----------------------------------------------------------------------------------------------------------
8  * containers header file for nano library.
9  * Copyright (C) 2015 Rob Clucas
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  * ----------------------------------------------------------------------------------------------------------
25  */
26 
27 #ifndef NANO_CONTAINERS_HPP
28 #define NANO_CONTAINERS_HPP
29 
30 #include <nano/list.hpp>
31 
32 #include <type_traits>
33 
34 namespace nano {
35 
36 // ----------------------------------------------------------------------------------------------------------
37 /// @struct pair
38 /// @brief Holds two elements
39 /// @tparam First The first eleement in the container
40 /// @tparam Second The second element in the container
41 // ----------------------------------------------------------------------------------------------------------
42 template <typename First, typename Second>
43 struct pair
44 {
45  using first = First;
46  using second = Second;
47 };
48 
49 namespace detail {
50 
51  // ------------------------------------------------------------------------------------------------------
52  /// @struct build_range
53  /// @brief Builds a range of nano::int_t types
54  /// @tparam Current The value to (maybe) add to the range
55  /// @tparam Step The increment size between successive elements
56  /// @tparam Iteration The current iteration of the build
57  /// @tparam Continue If the range must continue to be built, or if we are passed the end
58  /// @tparam Values The current values in the range
59  // ------------------------------------------------------------------------------------------------------
60  template <int Current, int Step, int Iteration, bool Continue, typename... Values>
61  struct build_range;
62 
63  // Case for when we arent passed the end
64  template <int Current, int Step, int Iteration, int... Values>
65  struct build_range<Current, Step, Iteration, true, list<nano::int_t<Values>...>>
66  {
67  static constexpr bool keep_building = Iteration >= 0;
68 
69  using index_list = typename std::conditional<
70  keep_building ,
72  list<nano::int_t<Values>...> // Don't add
73  >::type;
74 
75  using result = typename build_range<Current + Step , // Add Step to make next index
76  Step , // Same step
77  Iteration - 1 , // One less iteration
78  keep_building , // If we must add more indices
79  index_list >::result; // New list, and get result
80  };
81 
82  // Case for when we are pased the end
83  template <int Current, int Step, int Iteration, int... Values>
84  struct build_range<Current, Step, Iteration, false, list<nano::int_t<Values>...>>
85  {
87  };
88 
89 } // End namespace detail
90 
91 // ----------------------------------------------------------------------------------------------------------
92 /// @struct range
93 /// @brief Constructs a range of nano::int_t t types, which is essentially just a list of nano::int_t
94 /// types
95 /// @tparam Start The starting value of the range
96 /// @tparam End The end value of the range
97 /// @tparam Step The step size of range values
98 // ----------------------------------------------------------------------------------------------------------
99 template <int Start, int End, int Step>
100 struct range
101 {
102  static_assert( Start < End, "Invalid range parameters, Start must be less than End" );
103  static_assert( Step != 0 , "Invalid range parameters, Step cannot be 0" );
104 
105  using result = typename detail::build_range<Start ,
106  Step ,
107  (End - Start) / Step , // Calculate number of iterations
108  true , // Asserts passed so we can start
110 };
111 
112 } // End namespace nano
113 
114 #endif // NANO_CONTAINERS_HPP
Constructs a range of nano::int_t t types, which is essentially just a list of nano::int_t types...
Definition: containers.hpp:100
Header file for the list metaclass to provide compile time lists.
Wrapper around int for static int types used by metaclass and metafunctions in nano.
Definition: numeric_types.hpp:63
Holds two elements.
Definition: containers.hpp:43
Definition: containers.hpp:34
typename build_range< Current+Step, Step, Iteration-1, keep_building, index_list >::result result
Definition: containers.hpp:79
Meta class that holds types, and allows functions to be applied to the elements of the list using the...
Definition: list.hpp:51
Builds a range of nano::int_t types.
Definition: containers.hpp:61
typename detail::build_range< Start, Step,(End-Start)/Step, true, list<> >::result result
Definition: containers.hpp:109
First first
Definition: containers.hpp:45
Second second
Definition: containers.hpp:46
typename std::conditional< keep_building, list< nano::int_t< Values >..., nano::int_t< Current >>, list< nano::int_t< Values >...> >::type index_list
Definition: containers.hpp:73