Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
smallest.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_SMALLEST__
30 #define __ETL_SMALLEST__
31 
32 #include <stdint.h>
33 
34 #include "integral_limits.h"
35 
38 
39 namespace etl
40 {
41  //***************************************************************************
47  //***************************************************************************
48  template <typename T1, typename T2 = void, typename T3 = void, typename T4 = void,
49  typename T5 = void, typename T6 = void, typename T7 = void, typename T8 = void,
50  typename T9 = void, typename T10 = void, typename T11 = void, typename T12 = void,
51  typename T13 = void, typename T14 = void, typename T15 = void, typename T16 = void>
53  {
54  private:
55 
56  // Declaration.
57  template <const bool Boolean, typename TrueType, typename FalseType>
58  struct choose_type;
59 
60  // Specialisation for 'true'.
61  // Defines 'type' as 'TrueType'.
62  template <typename TrueType, typename FalseType>
63  struct choose_type<true, TrueType, FalseType>
64  {
65  typedef TrueType type;
66  };
67 
68  // Specialisation for 'false'.
69  // Defines 'type' as 'FalseType'.
70  template <typename TrueType, typename FalseType>
71  struct choose_type<false, TrueType, FalseType>
72  {
73  typedef FalseType type;
74  };
75 
76  public:
77 
78  // Define 'smallest_other' as 'smallest_type' with all but the first parameter.
79  typedef typename smallest_type<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>::type smallest_other;
80 
81  // Set 'type' to be the smallest of the first parameter and any of the others.
82  // This is recursive.
83  typedef typename choose_type<(sizeof(T1) < sizeof(smallest_other)),// Boolean
84  T1, // TrueType
85  smallest_other> // FalseType
86  ::type type; // The smallest type of the two.
87 
88  // The size of the smallest type.
89  enum
90  {
91  size = sizeof(type)
92  };
93  };
94 
95  //***************************************************************************
96  // Specialisation for one template parameter.
97  //***************************************************************************
98  template <typename T1>
99  struct smallest_type <T1, void, void, void,
100  void, void, void, void,
101  void, void, void, void,
102  void, void, void, void>
103  {
104  typedef T1 type;
105 
106  enum
107  {
108  size = sizeof(type)
109  };
110  };
111 
112  namespace __private_smallest__
113  {
114  //*************************************************************************
115  // Determine the type to hold the number of bits based on the index.
116  //*************************************************************************
117  template <const int index>
119 
120  //*************************************************************************
121  // Less than or equal to 8 bits.
122  //*************************************************************************
123  template <>
125  {
126  typedef uint_least8_t type;
127  };
128 
129  //*************************************************************************
130  // 9 to 16 bits.
131  //*************************************************************************
132  template <>
134  {
135  typedef uint_least16_t type;
136  };
137 
138  //*************************************************************************
139  // 17 to 31 bits.
140  //*************************************************************************
141  template <>
143  {
144  typedef uint_least32_t type;
145  };
146 
147  //*************************************************************************
148  // Greater than 32 bits.
149  //*************************************************************************
150  template <>
152  {
153  typedef uint_least64_t type;
154  };
155  }
156 
157  //***************************************************************************
162  //***************************************************************************
163  template <const size_t N>
165  {
166  private:
167 
168  // Determines the index of the best unsigned type for the required number of bits.
169  static const int TYPE_INDEX = ((N > 8) ? 1 : 0) + ((N > 16) ? 1 : 0) + ((N > 32) ? 1 : 0);
170 
171  public:
172 
174  };
175 }
176 
177 #endif
Definition: algorithm.h:43
Definition: smallest.h:164
Definition: smallest.h:52