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>
38 struct TypeTraitsBase
39 {
40  typedef T Value;
41  typedef const T ConstValue;
42  typedef T& Reference;
43  typedef const T& ConstReference;
44  typedef T* Pointer;
45  typedef const T* ConstPointer;
46 };
47 
49 template <typename T>
50 struct TypeTraits : public TypeTraitsBase<T>
51 {
52  static const unsigned int isConst = 0;
53  static const unsigned int isPointer = 0;
54  static const unsigned int isReference = 0;
55 };
57 
58 template <typename T>
59 struct TypeTraits<const T> : public TypeTraitsBase<T>
60 {
61  static const unsigned int isConst = 1;
62  static const unsigned int isPointer = 0;
63  static const unsigned int isReference = 0;
64 };
65 
66 
67 template <typename T>
68 struct TypeTraits<T&> : public TypeTraitsBase<T>
69 {
70  static const unsigned int isConst = 0;
71  static const unsigned int isPointer = 0;
72  static const unsigned int isReference = 1;
73 };
74 
75 
76 template <typename T>
77 struct TypeTraits<const T&> : public TypeTraitsBase<T>
78 {
79  static const unsigned int isConst = 1;
80  static const unsigned int isPointer = 0;
81  static const unsigned int isReference = 1;
82 };
83 
84 
85 template <typename T>
86 struct TypeTraits<T*> : public TypeTraitsBase<T>
87 {
88  static const unsigned int isConst = 0;
89  static const unsigned int isPointer = 1;
90  static const unsigned int isReference = 0;
91 };
92 
93 
94 template <typename T>
95 struct TypeTraits<const T*> : public TypeTraitsBase<T>
96 {
97  static const unsigned int isConst = 1;
98  static const unsigned int isPointer = 1;
99  static const unsigned int isReference = 0;
100 };
101 
102 
103 template <typename T, std::size_t N>
104 struct TypeTraits<T[N]> : public TypeTraitsBase<T>
105 {
106  static const unsigned int isConst = 0;
107  static const unsigned int isPointer = 1;
108  static const unsigned int isReference = 0;
109 };
110 
111 
112 template <>
113 struct TypeTraits<void>
114 {
115  typedef void Value;
116  typedef void ConstType;
117  typedef void Reference;
118  typedef void ConstReference;
119  typedef void* Pointer;
120  typedef void* ConstPointer;
121 
122  static const unsigned int isConst = 0;
123  static const unsigned int isPointer = 0;
124  static const unsigned int isReference = 0;
125 };
126 
127 
128 template <typename T>
129 struct IntTraits
130 {};
131 
132 template <>
133 struct IntTraits<signed char>
134 {
135  typedef unsigned char Unsigned;
136  typedef signed char Signed;
137 
138  static const unsigned int isSigned = 1;
139 };
140 
141 template <>
142 struct IntTraits<unsigned char>
143 {
144  typedef unsigned char Unsigned;
145  typedef signed char Signed;
146 
147  static const unsigned int isSigned = 0;
148 };
149 
150 template <>
151 struct IntTraits<short>
152 {
153  typedef unsigned short Unsigned;
154  typedef signed short Signed;
155 
156  static const unsigned int isSigned = 1;
157 };
158 
159 template <>
160 struct IntTraits<unsigned short>
161 {
162  typedef unsigned short Unsigned;
163  typedef signed short Signed;
164 
165  static const unsigned int isSigned = 0;
166 };
167 
168 template <>
169 struct IntTraits<int>
170 {
171  typedef unsigned int Unsigned;
172  typedef signed int Signed;
173 
174  static const unsigned int isSigned = 1;
175 };
176 
177 template <>
178 struct IntTraits<unsigned int>
179 {
180  typedef unsigned int Unsigned;
181  typedef signed int Signed;
182 
183  static const unsigned int isSigned = 0;
184 };
185 
186 template <>
187 struct IntTraits<long>
188 {
189  typedef unsigned long Unsigned;
190  typedef signed long Signed;
191 
192  static const unsigned int isSigned = 1;
193 };
194 
195 template <>
196 struct IntTraits<unsigned long>
197 {
198  typedef unsigned long Unsigned;
199  typedef signed long Signed;
200 
201  static const unsigned int isSigned = 0;
202 };
203 
204 template <>
205 struct IntTraits<long long>
206 {
207  typedef unsigned long long Unsigned;
208  typedef signed long long Signed;
209 
210  static const unsigned int isSigned = 1;
211 };
212 
213 template <>
214 struct IntTraits<unsigned long long>
215 {
216  typedef unsigned long long Unsigned;
217  typedef signed long long Signed;
218 
219  static const unsigned int isSigned = 0;
220 };
221 
222 } // namespace Pt
223 
224 #endif // Pt_TypeTraits_h
IMPLEMENTATION_DEFINED ConstReference
The derived const qualified reference type.
Definition: TypeTraits.h:30
IMPLEMENTATION_DEFINED ConstValue
The derived const qualified value type.
Definition: TypeTraits.h:24
static const unsigned int isReference
If the type is a reference 1, otherwise 0.
Definition: TypeTraits.h:45
Traits for type properties.
Definition: TypeTraits.h:18
IMPLEMENTATION_DEFINED Pointer
The derived pointer type.
Definition: TypeTraits.h:33
IMPLEMENTATION_DEFINED Reference
The derived reference type.
Definition: TypeTraits.h:27
static const unsigned int isPointer
If the type is a pointer 1, otherwise 0.
Definition: TypeTraits.h:42
static const unsigned int isConst
If the type is const 1, otherwise 0.
Definition: TypeTraits.h:39
IMPLEMENTATION_DEFINED Value
The derived value type.
Definition: TypeTraits.h:21
IMPLEMENTATION_DEFINED ConstPointer
The derived const qualified reference type.
Definition: TypeTraits.h:36