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 <stddef.h>
36
37#if ETL_CPP11_SUPPORTED
38namespace etl
39{
40 //***************************************************************************
42 //***************************************************************************
43 template <typename... TTypes>
44 class parameter_pack
45 {
46 public:
47
48 static constexpr size_t size = sizeof...(TTypes);
49
50 //***************************************************************************
52 //***************************************************************************
53 template <typename T>
54 class index_of_type
55 {
56 private:
57
58 //***********************************
59 template <typename Type, typename T1, typename... TRest>
60 struct index_of_type_helper
61 {
62 static constexpr size_t value = etl::is_same<Type, T1>::value ? 1 : 1 + index_of_type_helper<Type, TRest...>::value;
63 };
64
65 //***********************************
66 template <typename Type, typename T1>
67 struct index_of_type_helper<Type, T1>
68 {
69 static constexpr size_t value = 1;
70 };
71
72 public:
73
74 static_assert(etl::is_one_of<T, TTypes...>::value, "T is not in parameter pack");
75
77 static constexpr size_t value = index_of_type_helper<T, TTypes...>::value - 1;
78 };
79
80 #if ETL_USING_CPP17
81 template <typename T>
82 static constexpr size_t index_of_type_v = index_of_type<T>::value;
83 #endif
84
85 //***************************************************************************
87 //***************************************************************************
88 template <size_t Index>
89 class type_from_index
90 {
91 private:
92
93 //***********************************
94 template <size_t Desired_Index, size_t Current_Index, typename T1, typename... TRest>
95 struct type_from_index_helper
96 {
97 using type = typename etl::conditional< Desired_Index == Current_Index, T1,
98 typename type_from_index_helper<Desired_Index, Current_Index + 1, TRest...>::type>::type;
99 };
100
101 //***********************************
102 template <size_t Desired_Index, size_t Current_Index, typename T1>
103 struct type_from_index_helper<Desired_Index, Current_Index, T1>
104 {
105 using type = T1;
106 };
107
108 public:
109
110 static_assert(Index < sizeof...(TTypes), "Index out of bounds of parameter pack");
111
113 using type = typename type_from_index_helper<Index, 0, TTypes...>::type;
114 };
115
116 //***********************************
117 template <size_t Index>
118 using type_from_index_t = typename type_from_index<Index>::type;
119 };
120
121 //***********************************
122 template <size_t Index, typename... TTypes>
123 using parameter_pack_t = typename etl::parameter_pack<TTypes...>::template type_from_index_t<Index>;
124
125 //***********************************
126 template <typename... TTypes>
127 constexpr size_t parameter_pack<TTypes...>::size;
128
129 #if ETL_USING_CPP17
130 template <typename T, typename... TTypes>
131 inline constexpr size_t parameter_pack_v = etl::parameter_pack<TTypes...>::template index_of_type<T>::value;
132 #endif
133
134 #if ETL_USING_CPP17 && !ETL_USING_GCC_COMPILER && !ETL_USING_CLANG_COMPILER
135 //***********************************
136 template <typename... TTypes>
137 template <typename T>
138 constexpr size_t parameter_pack<TTypes...>::template index_of_type<T>::value;
139 #else
140 //***********************************
141 template <typename... TTypes>
142 template <typename T>
143 constexpr size_t parameter_pack<TTypes...>::index_of_type<T>::value;
144 #endif
145} // namespace etl
146#endif
147#endif
Definition absolute.h:40
ETL_CONSTEXPR TContainer::size_type size(const TContainer &container)
Definition iterator.h:1434