Embedded Template Library  1.0
smallest_generator.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 
10 Copyright(c) 2014 jwellbelove
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files(the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions :
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29 ******************************************************************************/
30 
31 /*[[[cog
32 import cog
33 cog.outl("#if 0")
34 ]]]*/
35 /*[[[end]]]*/
36 #error THIS HEADER IS A GENERATOR. DO NOT INCLUDE.
37 /*[[[cog
38 import cog
39 cog.outl("#endif")
40 ]]]*/
41 /*[[[end]]]*/
42 
43 /*[[[cog
44 import cog
45 cog.outl("//***************************************************************************")
46 cog.outl("// THIS FILE HAS BEEN AUTO GENERATED. DO NOT EDIT THIS FILE.")
47 cog.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 <stdint.h>
69 
70 #include "platform.h"
71 #include "integral_limits.h"
72 
75 
76 namespace etl
77 {
78 #if ETL_CPP11_SUPPORTED && !defined(ETL_SMALLEST_TYPE_FORCE_CPP03)
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_CPP14_SUPPORTED
126  template <typename... T>
127  using smallest_type_t = typename smallest_type<T...>::type;
128 #endif
129 
130 #if ETL_CPP17_SUPPORTED
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 <const 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 <const 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  //*************************************************************************
260  // Greater than 32 bits.
261  //*************************************************************************
262  template <>
264  {
265  typedef uint_least64_t type;
266  };
267 
268  //*************************************************************************
269  // Determine the type to hold the number of bits based on the index.
270  //*************************************************************************
271  template <const int index>
273 
274  //*************************************************************************
275  // Less than or equal to 8 bits.
276  //*************************************************************************
277  template <>
279  {
280  typedef int_least8_t type;
281  };
282 
283  //*************************************************************************
284  // 9 to 16 bits.
285  //*************************************************************************
286  template <>
288  {
289  typedef int_least16_t type;
290  };
291 
292  //*************************************************************************
293  // 17 to 31 bits.
294  //*************************************************************************
295  template <>
297  {
298  typedef int_least32_t type;
299  };
300 
301  //*************************************************************************
302  // Greater than 32 bits.
303  //*************************************************************************
304  template <>
306  {
307  typedef int_least64_t type;
308  };
309  }
310 
311  //***************************************************************************
316  //***************************************************************************
317  template <size_t NBITS>
319  {
320  private:
321 
322  // Determines the index of the best unsigned type for the required number of bits.
323  static const int TYPE_INDEX = ((NBITS > 8) ? 1 : 0) +
324  ((NBITS > 16) ? 1 : 0) +
325  ((NBITS > 32) ? 1 : 0);
326 
327  public:
328 
330  };
331 
332 #if ETL_CPP14_SUPPORTED
333  template <size_t NBITS>
334  using smallest_uint_for_bits_t = typename smallest_uint_for_bits<NBITS>::type;
335 #endif
336 
337  //***************************************************************************
342  //***************************************************************************
343  template <size_t NBITS>
345  {
346  private:
347 
348  // Determines the index of the best unsigned type for the required number of bits.
349  static const int TYPE_INDEX = ((NBITS > 8) ? 1 : 0) +
350  ((NBITS > 16) ? 1 : 0) +
351  ((NBITS > 32) ? 1 : 0);
352 
353  public:
354 
356  };
357 
358 #if ETL_CPP14_SUPPORTED
359  template <size_t NBITS>
360  using smallest_int_for_bits_t = typename smallest_int_for_bits<NBITS>::type;
361 #endif
362 
363  //***************************************************************************
368  //***************************************************************************
369  template <uintmax_t VALUE>
371  {
372  private:
373 
374  // Determines the index of the best unsigned type for the required value.
375  static const int TYPE_INDEX = ((VALUE > UINT_LEAST8_MAX) ? 1 : 0) +
376  ((VALUE > UINT16_MAX) ? 1 : 0) +
377  ((VALUE > UINT32_MAX) ? 1 : 0);
378 
379  public:
380 
382  };
383 
384 #if ETL_CPP14_SUPPORTED
385  template <uintmax_t VALUE>
386  using smallest_uint_for_value_t = typename smallest_uint_for_value<VALUE>::type;
387 #endif
388 
389  //***************************************************************************
394  //***************************************************************************
395  template <const intmax_t VALUE>
397  {
398  private:
399 
400  // Determines the index of the best signed type for the required value.
401  static const int TYPE_INDEX = (((VALUE > intmax_t(INT_LEAST8_MAX)) || (VALUE < intmax_t(INT_LEAST8_MIN))) ? 1 : 0) +
402  (((VALUE > intmax_t(INT16_MAX)) || (VALUE < intmax_t(INT16_MIN))) ? 1 : 0) +
403  (((VALUE > intmax_t(INT32_MAX)) || (VALUE < intmax_t(INT32_MIN))) ? 1 : 0);
404 
405  public:
406 
408  };
409 
410 #if ETL_CPP14_SUPPORTED
411  template <intmax_t VALUE>
412  using smallest_int_for_value_t = typename smallest_int_for_value<VALUE>::type;
413 #endif
414 }
415 
416 #endif
Template to determine the smallest signed int type that can contain a value with the specified number...
Definition: smallest_generator.h:345
Template to determine the smallest int type that can contain the specified signed value....
Definition: smallest_generator.h:397
Template to determine the smallest unsigned int type that can contain a value with the specified numb...
Definition: smallest_generator.h:319
Template to determine the smallest unsigned int type that can contain the specified unsigned value....
Definition: smallest_generator.h:371
conditional
Definition: type_traits_generator.h:1202
Definition: absolute.h:37
Definition: smallest_generator.h:272
Definition: smallest_generator.h:230
size_of
Definition: type_traits_generator.h:1572