Embedded Template Library  1.0
parameter_pack.h
1 /******************************************************************************
2 The MIT License(MIT)
3 
4 Embedded Template Library.
5 https://github.com/ETLCPP/etl
6 https://www.etlcpp.com
7 
8 Copyright(c) 2017 jwellbelove
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files(the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions :
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 ******************************************************************************/
28 
29 #ifndef ETL_PARAMETER_PACK
30 #define ETL_PARAMETER_PACK
31 
32 #include <stdint.h>
33 
34 #include "platform.h"
35 #include "type_traits.h"
36 
37 #if ETL_CPP11_NOT_SUPPORTED
38  #error NOT SUPPORTED FOR C++03 OR BELOW
39 #else
40 
41 namespace etl
42 {
43  //***************************************************************************
45  //***************************************************************************
46  template <typename... TTypes>
47  class parameter_pack
48  {
49  public:
50 
51  static constexpr size_t size = sizeof...(TTypes);
52 
53  //***************************************************************************
55  //***************************************************************************
56  template <typename T>
57  class index_of_type
58  {
59  private:
60 
61  //***********************************
62  template <typename Type, typename T1, typename... TRest>
63  struct index_of_type_helper
64  {
65  static constexpr size_t value = std::is_same<Type, T1>::value ? 1 : 1 + index_of_type_helper<Type, TRest...>::value;
66  };
67 
68  //***********************************
69  template <typename Type, typename T1>
70  struct index_of_type_helper<Type, T1>
71  {
72  static constexpr size_t value = 1;
73  };
74 
75  public:
76 
77  static_assert(etl::is_one_of<T, TTypes...>::value, "T is not in parameter pack");
78 
80  static constexpr size_t value = index_of_type_helper<T, TTypes...>::value - 1;
81  };
82 
83 #if ETL_CPP17_SUPPORTED
84  template <typename T>
85  static constexpr size_t index_of_type_v = index_of_type<T>::value;
86 #endif
87 
88  //***************************************************************************
90  //***************************************************************************
91  template <size_t I>
92  class type_from_index
93  {
94  private:
95 
96  //***********************************
97  template <size_t II, size_t N, typename T1, typename... TRest>
98  struct type_from_index_helper
99  {
100  using type = typename std::conditional<II == N, T1, typename type_from_index_helper<II, N + 1, TRest...>::type>::type;
101  };
102 
103  //***********************************
104  template <size_t II, size_t N, typename T1>
105  struct type_from_index_helper<II, N, T1>
106  {
107  using type = T1;
108  };
109 
110  public:
111 
112  static_assert(I < sizeof...(TTypes), "Index out of bounds of parameter pack");
113 
115  using type = typename type_from_index_helper<I, 0, TTypes...>::type;
116  };
117 
118  //***********************************
119  template <size_t I>
120  using type_from_index_t = typename type_from_index<I>::type;
121  };
122 }
123 #endif
124 #endif
Definition: absolute.h:37
Definition: type_traits.h:1397