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 Index>
93 class type_from_index
94 {
95 private:
96
97 //***********************************
98 template <size_t Desired_Index, size_t Current_Index, typename T1, typename... TRest>
99 struct type_from_index_helper
100 {
101 using type = typename etl::conditional< Desired_Index == Current_Index, T1,
102 typename type_from_index_helper<Desired_Index, Current_Index + 1, TRest...>::type>::type;
103 };
104
105 //***********************************
106 template <size_t Desired_Index, size_t Current_Index, typename T1>
107 struct type_from_index_helper<Desired_Index, Current_Index, T1>
108 {
109 using type = T1;
110 };
111
112 public:
113
114 static_assert(Index < sizeof...(TTypes), "Index out of bounds of parameter pack");
115
117 using type = typename type_from_index_helper<Index, 0, TTypes...>::type;
118 };
119
120 //***********************************
121 template <size_t Index>
122 using type_from_index_t = typename type_from_index<Index>::type;
123 };
124
125 //***********************************
126 template <size_t Index, typename... TTypes>
127 using parameter_pack_t = typename etl::parameter_pack<TTypes...>::template type_from_index_t<Index>;
128
129 //***********************************
130 template <typename... TTypes>
131 constexpr size_t parameter_pack<TTypes...>::size;
132
133 #if ETL_USING_CPP17
134 template <typename T, typename... TTypes>
135 inline constexpr size_t parameter_pack_v = etl::parameter_pack<TTypes...>::template index_of_type<T>::value;
136 #endif
137
138 #if ETL_USING_CPP17 && !ETL_USING_GCC_COMPILER && !ETL_USING_CLANG_COMPILER
139 //***********************************
140 template <typename... TTypes>
141 template <typename T>
142 constexpr size_t parameter_pack<TTypes...>::template index_of_type<T>::value;
143 #else
144 //***********************************
145 template <typename... TTypes>
146 template <typename T>
147 constexpr size_t parameter_pack<TTypes...>::index_of_type<T>::value;
148 #endif
149} // namespace etl
150#endif
151#endif
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR TContainer::size_type size(const TContainer &container)
Definition iterator.h:1192