]>
Commit | Line | Data |
---|---|---|
105c6331 BK |
1 | // -*- C++ -*- |
2 | ||
3 | // Copyright (C) 2005, 2006 Free Software Foundation, Inc. | |
4 | // | |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the terms | |
7 | // of the GNU General Public License as published by the Free Software | |
8 | // Foundation; either version 2, or (at your option) any later | |
9 | // version. | |
10 | ||
11 | // This library is distributed in the hope that it will be useful, but | |
12 | // WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | // General Public License for more details. | |
15 | ||
16 | // You should have received a copy of the GNU General Public License | |
17 | // along with this library; see the file COPYING. If not, write to | |
18 | // the Free Software Foundation, 59 Temple Place - Suite 330, Boston, | |
19 | // MA 02111-1307, USA. | |
20 | ||
21 | // As a special exception, you may use this file as part of a free | |
22 | // software library without restriction. Specifically, if other files | |
23 | // instantiate templates or use macros or inline functions from this | |
24 | // file, or you compile this file and link it with other files to | |
25 | // produce an executable, this file does not by itself cause the | |
26 | // resulting executable to be covered by the GNU General Public | |
27 | // License. This exception does not however invalidate any other | |
28 | // reasons why the executable file might be covered by the GNU General | |
29 | // Public License. | |
30 | ||
31 | #ifndef _EXT_TYPE_TRAITS | |
32 | #define _EXT_TYPE_TRAITS 1 | |
33 | ||
34 | #pragma GCC system_header | |
35 | ||
36 | #include <cstddef> | |
37 | #include <utility> | |
38 | #include <limits> | |
39 | #include <iosfwd> // std::streamsize | |
a1adedd0 | 40 | #include <bits/cpp_type_traits.h> |
105c6331 BK |
41 | |
42 | _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) | |
43 | ||
44 | // Define a nested type if some predicate holds. | |
45 | template<bool, typename> | |
46 | struct __enable_if | |
47 | { }; | |
48 | ||
49 | template<typename _Tp> | |
50 | struct __enable_if<true, _Tp> | |
51 | { typedef _Tp __type; }; | |
52 | ||
3454c18f | 53 | |
105c6331 BK |
54 | // Conditional expression for types. If true, first, if false, second. |
55 | template<bool _Cond, typename _Iftrue, typename _Iffalse> | |
56 | struct __conditional_type | |
57 | { typedef _Iftrue __type; }; | |
58 | ||
59 | template<typename _Iftrue, typename _Iffalse> | |
60 | struct __conditional_type<false, _Iftrue, _Iffalse> | |
61 | { typedef _Iffalse __type; }; | |
62 | ||
63 | ||
3454c18f BK |
64 | // Given an integral builtin type, return the corresponding unsigned type. |
65 | template<typename _T> | |
105c6331 | 66 | struct __add_unsigned |
3454c18f BK |
67 | { |
68 | private: | |
69 | typedef __enable_if<std::__is_integer<_T>::__value, _T> __if_type; | |
70 | ||
71 | public: | |
72 | typedef typename __if_type::__type __type; | |
73 | }; | |
105c6331 BK |
74 | |
75 | template<> | |
76 | struct __add_unsigned<char> | |
77 | { typedef unsigned char __type; }; | |
78 | ||
3454c18f BK |
79 | template<> |
80 | struct __add_unsigned<signed char> | |
81 | { typedef unsigned char __type; }; | |
82 | ||
105c6331 BK |
83 | template<> |
84 | struct __add_unsigned<short> | |
85 | { typedef unsigned short __type; }; | |
86 | ||
87 | template<> | |
88 | struct __add_unsigned<int> | |
89 | { typedef unsigned int __type; }; | |
90 | ||
91 | template<> | |
92 | struct __add_unsigned<long> | |
93 | { typedef unsigned long __type; }; | |
94 | ||
105c6331 BK |
95 | template<> |
96 | struct __add_unsigned<long long> | |
97 | { typedef unsigned long long __type; }; | |
105c6331 | 98 | |
3454c18f BK |
99 | // Declare but don't define. |
100 | template<> | |
101 | struct __add_unsigned<bool>; | |
102 | ||
103 | template<> | |
104 | struct __add_unsigned<wchar_t>; | |
105 | ||
106 | ||
107 | // Given an integral builtin type, return the corresponding signed type. | |
108 | template<typename _T> | |
105c6331 | 109 | struct __remove_unsigned |
3454c18f BK |
110 | { |
111 | private: | |
112 | typedef __enable_if<std::__is_integer<_T>::__value, _T> __if_type; | |
113 | ||
114 | public: | |
115 | typedef typename __if_type::__type __type; | |
116 | }; | |
117 | ||
118 | template<> | |
119 | struct __remove_unsigned<char> | |
120 | { typedef signed char __type; }; | |
105c6331 BK |
121 | |
122 | template<> | |
123 | struct __remove_unsigned<unsigned char> | |
3454c18f | 124 | { typedef signed char __type; }; |
105c6331 BK |
125 | |
126 | template<> | |
127 | struct __remove_unsigned<unsigned short> | |
128 | { typedef short __type; }; | |
129 | ||
130 | template<> | |
131 | struct __remove_unsigned<unsigned int> | |
132 | { typedef int __type; }; | |
133 | ||
134 | template<> | |
135 | struct __remove_unsigned<unsigned long> | |
136 | { typedef long __type; }; | |
137 | ||
105c6331 BK |
138 | template<> |
139 | struct __remove_unsigned<unsigned long long> | |
140 | { typedef long long __type; }; | |
3454c18f BK |
141 | |
142 | // Declare but don't define. | |
143 | template<> | |
144 | struct __remove_unsigned<bool>; | |
145 | ||
146 | template<> | |
147 | struct __remove_unsigned<wchar_t>; | |
148 | ||
105c6331 BK |
149 | |
150 | // Compile time constants for builtin types. | |
151 | // Sadly std::numeric_limits member functions cannot be used for this. | |
152 | #define __glibcxx_signed(T) ((T)(-1) < 0) | |
153 | #define __glibcxx_digits(T) (sizeof(T) * __CHAR_BIT__ - __glibcxx_signed(T)) | |
154 | ||
155 | #define __glibcxx_min(T) \ | |
156 | (__glibcxx_signed(T) ? (T)1 << __glibcxx_digits(T) : (T)0) | |
157 | ||
158 | #define __glibcxx_max(T) \ | |
159 | (__glibcxx_signed(T) ? ((T)1 << __glibcxx_digits(T)) - 1 : ~(T)0) | |
160 | ||
161 | template<typename _Value> | |
a1adedd0 | 162 | struct __numeric_traits_integer |
105c6331 | 163 | { |
a1adedd0 BK |
164 | // Only integers for initialization of member constant. |
165 | static const _Value __min = __glibcxx_min(_Value); | |
166 | static const _Value __max = __glibcxx_max(_Value); | |
105c6331 BK |
167 | }; |
168 | ||
169 | template<typename _Value> | |
a1adedd0 | 170 | const _Value __numeric_traits_integer<_Value>::__min; |
105c6331 BK |
171 | |
172 | template<typename _Value> | |
a1adedd0 | 173 | const _Value __numeric_traits_integer<_Value>::__max; |
105c6331 BK |
174 | |
175 | template<typename _Value> | |
a1adedd0 BK |
176 | struct __numeric_traits_floating |
177 | { | |
178 | // Only floating point types. See N1822. | |
179 | static const std::streamsize __max_digits10 = | |
180 | 2 + std::numeric_limits<_Value>::digits * 3010/10000; | |
181 | }; | |
182 | ||
183 | template<typename _Value> | |
184 | const std::streamsize __numeric_traits_floating<_Value>::__max_digits10; | |
185 | ||
186 | template<typename _Value> | |
187 | struct __numeric_traits | |
188 | : public __conditional_type<std::__is_integer<_Value>::__value, | |
189 | __numeric_traits_integer<_Value>, | |
190 | __numeric_traits_floating<_Value> >::__type | |
191 | { }; | |
105c6331 BK |
192 | |
193 | _GLIBCXX_END_NAMESPACE | |
194 | ||
195 | #endif |