OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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  // double is the better choice for these
83  CREATEPROMOTION( double, float, double )
84  CREATEPROMOTION( double, int64_t, double )
85  CREATEPROMOTION( double, int32_t, double )
86  CREATEPROMOTION( double, int16_t, double )
87  CREATEPROMOTION( double, int8_t, double )
88  CREATEPROMOTION( double, uint64_t, double )
89  CREATEPROMOTION( double, uint32_t, double )
90  CREATEPROMOTION( double, uint16_t, double )
91  CREATEPROMOTION( double, uint8_t, double )
92 
93  // float is the better choice for these (?)
94  CREATEPROMOTION( float, int64_t, float )
95  CREATEPROMOTION( float, int32_t, float )
96  CREATEPROMOTION( float, int16_t, float )
97  CREATEPROMOTION( float, int8_t, float )
98  CREATEPROMOTION( float, uint64_t, float )
99  CREATEPROMOTION( float, uint32_t, float )
100  CREATEPROMOTION( float, uint16_t, float )
101  CREATEPROMOTION( float, uint8_t, float )
102 
103  // int64_t is the better choice for these (?)
104  CREATEPROMOTION( int64_t, int32_t, int64_t )
105  CREATEPROMOTION( int64_t, int16_t, int64_t )
106  CREATEPROMOTION( int64_t, int8_t, int64_t )
107  CREATEPROMOTION( int64_t, uint64_t, double ) // ?
108  CREATEPROMOTION( int64_t, uint32_t, int64_t )
109  CREATEPROMOTION( int64_t, uint16_t, int64_t )
110  CREATEPROMOTION( int64_t, uint8_t, int64_t )
111 
112  // int32_t is the better choice for these (?)
113  CREATEPROMOTION( int32_t, int16_t, int32_t )
114  CREATEPROMOTION( int32_t, int8_t, int32_t )
115  CREATEPROMOTION( int32_t, uint64_t, double ) // ?
116  CREATEPROMOTION( int32_t, uint32_t, int64_t ) // ?
117  CREATEPROMOTION( int32_t, uint16_t, int32_t )
118  CREATEPROMOTION( int32_t, uint8_t, int32_t )
119 
120  // int16_t is the better choice for these (?)
121  CREATEPROMOTION( int16_t, int8_t, int16_t )
122  CREATEPROMOTION( int16_t, uint64_t, double ) // ?
123  CREATEPROMOTION( int16_t, uint32_t, int64_t ) // ?
124  CREATEPROMOTION( int16_t, uint16_t, int32_t ) // ?
125  CREATEPROMOTION( int16_t, uint8_t, int16_t )
126 
127  // int8_t is the better choice for these (?)
128  CREATEPROMOTION( int8_t, uint64_t, double ) // ?
129  CREATEPROMOTION( int8_t, uint32_t, int64_t ) // ?
130  CREATEPROMOTION( int8_t, uint16_t, int32_t ) // ?
131  CREATEPROMOTION( int8_t, uint8_t, int16_t ) // ?
132 
133  // uint64_t is the better choice for these (?)
134  CREATEPROMOTION( uint64_t, uint32_t, uint64_t )
135  CREATEPROMOTION( uint64_t, uint16_t, uint64_t )
136  CREATEPROMOTION( uint64_t, uint8_t, uint64_t )
137 
138  // uint32_t is the better choice for these (?)
139  CREATEPROMOTION( uint32_t, uint16_t, uint32_t )
140  CREATEPROMOTION( uint32_t, uint8_t, uint32_t )
141 
142  // uint16_t is the better choice for these (?)
143  CREATEPROMOTION( uint16_t, uint8_t, uint16_t )
144 
145  // uint8_t is the better choice for these (?)
146  // promoted already
147 
148  // Exclusion of this macro stuff from doxygen: end
149  // \endcond
150 }
151 
152 #endif // WTYPETRAITS_H
153