TypeTraits.h
1 /*
2  * Copyright (C) 2005 Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef Pt_TypeTraits_h
30 #define Pt_TypeTraits_h
31 
32 #include <Pt/Api.h>
33 #include <cstddef>
34 
35 namespace Pt {
36 
37 template <typename T, T V>
38 struct IntegralConstant
39 {
40  static const T value = V;
41  typedef T ValueType;
42  typedef IntegralConstant<T, V> Type;
43 
44  operator ValueType() const
45  { return value; }
46 };
47 
48 
49 template <bool V>
50 using BoolConstant = IntegralConstant<bool, V>;
51 
52 typedef BoolConstant<false> FalseType;
53 typedef BoolConstant<true> TrueType;
54 
55 
56 template<class T, class U>
57 struct IsSame : FalseType
58 {};
59 
60 
61 template<class T>
62 struct IsSame<T, T> : TrueType
63 {};
64 
65 
66 template <bool Condition, typename TrueT, typename FalseT>
67 struct IfElse
68 {
69  typedef TrueT Type;
70 };
71 
72 
73 template <typename TrueT, typename FalseT>
74 struct IfElse<false, TrueT, FalseT>
75 {
76  typedef FalseT Type;
77 };
78 
79 
80 template <typename T>
81 struct TypeTraitsBase
82 {
83  typedef T Value;
84  typedef const T ConstValue;
85  typedef T& Reference;
86  typedef const T& ConstReference;
87  typedef T* Pointer;
88  typedef const T* ConstPointer;
89 };
90 
92 template <typename T>
93 struct TypeTraits : public TypeTraitsBase<T>
94 {
95  static const unsigned int isConst = 0;
96  static const unsigned int isPointer = 0;
97  static const unsigned int isReference = 0;
98 };
100 
101 template <typename T>
102 struct TypeTraits<const T> : public TypeTraitsBase<T>
103 {
104  static const unsigned int isConst = 1;
105  static const unsigned int isPointer = 0;
106  static const unsigned int isReference = 0;
107 };
108 
109 
110 template <typename T>
111 struct TypeTraits<T&> : public TypeTraitsBase<T>
112 {
113  static const unsigned int isConst = 0;
114  static const unsigned int isPointer = 0;
115  static const unsigned int isReference = 1;
116 };
117 
118 
119 template <typename T>
120 struct TypeTraits<const T&> : public TypeTraitsBase<T>
121 {
122  static const unsigned int isConst = 1;
123  static const unsigned int isPointer = 0;
124  static const unsigned int isReference = 1;
125 };
126 
127 
128 template <typename T>
129 struct TypeTraits<T*> : public TypeTraitsBase<T>
130 {
131  static const unsigned int isConst = 0;
132  static const unsigned int isPointer = 1;
133  static const unsigned int isReference = 0;
134 };
135 
136 
137 template <typename T>
138 struct TypeTraits<const T*> : public TypeTraitsBase<T>
139 {
140  static const unsigned int isConst = 1;
141  static const unsigned int isPointer = 1;
142  static const unsigned int isReference = 0;
143 };
144 
145 
146 template <typename T, std::size_t N>
147 struct TypeTraits<T[N]> : public TypeTraitsBase<T>
148 {
149  static const unsigned int isConst = 0;
150  static const unsigned int isPointer = 1;
151  static const unsigned int isReference = 0;
152 };
153 
154 
155 template <>
156 struct TypeTraits<void>
157 {
158  typedef void Value;
159  typedef void ConstType;
160  typedef void Reference;
161  typedef void ConstReference;
162  typedef void* Pointer;
163  typedef void* ConstPointer;
164 
165  static const unsigned int isConst = 0;
166  static const unsigned int isPointer = 0;
167  static const unsigned int isReference = 0;
168 };
169 
170 
171 template <typename Base, typename Derived>
172 class IsCompatibleImpl
173 {
174  private:
175  struct YesType
176  {
177  char value;
178  };
179 
180  struct NoType
181  {
182  char value[2];
183  };
184 
185  typedef typename TypeTraits<Base>::Value BaseType;
186  typedef typename TypeTraits<Derived>::Value DerivedType;
187 
188  static YesType test(BaseType*);
189  static NoType test(...);
190 
191  public:
192  enum
193  {
194  value = sizeof(test(static_cast<DerivedType*>(0))) == sizeof(YesType)
195  };
196 };
197 
198 
199 template <typename Base, typename Derived>
200 class IsCompatible
201 : public BoolConstant< IsCompatibleImpl<Base, Derived>::value >
202 {};
203 
204 
205 template <typename T>
206 struct IntTraits
207 {};
208 
209 template <>
210 struct IntTraits<signed char>
211 {
212  typedef unsigned char Unsigned;
213  typedef signed char Signed;
214 
215  static const unsigned int isSigned = 1;
216 };
217 
218 template <>
219 struct IntTraits<unsigned char>
220 {
221  typedef unsigned char Unsigned;
222  typedef signed char Signed;
223 
224  static const unsigned int isSigned = 0;
225 };
226 
227 template <>
228 struct IntTraits<short>
229 {
230  typedef unsigned short Unsigned;
231  typedef signed short Signed;
232 
233  static const unsigned int isSigned = 1;
234 };
235 
236 template <>
237 struct IntTraits<unsigned short>
238 {
239  typedef unsigned short Unsigned;
240  typedef signed short Signed;
241 
242  static const unsigned int isSigned = 0;
243 };
244 
245 template <>
246 struct IntTraits<int>
247 {
248  typedef unsigned int Unsigned;
249  typedef signed int Signed;
250 
251  static const unsigned int isSigned = 1;
252 };
253 
254 template <>
255 struct IntTraits<unsigned int>
256 {
257  typedef unsigned int Unsigned;
258  typedef signed int Signed;
259 
260  static const unsigned int isSigned = 0;
261 };
262 
263 template <>
264 struct IntTraits<long>
265 {
266  typedef unsigned long Unsigned;
267  typedef signed long Signed;
268 
269  static const unsigned int isSigned = 1;
270 };
271 
272 template <>
273 struct IntTraits<unsigned long>
274 {
275  typedef unsigned long Unsigned;
276  typedef signed long Signed;
277 
278  static const unsigned int isSigned = 0;
279 };
280 
281 template <>
282 struct IntTraits<long long>
283 {
284  typedef unsigned long long Unsigned;
285  typedef signed long long Signed;
286 
287  static const unsigned int isSigned = 1;
288 };
289 
290 template <>
291 struct IntTraits<unsigned long long>
292 {
293  typedef unsigned long long Unsigned;
294  typedef signed long long Signed;
295 
296  static const unsigned int isSigned = 0;
297 };
298 
299 } // namespace Pt
300 
301 #endif // Pt_TypeTraits_h
Core module.
Definition: pt-gfx-images.dox:14
static const unsigned int isReference
If the type is a reference 1, otherwise 0.
Definition: TypeTraits.h:45
IMPLEMENTATION_DEFINED ConstReference
The derived const qualified reference type.
Definition: TypeTraits.h:30
static const unsigned int isPointer
If the type is a pointer 1, otherwise 0.
Definition: TypeTraits.h:42
IMPLEMENTATION_DEFINED Pointer
The derived pointer type.
Definition: TypeTraits.h:33
IMPLEMENTATION_DEFINED Reference
The derived reference type.
Definition: TypeTraits.h:27
IMPLEMENTATION_DEFINED Value
The derived value type.
Definition: TypeTraits.h:21
IMPLEMENTATION_DEFINED ConstPointer
The derived const qualified reference type.
Definition: TypeTraits.h:36
static const unsigned int isConst
If the type is const 1, otherwise 0.
Definition: TypeTraits.h:39