OpenWalnut  1.5.0dev
WTypeTraits.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WTYPETRAITS_H
26 #define WTYPETRAITS_H
27 
28 #include <stdint.h>
29 
30 /**
31  * All kinds of type traits and policies like type priorities and type combinations.
32  */
33 namespace WTypeTraits
34 {
35  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
36  // Type promitions
37  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
38 
39  /**
40  * Class for checking the "better" type if two integral types are known.
41  *
42  * \tparam T1 the first type
43  * \tparam T2 the second type
44  */
45  template < typename T1, typename T2 >
46  class TypePromotion; // leaving this one empty is a good idea in most cases as there is no "natural order" in all c++ types.
47 
48  /**
49  * Class for checking the "better" type if two integral types are known. Specialization if both types are equal
50  *
51  * \tparam T the types
52  */
53  template < typename T >
54  class TypePromotion< T, T >
55  {
56  public:
57  typedef T Result; //!< if both types are the same, the "better" type is obvious.
58  };
59 
60  // we assume macros to be evil but it helps us here!
61 #define CREATEPROMOTION( T1, T2, ResultType ) \
62  template <> \
63  class TypePromotion< T1, T2 > \
64  { /*NOLINT*/ \
65  public: \
66  typedef ResultType Result; \
67  }; /*NOLINT*/ \
68  \
69  template <> \
70  class TypePromotion< T2, T1 > \
71  { /*NOLINT*/ \
72  public: \
73  typedef ResultType Result; \
74  }; /*NOLINT*/ \
75 
76  // Create the promotions we need. Please check this list. But do not change arbitrarily if you need a different mapping. Instead, specialize
77  // the template TypePromotion locally.
78 
79  // Exclusion of this macro stuff from doxygen:
80  // \cond HIDDEN_SYMBOLS
81 
82  // long double is the better choice for these
83  CREATEPROMOTION( long double, double, long double )
84  CREATEPROMOTION( long double, float, long double )
85  CREATEPROMOTION( long double, int64_t, long double )
86  CREATEPROMOTION( long double, int32_t, long double )
87  CREATEPROMOTION( long double, int16_t, long double )
88  CREATEPROMOTION( long double, int8_t, long double )
89  CREATEPROMOTION( long double, uint64_t, long double )
90  CREATEPROMOTION( long double, uint32_t, long double )
91  CREATEPROMOTION( long double, uint16_t, long double )
92  CREATEPROMOTION( long double, uint8_t, long double )
93 
94  // double is the better choice for these
95  CREATEPROMOTION( double, float, double )
96  CREATEPROMOTION( double, int64_t, double )
97  CREATEPROMOTION( double, int32_t, double )
98  CREATEPROMOTION( double, int16_t, double )
99  CREATEPROMOTION( double, int8_t, double )
100  CREATEPROMOTION( double, uint64_t, double )
101  CREATEPROMOTION( double, uint32_t, double )
102  CREATEPROMOTION( double, uint16_t, double )
103  CREATEPROMOTION( double, uint8_t, double )
104 
105  // float is the better choice for these (?)
106  CREATEPROMOTION( float, int64_t, float )
107  CREATEPROMOTION( float, int32_t, float )
108  CREATEPROMOTION( float, int16_t, float )
109  CREATEPROMOTION( float, int8_t, float )
110  CREATEPROMOTION( float, uint64_t, float )
111  CREATEPROMOTION( float, uint32_t, float )
112  CREATEPROMOTION( float, uint16_t, float )
113  CREATEPROMOTION( float, uint8_t, float )
114 
115  // int64_t is the better choice for these (?)
116  CREATEPROMOTION( int64_t, int32_t, int64_t )
117  CREATEPROMOTION( int64_t, int16_t, int64_t )
118  CREATEPROMOTION( int64_t, int8_t, int64_t )
119  CREATEPROMOTION( int64_t, uint64_t, double ) // ?
120  CREATEPROMOTION( int64_t, uint32_t, int64_t )
121  CREATEPROMOTION( int64_t, uint16_t, int64_t )
122  CREATEPROMOTION( int64_t, uint8_t, int64_t )
123 
124  // int32_t is the better choice for these (?)
125  CREATEPROMOTION( int32_t, int16_t, int32_t )
126  CREATEPROMOTION( int32_t, int8_t, int32_t )
127  CREATEPROMOTION( int32_t, uint64_t, double ) // ?
128  CREATEPROMOTION( int32_t, uint32_t, int64_t ) // ?
129  CREATEPROMOTION( int32_t, uint16_t, int32_t )
130  CREATEPROMOTION( int32_t, uint8_t, int32_t )
131 
132  // int16_t is the better choice for these (?)
133  CREATEPROMOTION( int16_t, int8_t, int16_t )
134  CREATEPROMOTION( int16_t, uint64_t, double ) // ?
135  CREATEPROMOTION( int16_t, uint32_t, int64_t ) // ?
136  CREATEPROMOTION( int16_t, uint16_t, int32_t ) // ?
137  CREATEPROMOTION( int16_t, uint8_t, int16_t )
138 
139  // int8_t is the better choice for these (?)
140  CREATEPROMOTION( int8_t, uint64_t, double ) // ?
141  CREATEPROMOTION( int8_t, uint32_t, int64_t ) // ?
142  CREATEPROMOTION( int8_t, uint16_t, int32_t ) // ?
143  CREATEPROMOTION( int8_t, uint8_t, int16_t ) // ?
144 
145  // uint64_t is the better choice for these (?)
146  CREATEPROMOTION( uint64_t, uint32_t, uint64_t )
147  CREATEPROMOTION( uint64_t, uint16_t, uint64_t )
148  CREATEPROMOTION( uint64_t, uint8_t, uint64_t )
149 
150  // uint32_t is the better choice for these (?)
151  CREATEPROMOTION( uint32_t, uint16_t, uint32_t )
152  CREATEPROMOTION( uint32_t, uint8_t, uint32_t )
153 
154  // uint16_t is the better choice for these (?)
155  CREATEPROMOTION( uint16_t, uint8_t, uint16_t )
156 
157  // uint8_t is the better choice for these (?)
158  // promoted already
159 
160  // Exclusion of this macro stuff from doxygen: end
161  // \endcond
162 }
163 
164 #endif // WTYPETRAITS_H
165 
T Result
if both types are the same, the "better" type is obvious.
Definition: WTypeTraits.h:57
Class for checking the "better" type if two integral types are known.
Definition: WTypeTraits.h:46
All kinds of type traits and policies like type priorities and type combinations.
Definition: WTypeTraits.h:34