Embedded Template Library  1.0
largest_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 -olargest.h -DNTypes=<n> largest_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 -olargest.h -DNTypes=16 largest_generator.h
61 //
62 // See generate.bat
63 //***************************************************************************
64 
65 #ifndef ETL_LARGEST_INCLUDED
66 #define ETL_LARGEST_INCLUDED
67 
70 
71 #include "platform.h"
72 #include "type_traits.h"
73 #include "smallest.h"
74 #include "static_assert.h"
75 
76 namespace etl
77 {
78 #if ETL_CPP11_SUPPORTED && !defined(ETL_LARGEST_TYPE_FORCE_CPP03)
79  //***************************************************************************
84  //***************************************************************************
85  template <typename T1, typename... TRest>
86  class largest_type
87  {
88  private:
89 
90  // Define 'largest_other' as 'largest_type' with all but the first parameter.
91  using largest_other = typename largest_type<TRest...>::type;
92 
93  public:
94 
95  // Set 'type' to be the largest of the first parameter and any of the others.
96  // This is recursive.
98  T1, // TrueType
99  largest_other> // FalseType
100  ::type; // The largest type of the two.
101 
102  // The size of the largest type.
103  enum
104  {
106  };
107  };
108 
109  //***************************************************************************
110  // Specialisation for one template parameter.
111  //***************************************************************************
112  template <typename T1>
113  class largest_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 largest_type_t = typename largest_type<T...>::type;
128 #endif
129 
130 #if ETL_CPP17_SUPPORTED
131  template <typename... T>
132  constexpr size_t largest_type_v = largest_type<T...>::size;
133 #endif
134 
135 #else
136  /*[[[cog
137  import cog
138  cog.outl("//***************************************************************************")
139  cog.outl("/// Template to determine the largest type and size.")
140  cog.outl("/// Supports up to %s types." % NTypes)
141  cog.outl("/// Defines 'value_type' which is the type of the largest parameter.")
142  cog.outl("/// Defines 'size' which is the size of the largest parameter.")
143  cog.outl("///\ingroup largest")
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 largest_type")
153  cog.outl("{")
154  cog.outl(" // Define 'largest_other' as 'largest_type' with all but the first parameter. ")
155  cog.out(" typedef typename largest_type<")
156  for n in range(2, int(NTypes)):
157  cog.out("T%s, " % n)
158  if n % 16 == 0:
159  cog.outl("")
160  cog.out(" ")
161  cog.outl("T%s>::type largest_other;" % int(NTypes))
162  cog.outl("")
163  cog.outl(" // Set 'type' to be the largest of the first parameter and any of the others.")
164  cog.outl(" // This is recursive.")
165  cog.outl(" typedef typename etl::conditional<(sizeof(T1) > sizeof(largest_other)), // Boolean")
166  cog.outl(" T1, // TrueType")
167  cog.outl(" largest_other> // FalseType")
168  cog.outl(" ::type type; // The largest type of the two.")
169  cog.outl("")
170  cog.outl(" // The size of the largest type.")
171  cog.outl(" enum")
172  cog.outl(" {")
173  cog.outl(" size = sizeof(type)")
174  cog.outl(" };")
175  cog.outl("};")
176  cog.outl("")
177  cog.outl("//***************************************************************************")
178  cog.outl("// Specialisation for one template parameter.")
179  cog.outl("//***************************************************************************")
180  cog.outl("template <typename T1>")
181  cog.out("struct largest_type<T1, ")
182  for n in range(2, int(NTypes)):
183  cog.out("void, ")
184  if n % 8 == 0:
185  cog.outl("")
186  cog.out(" ")
187  cog.outl("void>")
188  cog.outl("{")
189  cog.outl(" typedef T1 type;")
190  cog.outl("")
191  cog.outl(" enum")
192  cog.outl(" {")
193  cog.outl(" size = sizeof(type)")
194  cog.outl(" };")
195  cog.outl("};")
196  ]]]*/
197  /*[[[end]]]*/
198 #endif
199 
200 #if ETL_CPP11_SUPPORTED && !defined(ETL_LARGEST_ALIGNMENT_FORCE_CPP03)
201  //***************************************************************************
205  //***************************************************************************
206  template <typename T1, typename... TRest>
207  struct largest_alignment
208  {
209  // Define 'largest_other' as 'largest_type' with all but the first parameter.
210  using largest_other = typename largest_alignment<TRest...>::type;
211 
212  // Set 'type' to be the largest of the first parameter and any of the others.
213  // This is recursive.
215  T1, // TrueType
216  largest_other> // FalseType
217  ::type; // The largest type of the two.
218 
219  // The largest alignment.
220  enum
221  {
223  };
224  };
225 
226  //***************************************************************************
227  // Specialisation for one template parameter.
228  //***************************************************************************
229  template <typename T1>
230  struct largest_alignment<T1>
231  {
232  typedef T1 type;
233 
234  enum
235  {
237  };
238  };
239 
240 #if ETL_CPP17_SUPPORTED
241  template <typename... T>
242  inline constexpr size_t largest_alignment_v = largest_alignment<T...>::value;
243 #endif
244 
245 #else
246  /*[[[cog
247  import cog
248  cog.outl("//***************************************************************************")
249  cog.outl("/// Template to determine the largest alignment.")
250  cog.outl("/// Supports up to %s types." % int(NTypes))
251  cog.outl("/// Defines <b>value</b> which is the largest alignment of all the parameters.")
252  cog.outl("///\ingroup largest")
253  cog.outl("//***************************************************************************")
254  cog.out("template <typename T1, ")
255  for n in range(2, int(NTypes)):
256  cog.out("typename T%s = void, " % n)
257  if n % 4 == 0:
258  cog.outl("")
259  cog.out(" ")
260  cog.outl("typename T%s = void>" % int(NTypes))
261  cog.outl("struct largest_alignment")
262  cog.outl("{")
263  cog.outl(" // Define 'largest_other' as 'largest_type' with all but the first parameter. ")
264  cog.out(" typedef typename largest_alignment<")
265  for n in range(2, int(NTypes)):
266  cog.out("T%s, " % n)
267  if n % 16 == 0:
268  cog.outl("")
269  cog.out(" ")
270  cog.outl("T%s>::type largest_other;" % int(NTypes))
271  cog.outl("")
272  cog.outl(" // Set 'type' to be the largest of the first parameter and any of the others.")
273  cog.outl(" // This is recursive.")
274  cog.outl(" typedef typename etl::conditional<(etl::alignment_of<T1>::value > etl::alignment_of<largest_other>::value), // Boolean")
275  cog.outl(" T1, // TrueType")
276  cog.outl(" largest_other> // FalseType")
277  cog.outl(" ::type type; // The largest type of the two.")
278  cog.outl("")
279  cog.outl(" // The largest alignment.")
280  cog.outl(" enum")
281  cog.outl(" {")
282  cog.outl(" value = etl::alignment_of<type>::value")
283  cog.outl(" };")
284  cog.outl("};")
285  cog.outl("")
286  cog.outl("//***************************************************************************")
287  cog.outl("// Specialisation for one template parameter.")
288  cog.outl("//***************************************************************************")
289  cog.outl("template <typename T1>")
290  cog.out("struct largest_alignment<T1, ")
291  for n in range(2, int(NTypes)):
292  cog.out("void, ")
293  if n % 8 == 0:
294  cog.outl("")
295  cog.out(" ")
296  cog.outl("void>")
297  cog.outl("{")
298  cog.outl(" typedef T1 type;")
299  cog.outl("")
300  cog.outl(" enum")
301  cog.outl(" {")
302  cog.outl(" value = etl::alignment_of<type>::value")
303  cog.outl(" };")
304  cog.outl("};")
305  ]]]*/
306  /*[[[end]]]*/
307 #endif
308 
309  //***************************************************************************
313  //***************************************************************************
314  template <typename T>
316  {
317  ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Must be an integral type");
318 
320  };
321 
322 #if ETL_CPP14_SUPPORTED
323  template <typename T>
324  using larger_int_type_t = typename larger_int_type<T>::type;
325 #endif
326 
327  //***************************************************************************
331  //***************************************************************************
332  template <typename T>
334  {
335  ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Must be an integral type");
336 
338  };
339 
340 #if ETL_CPP14_SUPPORTED
341  template <typename T>
342  using larger_uint_type_t = typename larger_uint_type<T>::type;
343 #endif
344 
345  //***************************************************************************
350  //***************************************************************************
351  template <typename T, bool IS_SIGNED = etl::is_signed<T>::value>
352  struct larger_type;
353 
354  template <typename T>
355  struct larger_type<T, false>
356  {
357  ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Must be an integral type");
358 
359  typedef typename etl::smallest_uint_for_bits<etl::integral_limits<T>::bits + 1>::type type;
360  };
361 
362  template <typename T>
363  struct larger_type<T, true>
364  {
365  ETL_STATIC_ASSERT(etl::is_integral<T>::value, "Must be an integral type");
366 
367  typedef typename etl::smallest_int_for_bits<etl::integral_limits<T>::bits + 1>::type type;
368  };
369 
370 #if ETL_CPP14_SUPPORTED
371  template <typename T>
372  using larger_type_t = typename larger_type<T>::type;
373 #endif
374 
375 #if ETL_CPP11_SUPPORTED && !defined(ETL_LARGEST_FORCE_CPP03)
376  //***************************************************************************
380  //***************************************************************************
381  template <typename... T>
382  struct largest
383  {
384  using type = typename etl::largest_type<T...>::type;
385 
386  enum
387  {
388  size = etl::largest_type<T...>::size,
389  alignment = etl::largest_alignment<T...>::value
390  };
391  };
392 
393 #if ETL_CPP14_SUPPORTED
394  template <typename... T>
395  using largest_t = typename largest<T...>::type;
396 #endif
397 
398 #else
399  /*[[[cog
400  import cog
401  cog.outl("//***************************************************************************")
402  cog.outl("/// Template to determine the largest type, size and alignment.")
403  cog.outl("/// Supports up to %s types." % NTypes)
404  cog.outl("/// Defines <b>value</b> which is the largest type, size and alignment of all the parameters.")
405  cog.outl("///\ingroup largest")
406  cog.outl("//***************************************************************************")
407  cog.out("template <typename T1, ")
408  for n in range(2, int(NTypes)):
409  cog.out("typename T%s = void, " % n)
410  if n % 4 == 0:
411  cog.outl("")
412  cog.out(" ")
413  cog.outl("typename T%s = void>" % NTypes)
414  cog.outl("struct largest")
415  cog.outl("{")
416  cog.out(" typedef typename etl::largest_type<")
417  for n in range(1, int(NTypes)):
418  cog.out("T%s, " % n)
419  if n % 16 == 0:
420  cog.outl("")
421  cog.out(" ")
422  cog.outl("T%s>::type type;" % NTypes)
423  cog.outl("")
424  cog.outl(" enum")
425  cog.outl(" {")
426  cog.out(" size = etl::largest_type<")
427  for n in range(1, int(NTypes)):
428  cog.out("T%s, " % n)
429  if n % 16 == 0:
430  cog.outl("")
431  cog.out(" ")
432  cog.outl("T%s>::size," % NTypes)
433  cog.out(" alignment = etl::largest_alignment<")
434  for n in range(1, int(NTypes)):
435  cog.out("T%s, " % n)
436  if n % 16 == 0:
437  cog.outl("")
438  cog.out(" ")
439  cog.outl("T%s>::value" % NTypes)
440  cog.outl(" };")
441  cog.outl("};")
442  ]]]*/
443  /*[[[end]]]*/
444 #endif
445 }
446 
447 #endif
Defines a type that is as larger or larger than the specified type. Will return the specified type is...
Definition: largest_generator.h:316
Defines a type that is as larger or larger than the specified type. Will return the specified type is...
Definition: largest_generator.h:352
Defines a type that is as larger or larger than the specified type. Will return the specified type is...
Definition: largest_generator.h:334
Definition: largest.h:362
Definition: largest.h:227
Definition: largest.h:136
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 unsigned int type that can contain a value with the specified numb...
Definition: smallest_generator.h:319
add_rvalue_reference
Definition: type_traits_generator.h:1348
conditional
Definition: type_traits_generator.h:1202
is_integral
Definition: type_traits_generator.h:941
Definition: absolute.h:37
size_of
Definition: type_traits_generator.h:1572