Embedded Template Library 1.0
Loading...
Searching...
No Matches
largest.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2014 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_LARGEST_INCLUDED
32#define ETL_LARGEST_INCLUDED
33
36
37#include "platform.h"
38#include "smallest.h"
39#include "static_assert.h"
40#include "type_traits.h"
41
42namespace etl
43{
44#if ETL_USING_CPP11 && !defined(ETL_LARGEST_TYPE_FORCE_CPP03_IMPLEMENTATION)
45 //***************************************************************************
50 //***************************************************************************
51 template <typename T1, typename... TRest>
52 class largest_type
53 {
54 private:
55
56 // Define 'largest_other' as 'largest_type' with all but the first
57 // parameter.
58 using largest_other = typename largest_type<TRest...>::type;
59
60 public:
61
62 // Set 'type' to be the largest of the first parameter and any of the
63 // others. This is recursive.
64 using type = typename etl::conditional< (etl::size_of<T1>::value > etl::size_of<largest_other>::value), // Boolean
65 T1, // TrueType
66 largest_other> // FalseType
67 ::type; // The largest type of the two.
69 // The size of the largest type.
70 enum
71 {
72 size = etl::size_of<type>::value
73 };
74 };
75
76 //***************************************************************************
77 // Specialisation for one template parameter.
78 //***************************************************************************
79 template <typename T1>
80 class largest_type<T1>
81 {
82 public:
83
84 using type = T1;
85
86 enum
87 {
88 size = etl::size_of<type>::value
89 };
90 };
91
92 #if ETL_USING_CPP11
93 template <typename... T>
94 using largest_type_t = typename largest_type<T...>::type;
95 #endif
96
97 #if ETL_USING_CPP17
98 template <typename... T>
99 constexpr size_t largest_type_v = largest_type<T...>::size;
100 #endif
101
102#else
103 #include "private/largest_type_cpp03.h"
104#endif
105
106#if ETL_USING_CPP11 && !defined(ETL_LARGEST_ALIGNMENT_FORCE_CPP03_IMPLEMENTATION)
107 //***************************************************************************
111 //***************************************************************************
112 template <typename T1, typename... TRest>
113 struct largest_alignment
114 {
115 // Define 'largest_other' as 'largest_type' with all but the first
116 // parameter.
117 using largest_other = typename largest_alignment<TRest...>::type;
118
119 // Set 'type' to be the largest of the first parameter and any of the
120 // others. This is recursive.
121 using type = typename etl::conditional< (etl::alignment_of<T1>::value > etl::alignment_of<largest_other>::value), // Boolean
122 T1, // TrueType
123 largest_other> // FalseType
124 ::type; // The largest type of the two.
125
126 // The largest alignment.
127 enum
128 {
129 value = etl::alignment_of<type>::value
130 };
131 };
132
133 //***************************************************************************
134 // Specialisation for one template parameter.
135 //***************************************************************************
136 template <typename T1>
137 struct largest_alignment<T1>
138 {
139 typedef T1 type;
140
141 enum
142 {
143 value = etl::alignment_of<type>::value
144 };
145 };
146
147 #if ETL_USING_CPP17
148 template <typename... T>
149 inline constexpr size_t largest_alignment_v = largest_alignment<T...>::value;
150 #endif
151
152#else
153 #include "private/largest_alignment_cpp03.h"
154#endif
155
156 //***************************************************************************
160 //***************************************************************************
161 template <typename T>
163 {
164 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Must be an integral type");
165
167 };
168
169#if ETL_USING_CPP11
170 template <typename T>
171 using larger_int_type_t = typename larger_int_type<T>::type;
172#endif
173
174 //***************************************************************************
178 //***************************************************************************
179 template <typename T>
181 {
182 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Must be an integral type");
183
185 };
186
187#if ETL_USING_CPP11
188 template <typename T>
189 using larger_uint_type_t = typename larger_uint_type<T>::type;
190#endif
191
192 //***************************************************************************
197 //***************************************************************************
198 template <typename T, bool IS_SIGNED = etl::is_signed<T>::value>
200
201 template <typename T>
202 struct larger_type<T, false>
203 {
204 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Must be an integral type");
205
206 typedef typename etl::smallest_uint_for_bits<etl::integral_limits<T>::bits + 1>::type type;
207 };
208
209 template <typename T>
210 struct larger_type<T, true>
211 {
212 ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Must be an integral type");
213
214 typedef typename etl::smallest_int_for_bits<etl::integral_limits<T>::bits + 1>::type type;
215 };
216
217#if ETL_USING_CPP11
218 template <typename T>
219 using larger_type_t = typename larger_type<T>::type;
220#endif
221
222#if ETL_USING_CPP11 && !defined(ETL_LARGEST_FORCE_CPP03_IMPLEMENTATION)
223 //***************************************************************************
228 //***************************************************************************
229 template <typename... T>
230 struct largest
231 {
232 using type = typename etl::largest_type<T...>::type;
233
234 enum
235 {
237 alignment = etl::largest_alignment<T...>::value
238 };
239 };
240
241 #if ETL_USING_CPP11
242 template <typename... T>
243 using largest_t = typename largest<T...>::type;
244 #endif
245
246 #if ETL_USING_CPP17
247 template <typename... T>
248 inline constexpr size_t largest_size = largest<T...>::size;
249 #endif
250
251#else
252 #include "private/largest_cpp03.h"
253#endif
254} // namespace etl
255
256#endif
Definition largest.h:199
Definition largest.h:181
Definition largest.h:45
Definition largest.h:45
Definition largest.h:46
Definition largest_type_cpp03.h:45
Definition smallest.h:233
Definition smallest.h:205
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR TContainer::size_type size(const TContainer &container)
Definition iterator.h:1192
\ingroup largest
Definition largest.h:163