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