Embedded Template Library  1.0
smallest.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 #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 <stdint.h>
57 
58 #include "platform.h"
59 #include "integral_limits.h"
60 
63 
64 namespace etl
65 {
66 #if ETL_CPP11_SUPPORTED && !defined(ETL_SMALLEST_TYPE_FORCE_CPP03)
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_CPP14_SUPPORTED
114  template <typename... T>
115  using smallest_type_t = typename smallest_type<T...>::type;
116 #endif
117 
118 #if ETL_CPP17_SUPPORTED
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 <const 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>
182  struct smallest_type<T1, void, void, void, void, void, void, void,
183  void, void, void, void, void, void, void, void>
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 <const 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  //*************************************************************************
230  // Greater than 32 bits.
231  //*************************************************************************
232  template <>
233  struct best_fit_uint_type<3>
234  {
235  typedef uint_least64_t type;
236  };
237 
238  //*************************************************************************
239  // Determine the type to hold the number of bits based on the index.
240  //*************************************************************************
241  template <const int index>
242  struct best_fit_int_type;
243 
244  //*************************************************************************
245  // Less than or equal to 8 bits.
246  //*************************************************************************
247  template <>
248  struct best_fit_int_type<0>
249  {
250  typedef int_least8_t type;
251  };
252 
253  //*************************************************************************
254  // 9 to 16 bits.
255  //*************************************************************************
256  template <>
257  struct best_fit_int_type<1>
258  {
259  typedef int_least16_t type;
260  };
261 
262  //*************************************************************************
263  // 17 to 31 bits.
264  //*************************************************************************
265  template <>
266  struct best_fit_int_type<2>
267  {
268  typedef int_least32_t type;
269  };
270 
271  //*************************************************************************
272  // Greater than 32 bits.
273  //*************************************************************************
274  template <>
275  struct best_fit_int_type<3>
276  {
277  typedef int_least64_t type;
278  };
279  }
280 
281  //***************************************************************************
286  //***************************************************************************
287  template <size_t NBITS>
288  struct smallest_uint_for_bits
289  {
290  private:
291 
292  // Determines the index of the best unsigned type for the required number of bits.
293  static const int TYPE_INDEX = ((NBITS > 8) ? 1 : 0) +
294  ((NBITS > 16) ? 1 : 0) +
295  ((NBITS > 32) ? 1 : 0);
296 
297  public:
298 
299  typedef typename private_smallest::best_fit_uint_type<TYPE_INDEX>::type type;
300  };
301 
302 #if ETL_CPP14_SUPPORTED
303  template <size_t NBITS>
304  using smallest_uint_for_bits_t = typename smallest_uint_for_bits<NBITS>::type;
305 #endif
306 
307  //***************************************************************************
312  //***************************************************************************
313  template <size_t NBITS>
314  struct smallest_int_for_bits
315  {
316  private:
317 
318  // Determines the index of the best unsigned type for the required number of bits.
319  static const int TYPE_INDEX = ((NBITS > 8) ? 1 : 0) +
320  ((NBITS > 16) ? 1 : 0) +
321  ((NBITS > 32) ? 1 : 0);
322 
323  public:
324 
325  typedef typename private_smallest::best_fit_int_type<TYPE_INDEX>::type type;
326  };
327 
328 #if ETL_CPP14_SUPPORTED
329  template <size_t NBITS>
330  using smallest_int_for_bits_t = typename smallest_int_for_bits<NBITS>::type;
331 #endif
332 
333  //***************************************************************************
338  //***************************************************************************
339  template <uintmax_t VALUE>
340  struct smallest_uint_for_value
341  {
342  private:
343 
344  // Determines the index of the best unsigned type for the required value.
345  static const int TYPE_INDEX = ((VALUE > UINT_LEAST8_MAX) ? 1 : 0) +
346  ((VALUE > UINT16_MAX) ? 1 : 0) +
347  ((VALUE > UINT32_MAX) ? 1 : 0);
348 
349  public:
350 
351  typedef typename private_smallest::best_fit_uint_type<TYPE_INDEX>::type type;
352  };
353 
354 #if ETL_CPP14_SUPPORTED
355  template <uintmax_t VALUE>
356  using smallest_uint_for_value_t = typename smallest_uint_for_value<VALUE>::type;
357 #endif
358 
359  //***************************************************************************
364  //***************************************************************************
365  template <const intmax_t VALUE>
366  struct smallest_int_for_value
367  {
368  private:
369 
370  // Determines the index of the best signed type for the required value.
371  static const int TYPE_INDEX = (((VALUE > intmax_t(INT_LEAST8_MAX)) || (VALUE < intmax_t(INT_LEAST8_MIN))) ? 1 : 0) +
372  (((VALUE > intmax_t(INT16_MAX)) || (VALUE < intmax_t(INT16_MIN))) ? 1 : 0) +
373  (((VALUE > intmax_t(INT32_MAX)) || (VALUE < intmax_t(INT32_MIN))) ? 1 : 0);
374 
375  public:
376 
377  typedef typename private_smallest::best_fit_int_type<TYPE_INDEX>::type type;
378  };
379 
380 #if ETL_CPP14_SUPPORTED
381  template <intmax_t VALUE>
382  using smallest_int_for_value_t = typename smallest_int_for_value<VALUE>::type;
383 #endif
384 }
385 
386 #endif
Definition: smallest.h:136
conditional
Definition: type_traits_generator.h:1202
Definition: absolute.h:37
size_of
Definition: type_traits_generator.h:1572