Sierra Toolkit  Version of the Day
type_transformations_eastl.h
1 /*
2 Copyright (C) 2005,2009-2010 Electronic Arts, Inc. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 
8 1. Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13 3. Neither the name of Electronic Arts, Inc. ("EA") nor the names of
14  its contributors may be used to endorse or promote products derived
15  from this software without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY ELECTRONIC ARTS AND ITS CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ELECTRONIC ARTS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
30 // EASTL/internal/type_transformations.h
31 // Written and maintained by Paul Pedriana - 2005
33 
34 
35 #ifndef EASTL_INTERNAL_TYPE_TRANFORMATIONS_H
36 #define EASTL_INTERNAL_TYPE_TRANFORMATIONS_H
37 
38 
39 namespace eastl
40 {
41 
42 
43  // The following transformations are defined here. If the given item
44  // is missing then it simply hasn't been implemented, at least not yet.
45  // add_unsigned
46  // add_signed
47  // remove_const
48  // remove_volatile
49  // remove_cv
50  // add_const
51  // add_volatile
52  // add_cv
53  // remove_reference
54  // add_reference
55  // remove_extent
56  // remove_all_extents
57  // remove_pointer
58  // add_pointer
59  // aligned_storage
60 
61 
63  // add_signed
64  //
65  // Adds signed-ness to the given type.
66  // Modifies only integral values; has no effect on others.
67  // add_signed<int>::type is int
68  // add_signed<unsigned int>::type is int
69  //
71 
72  template<class T>
73  struct add_signed
74  { typedef T type; };
75 
76  template<>
77  struct add_signed<unsigned char>
78  { typedef signed char type; };
79 
80  #if (defined(CHAR_MAX) && defined(UCHAR_MAX) && (CHAR_MAX == UCHAR_MAX)) // If char is unsigned (which is usually not the case)...
81  template<>
82  struct add_signed<char>
83  { typedef signed char type; };
84  #endif
85 
86  template<>
87  struct add_signed<unsigned short>
88  { typedef short type; };
89 
90  template<>
91  struct add_signed<unsigned int>
92  { typedef int type; };
93 
94  template<>
95  struct add_signed<unsigned long>
96  { typedef long type; };
97 
98  template<>
99  struct add_signed<unsigned long long>
100  { typedef long long type; };
101 
102  #ifndef EA_WCHAR_T_NON_NATIVE // If wchar_t is a native type instead of simply a define to an existing type...
103  #if (defined(__WCHAR_MAX__) && (__WCHAR_MAX__ == 4294967295U)) // If wchar_t is a 32 bit unsigned value...
104  template<>
105  struct add_signed<wchar_t>
106  { typedef int32_t type; };
107  #elif (defined(__WCHAR_MAX__) && (__WCHAR_MAX__ == 65535)) // If wchar_t is a 16 bit unsigned value...
108  template<>
109  struct add_signed<wchar_t>
110  { typedef int16_t type; };
111  #endif
112  #endif
113 
114 
115 
117  // add_unsigned
118  //
119  // Adds unsigned-ness to the given type.
120  // Modifies only integral values; has no effect on others.
121  // add_unsigned<int>::type is unsigned int
122  // add_unsigned<unsigned int>::type is unsigned int
123  //
125 
126  template<class T>
127  struct add_unsigned
128  { typedef T type; };
129 
130  template<>
131  struct add_unsigned<signed char>
132  { typedef unsigned char type; };
133 
134  #if (defined(CHAR_MAX) && defined(SCHAR_MAX) && (CHAR_MAX == SCHAR_MAX)) // If char is signed (which is usually so)...
135  template<>
136  struct add_unsigned<char>
137  { typedef unsigned char type; };
138  #endif
139 
140  template<>
141  struct add_unsigned<short>
142  { typedef unsigned short type; };
143 
144  template<>
145  struct add_unsigned<int>
146  { typedef unsigned int type; };
147 
148  template<>
149  struct add_unsigned<long>
150  { typedef unsigned long type; };
151 
152  template<>
153  struct add_unsigned<long long>
154  { typedef unsigned long long type; };
155 
156  #ifndef EA_WCHAR_T_NON_NATIVE // If wchar_t is a native type instead of simply a define to an existing type...
157  #if (defined(__WCHAR_MAX__) && (__WCHAR_MAX__ == 2147483647)) // If wchar_t is a 32 bit signed value...
158  template<>
159  struct add_unsigned<wchar_t>
160  { typedef uint32_t type; };
161  #elif (defined(__WCHAR_MAX__) && (__WCHAR_MAX__ == 32767)) // If wchar_t is a 16 bit signed value...
162  template<>
163  struct add_unsigned<wchar_t>
164  { typedef uint16_t type; };
165  #endif
166  #endif
167 
169  // remove_cv
170  //
171  // Remove const and volatile from a type.
172  //
173  // The remove_cv transformation trait removes top-level const and/or volatile
174  // qualification (if any) from the type to which it is applied. For a given type T,
175  // remove_cv<T const volatile>::type is equivalent to T. For example,
176  // remove_cv<char* volatile>::type is equivalent to char*, while remove_cv<const char*>::type
177  // is equivalent to const char*. In the latter case, the const qualifier modifies
178  // char, not *, and is therefore not at the top level.
179  //
181  template <typename T> struct remove_cv_imp{};
182  template <typename T> struct remove_cv_imp<T*> { typedef T unqualified_type; };
183  template <typename T> struct remove_cv_imp<const T*> { typedef T unqualified_type; };
184  template <typename T> struct remove_cv_imp<volatile T*> { typedef T unqualified_type; };
185  template <typename T> struct remove_cv_imp<const volatile T*> { typedef T unqualified_type; };
186 
187  template <typename T> struct remove_cv{ typedef typename remove_cv_imp<T*>::unqualified_type type; };
188  template <typename T> struct remove_cv<T&>{ typedef T& type; }; // References are automatically not const nor volatile. See section 8.3.2p1 of the C++ standard.
189 
190  template <typename T, size_t N> struct remove_cv<T const[N]> { typedef T type[N]; };
191  template <typename T, size_t N> struct remove_cv<T volatile[N]> { typedef T type[N]; };
192  template <typename T, size_t N> struct remove_cv<T const volatile[N]>{ typedef T type[N]; };
193 
194 
195 
197  // add_reference
198  //
199  // Add reference to a type.
200  //
201  // The add_reference transformation trait adds a level of indirection
202  // by reference to the type to which it is applied. For a given type T,
203  // add_reference<T>::type is equivalent to T& if is_reference<T>::value == false,
204  // and T otherwise.
205  //
207  template <typename T>
208  struct add_reference_impl{ typedef T& type; };
209 
210  template <typename T>
211  struct add_reference_impl<T&>{ typedef T& type; };
212 
213  template <>
214  struct add_reference_impl<void>{ typedef void type; };
215 
216  template <typename T>
217  struct add_reference { typedef typename add_reference_impl<T>::type type; };
218 
219 
220 } // namespace eastl
221 
222 
223 #endif // Header include guard
EA Standard Template Library.