Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
largest.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 
8 Copyright(c) 2014 jwellbelove
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files(the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions :
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 ******************************************************************************/
28 
29 #ifndef __ETL_LARGEST__
30 #define __ETL_LARGEST__
31 
34 
35 #include "type_traits.h"
36 
37 namespace etl
38 {
39  //***************************************************************************
45  //***************************************************************************
46  template <typename T1, typename T2 = void, typename T3 = void, typename T4 = void,
47  typename T5 = void, typename T6 = void, typename T7 = void, typename T8 = void,
48  typename T9 = void, typename T10 = void, typename T11 = void, typename T12 = void,
49  typename T13 = void, typename T14 = void, typename T15 = void, typename T16 = void>
50  struct largest_type
51  {
52  private:
53 
54  // Declaration.
55  template <const bool Boolean, typename TrueType, typename FalseType>
56  struct choose_type;
57 
58  // Specialisation for 'true'.
59  // Defines 'type' as 'TrueType'.
60  template <typename TrueType, typename FalseType>
61  struct choose_type<true, TrueType, FalseType>
62  {
63  typedef TrueType type;
64  };
65 
66  // Specialisation for 'false'.
67  // Defines 'type' as 'FalseType'.
68  template <typename TrueType, typename FalseType>
69  struct choose_type<false, TrueType, FalseType>
70  {
71  typedef FalseType type;
72  };
73 
74  public:
75 
76  // Define 'largest_other' as 'largest_type' with all but the first parameter.
77  typedef typename largest_type<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>::type largest_other;
78 
79  // Set 'type' to be the largest of the first parameter and any of the others.
80  // This is recursive.
81  typedef typename choose_type<(sizeof(T1) > sizeof(largest_other)), // Boolean
82  T1, // TrueType
83  largest_other> // FalseType
84  ::type type; // The largest type of the two.
85 
86  // The size of the largest type.
87  enum
88  {
89  size = sizeof(type)
90  };
91  };
92 
93  //***************************************************************************
94  // Specialisation for one template parameter.
95  //***************************************************************************
96  template <typename T1>
97  struct largest_type<T1, void, void, void,
98  void, void, void, void,
99  void, void, void, void,
100  void, void, void, void>
101  {
102  typedef T1 type;
103 
104  enum
105  {
106  size = sizeof(type)
107  };
108  };
109 
110  //***************************************************************************
115  //***************************************************************************
116  template <typename T1, typename T2 = void, typename T3 = void, typename T4 = void,
117  typename T5 = void, typename T6 = void, typename T7 = void, typename T8 = void,
118  typename T9 = void, typename T10 = void, typename T11 = void, typename T12 = void,
119  typename T13 = void, typename T14 = void, typename T15 = void, typename T16 = void>
121  {
122  // Determine the largest size.
123  template <const size_t size1, const size_t size2>
124  struct max_size
125  {
126  enum
127  {
128  value = (size1 > size2) ? size1 : size2
129  };
130  };
131 
132  // All of the alignments.
133  enum
134  {
135  t1 = alignment_of<T1>::value,
136  t2 = alignment_of<T2>::value,
137  t3 = alignment_of<T3>::value,
138  t4 = alignment_of<T4>::value,
139  t5 = alignment_of<T5>::value,
140  t6 = alignment_of<T6>::value,
141  t7 = alignment_of<T7>::value,
142  t8 = alignment_of<T8>::value,
143  t9 = alignment_of<T9>::value,
144  t10 = alignment_of<T10>::value,
145  t11 = alignment_of<T11>::value,
146  t12 = alignment_of<T12>::value,
147  t13 = alignment_of<T13>::value,
148  t14 = alignment_of<T14>::value,
149  t15 = alignment_of<T15>::value,
150  t16 = alignment_of<T16>::value,
151  };
152 
153  public:
154 
155  // The largest of all of them.
156  enum
157  {
158  value = max_size<t1, max_size<t2, max_size<t3, max_size<t4, max_size<t5, max_size<t6, max_size<t7, max_size<t8,
160  ::value>::value>::value>::value>::value>::value>::value>::value>
161  ::value>::value>::value>::value>::value>::value>::value
162  };
163  };
164 }
165 
166 #endif
Definition: largest.h:120
Definition: algorithm.h:43
Definition: largest.h:124
Definition: largest.h:50