Embedded Template Library 1.0
Loading...
Searching...
No Matches
smallest.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#if 0
32#error THIS HEADER IS A GENERATOR. DO NOT INCLUDE.
33#endif
34
35//***************************************************************************
36// THIS FILE HAS BEEN AUTO GENERATED. DO NOT EDIT THIS FILE.
37//***************************************************************************
38
39//***************************************************************************
40// To generate to header file, run this at the command line.
41// Note: You will need Python and COG installed.
42//
43// python -m cogapp -d -e -osmallest.h -DNTypes=<n> smallest_generator.h
44// Where <n> is the number of types to support.
45//
46// e.g.
47// To generate handlers for up to 16 types...
48// python -m cogapp -d -e -osmallest.h -DNTypes=16 smallest_generator.h
49//
50// See generate.bat
51//***************************************************************************
52
53#ifndef ETL_SMALLEST_INCLUDED
54#define ETL_SMALLEST_INCLUDED
55
56#include "platform.h"
57#include "integral_limits.h"
58
59#include <stdint.h>
60
63
64namespace etl
65{
66#if ETL_USING_CPP11 && !defined(ETL_SMALLEST_TYPE_FORCE_CPP03_IMPLEMENTATION)
67 //***************************************************************************
72 //***************************************************************************
73 template <typename T1, typename... TRest>
74 class smallest_type
75 {
76 private:
77
78 // Define 'smallest_other' as 'smallest_type' with all but the first parameter.
79 using smallest_other = typename smallest_type<TRest...>::type;
80
81 public:
82
83 // Set 'type' to be the smallest of the first parameter and any of the others.
84 // This is recursive.
86 T1, // TrueType
87 smallest_other> // FalseType
88 ::type; // The smallest type of the two.
89
90 // The size of the smallest type.
91 enum
92 {
94 };
95 };
96
97 //***************************************************************************
98 // Specialisation for one template parameter.
99 //***************************************************************************
100 template <typename T1>
101 class smallest_type<T1>
102 {
103 public:
104
105 using type = T1;
106
107 enum
108 {
110 };
111 };
112
113#if ETL_USING_CPP11
114 template <typename... T>
115 using smallest_type_t = typename smallest_type<T...>::type;
116#endif
117
118#if ETL_USING_CPP17
119 template <typename... T>
120 constexpr size_t smallest_type_v = smallest_type<T...>::size;
121#endif
122
123#else
124 //***************************************************************************
130 //***************************************************************************
131 template <typename T1, typename T2 = void, typename T3 = void, typename T4 = void,
132 typename T5 = void, typename T6 = void, typename T7 = void, typename T8 = void,
133 typename T9 = void, typename T10 = void, typename T11 = void, typename T12 = void,
134 typename T13 = void, typename T14 = void, typename T15 = void, typename T16 = void>
136 {
137 private:
138
139 // Declaration.
140 template <bool Boolean, typename TrueType, typename FalseType>
141 struct choose_type;
142
143 // Specialisation for 'true'.
144 // Defines 'type' as 'TrueType'.
145 template <typename TrueType, typename FalseType>
146 struct choose_type<true, TrueType, FalseType>
147 {
148 typedef TrueType type;
149 };
150
151 // Specialisation for 'false'.
152 // Defines 'type' as 'FalseType'.
153 template <typename TrueType, typename FalseType>
154 struct choose_type<false, TrueType, FalseType>
155 {
156 typedef FalseType type;
157 };
158
159 public:
160
161 // Define 'smallest_other' as 'smallest_type' with all but the first parameter.
162 typedef typename smallest_type<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>::type smallest_other;
163
164 // Set 'type' to be the smallest of the first parameter and any of the others.
165 // This is recursive.
166 typedef typename choose_type<(sizeof(T1) < sizeof(smallest_other)), // Boolean
167 T1, // TrueType
168 smallest_other> // FalseType
169 ::type type; // The smallest type of the two.
170
171 // The size of the smallest type.
172 enum
173 {
174 size = sizeof(type)
175 };
176 };
177
178 //***************************************************************************
179 // Specialisation for one template parameter.
180 //***************************************************************************
181 template <typename T1>
184 {
185 typedef T1 type;
186
187 enum
188 {
189 size = sizeof(type)
190 };
191 };
192#endif
193
194 namespace private_smallest
195 {
196 //*************************************************************************
197 // Determine the type to hold the number of bits based on the index.
198 //*************************************************************************
199 template <int index>
200 struct best_fit_uint_type;
201
202 //*************************************************************************
203 // Less than or equal to 8 bits.
204 //*************************************************************************
205 template <>
206 struct best_fit_uint_type<0>
207 {
208 typedef uint_least8_t type;
209 };
210
211 //*************************************************************************
212 // 9 to 16 bits.
213 //*************************************************************************
214 template <>
215 struct best_fit_uint_type<1>
216 {
217 typedef uint_least16_t type;
218 };
219
220 //*************************************************************************
221 // 17 to 31 bits.
222 //*************************************************************************
223 template <>
224 struct best_fit_uint_type<2>
225 {
226 typedef uint_least32_t type;
227 };
228
229#if ETL_USING_64BIT_TYPES
230 //*************************************************************************
231 // Greater than 32 bits.
232 //*************************************************************************
233 template <>
235 {
236 typedef uint_least64_t type;
237 };
238#endif
239
240 //*************************************************************************
241 // Determine the type to hold the number of bits based on the index.
242 //*************************************************************************
243 template <int index>
244 struct best_fit_int_type;
245
246 //*************************************************************************
247 // Less than or equal to 8 bits.
248 //*************************************************************************
249 template <>
250 struct best_fit_int_type<0>
251 {
252 typedef int_least8_t type;
253 };
254
255 //*************************************************************************
256 // 9 to 16 bits.
257 //*************************************************************************
258 template <>
259 struct best_fit_int_type<1>
260 {
261 typedef int_least16_t type;
262 };
263
264 //*************************************************************************
265 // 17 to 31 bits.
266 //*************************************************************************
267 template <>
268 struct best_fit_int_type<2>
269 {
270 typedef int_least32_t type;
271 };
272
273#if ETL_USING_64BIT_TYPES
274 //*************************************************************************
275 // Greater than 32 bits.
276 //*************************************************************************
277 template <>
279 {
280 typedef int_least64_t type;
281 };
282#endif
283 }
284
285 //***************************************************************************
290 //***************************************************************************
291 template <size_t NBITS>
293 {
294 private:
295
296 // Determines the index of the best unsigned type for the required number of bits.
297 static ETL_CONSTANT int TYPE_INDEX = ((NBITS > 8) ? 1 : 0) +
298 ((NBITS > 16) ? 1 : 0) +
299 ((NBITS > 32) ? 1 : 0);
300
301 public:
302
304 };
305
306 template <size_t NBITS>
307 ETL_CONSTANT int smallest_uint_for_bits<NBITS>::TYPE_INDEX;
308
309#if ETL_USING_CPP11
310 template <size_t NBITS>
311 using smallest_uint_for_bits_t = typename smallest_uint_for_bits<NBITS>::type;
312#endif
313
314 //***************************************************************************
319 //***************************************************************************
320 template <size_t NBITS>
321 struct smallest_int_for_bits
322 {
323 private:
324
325 // Determines the index of the best unsigned type for the required number of bits.
326 static ETL_CONSTANT int TYPE_INDEX = ((NBITS > 8) ? 1 : 0) +
327 ((NBITS > 16) ? 1 : 0) +
328 ((NBITS > 32) ? 1 : 0);
329
330 public:
331
332 typedef typename private_smallest::best_fit_int_type<TYPE_INDEX>::type type;
333 };
334
335 template <size_t NBITS>
336 ETL_CONSTANT int smallest_int_for_bits<NBITS>::TYPE_INDEX;
337
338#if ETL_USING_CPP11
339 template <size_t NBITS>
340 using smallest_int_for_bits_t = typename smallest_int_for_bits<NBITS>::type;
341#endif
342
343 //***************************************************************************
348 //***************************************************************************
349 template <uintmax_t VALUE>
350 struct smallest_uint_for_value
351 {
352 private:
353
354 // Determines the index of the best unsigned type for the required value.
355 static ETL_CONSTANT int TYPE_INDEX = ((VALUE > UINT_LEAST8_MAX) ? 1 : 0) +
356 ((VALUE > UINT16_MAX) ? 1 : 0) +
357 ((VALUE > UINT32_MAX) ? 1 : 0);
358
359 public:
360
361 typedef typename private_smallest::best_fit_uint_type<TYPE_INDEX>::type type;
362 };
363
364 template <uintmax_t VALUE>
365 ETL_CONSTANT int smallest_uint_for_value<VALUE>::TYPE_INDEX;
366
367#if ETL_USING_CPP11
368 template <uintmax_t VALUE>
369 using smallest_uint_for_value_t = typename smallest_uint_for_value<VALUE>::type;
370#endif
371
372 //***************************************************************************
377 //***************************************************************************
378 template <intmax_t VALUE>
379 struct smallest_int_for_value
380 {
381 private:
382
383 // Determines the index of the best signed type for the required value.
384 static ETL_CONSTANT int TYPE_INDEX = (((VALUE > intmax_t(INT_LEAST8_MAX)) || (VALUE < intmax_t(INT_LEAST8_MIN))) ? 1 : 0) +
385 (((VALUE > intmax_t(INT16_MAX)) || (VALUE < intmax_t(INT16_MIN))) ? 1 : 0) +
386 (((VALUE > intmax_t(INT32_MAX)) || (VALUE < intmax_t(INT32_MIN))) ? 1 : 0);
387
388 public:
389
390 typedef typename private_smallest::best_fit_int_type<TYPE_INDEX>::type type;
391 };
392
393 template <intmax_t VALUE>
394 ETL_CONSTANT int smallest_int_for_value<VALUE>::TYPE_INDEX;
395
396#if ETL_USING_CPP11
397 template <intmax_t VALUE>
398 using smallest_int_for_value_t = typename smallest_int_for_value<VALUE>::type;
399#endif
400}
401
402#endif
Definition smallest.h:136
Template to determine the smallest unsigned int type that can contain a value with the specified numb...
Definition smallest_generator.h:323
conditional
Definition type_traits_generator.h:1160
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
Definition smallest_generator.h:274
Definition smallest_generator.h:230
size_of
Definition type_traits_generator.h:1597