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