typetraits.h
Go to the documentation of this file.
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 #ifndef cxxtools_TypeTraits_h
29 #define cxxtools_TypeTraits_h
30 
31 #include <cxxtools/config.h>
32 #include <cstddef>
33 #include <stdint.h>
34 
35 namespace cxxtools
36 {
37 
38  template <typename T>
39  struct TypeTraitsBase {
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 
54  template <typename T>
55  struct TypeTraits : public TypeTraitsBase<T>
56  {
57  static const unsigned int isConst = 0;
58  static const unsigned int isPointer = 0;
59  static const unsigned int isReference = 0;
60  };
61 
64  template <typename T>
65  struct TypeTraits<const T> : public TypeTraitsBase<T>
66  {
67  static const unsigned int isConst = 1;
68  static const unsigned int isPointer = 0;
69  static const unsigned int isReference = 0;
70  };
71 
74  template <typename T>
75  struct TypeTraits<T&> : public TypeTraitsBase<T>
76  {
77  static const unsigned int isConst = 0;
78  static const unsigned int isPointer = 0;
79  static const unsigned int isReference = 1;
80  };
81 
84  template <typename T>
85  struct TypeTraits<const T&> : public TypeTraitsBase<T>
86  {
87  static const unsigned int isConst = 1;
88  static const unsigned int isPointer = 0;
89  static const unsigned int isReference = 1;
90  };
91 
94  template <typename T>
95  struct TypeTraits<T*> : public TypeTraitsBase<T>
96  {
97  static const unsigned int isConst = 0;
98  static const unsigned int isPointer = 1;
99  static const unsigned int isReference = 0;
100  };
101 
104  template <typename T>
105  struct TypeTraits<const T*> : public TypeTraitsBase<T>
106  {
107  static const unsigned int isConst = 1;
108  static const unsigned int isPointer = 1;
109  static const unsigned int isReference = 0;
110  };
111 
114  template <typename T, std::size_t N>
115  struct TypeTraits<T[N]> : public TypeTraitsBase<T>
116  {
117  static const unsigned int isConst = 0;
118  static const unsigned int isPointer = 1;
119  static const unsigned int isReference = 0;
120  };
121 
124  template <>
125  struct TypeTraits<void>
126  {
127  typedef void Value;
128  typedef void ConstType;
129  typedef void Reference;
130  typedef void ConstReference;
131  typedef void* Pointer;
132  typedef void* ConstPointer;
133 
134  static const unsigned int isConst = 0;
135  static const unsigned int isPointer = 0;
136  static const unsigned int isReference = 0;
137  };
138 
139 
140  template <typename T>
141  struct IntTraits
142  {};
143 
144 
145  template <>
146  struct IntTraits<signed char>
147  {
148  typedef unsigned char Unsigned;
149  typedef signed char Signed;
150  };
151 
152  template <>
153  struct IntTraits<unsigned char>
154  {
155  typedef unsigned char Unsigned;
156  typedef signed char Signed;
157  };
158 
159 
160  template <>
161  struct IntTraits<short>
162  {
163  typedef unsigned short Unsigned;
164  typedef signed short Signed;
165  };
166 
167  template <>
168  struct IntTraits<unsigned short>
169  {
170  typedef unsigned short Unsigned;
171  typedef signed short Signed;
172  };
173 
174 
175  template <>
176  struct IntTraits<int>
177  {
178  typedef unsigned int Unsigned;
179  typedef signed int Signed;
180  };
181 
182  template <>
183  struct IntTraits<unsigned int>
184  {
185  typedef unsigned int Unsigned;
186  typedef signed int Signed;
187  };
188 
189 
190  template <>
191  struct IntTraits<long>
192  {
193  typedef unsigned long Unsigned;
194  typedef signed long Signed;
195  };
196 
197 
198  template <>
199  struct IntTraits<unsigned long>
200  {
201  typedef unsigned long Unsigned;
202  typedef signed long Signed;
203  };
204 
205 
206 #ifdef HAVE_LONG_LONG
207  template <>
208  struct IntTraits<long long>
209  {
210  typedef unsigned long long Unsigned;
211  typedef signed long long Signed;
212  };
213 #endif
214 
215 
216 #ifdef HAVE_UNSIGNED_LONG_LONG
217  template <>
218  struct IntTraits<unsigned long long>
219  {
220  typedef unsigned long long Unsigned;
221  typedef signed long long Signed;
222  };
223 #endif
224 
225 } // !namespace cxxtools
226 
227 
228 #endif