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