| 1 | // -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2005-2024 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 3, 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 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | /** @file ext/type_traits.h |
| 26 | * This file is a GNU extension to the Standard C++ Library. |
| 27 | */ |
| 28 | |
| 29 | #ifndef _EXT_TYPE_TRAITS |
| 30 | #define _EXT_TYPE_TRAITS 1 |
| 31 | |
| 32 | #pragma GCC system_header |
| 33 | |
| 34 | #include <bits/c++config.h> |
| 35 | #include <bits/cpp_type_traits.h> |
| 36 | |
| 37 | extern "C++" { |
| 38 | |
| 39 | namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) |
| 40 | { |
| 41 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 42 | |
| 43 | // Define a nested type if some predicate holds. |
| 44 | template<bool, typename> |
| 45 | struct __enable_if |
| 46 | { }; |
| 47 | |
| 48 | template<typename _Tp> |
| 49 | struct __enable_if<true, _Tp> |
| 50 | { typedef _Tp __type; }; |
| 51 | |
| 52 | |
| 53 | // Conditional expression for types. If true, first, if false, second. |
| 54 | template<bool _Cond, typename _Iftrue, typename _Iffalse> |
| 55 | struct __conditional_type |
| 56 | { typedef _Iftrue __type; }; |
| 57 | |
| 58 | template<typename _Iftrue, typename _Iffalse> |
| 59 | struct __conditional_type<false, _Iftrue, _Iffalse> |
| 60 | { typedef _Iffalse __type; }; |
| 61 | |
| 62 | |
| 63 | // Given an integral builtin type, return the corresponding unsigned type. |
| 64 | template<typename _Tp> |
| 65 | struct __add_unsigned |
| 66 | { |
| 67 | private: |
| 68 | typedef __enable_if<std::__is_integer<_Tp>::__value, _Tp> __if_type; |
| 69 | |
| 70 | public: |
| 71 | typedef typename __if_type::__type __type; |
| 72 | }; |
| 73 | |
| 74 | template<> |
| 75 | struct __add_unsigned<char> |
| 76 | { typedef unsigned char __type; }; |
| 77 | |
| 78 | template<> |
| 79 | struct __add_unsigned<signed char> |
| 80 | { typedef unsigned char __type; }; |
| 81 | |
| 82 | template<> |
| 83 | struct __add_unsigned<short> |
| 84 | { typedef unsigned short __type; }; |
| 85 | |
| 86 | template<> |
| 87 | struct __add_unsigned<int> |
| 88 | { typedef unsigned int __type; }; |
| 89 | |
| 90 | template<> |
| 91 | struct __add_unsigned<long> |
| 92 | { typedef unsigned long __type; }; |
| 93 | |
| 94 | template<> |
| 95 | struct __add_unsigned<long long> |
| 96 | { typedef unsigned long long __type; }; |
| 97 | |
| 98 | // Declare but don't define. |
| 99 | template<> |
| 100 | struct __add_unsigned<bool>; |
| 101 | |
| 102 | template<> |
| 103 | struct __add_unsigned<wchar_t>; |
| 104 | |
| 105 | |
| 106 | // Given an integral builtin type, return the corresponding signed type. |
| 107 | template<typename _Tp> |
| 108 | struct __remove_unsigned |
| 109 | { |
| 110 | private: |
| 111 | typedef __enable_if<std::__is_integer<_Tp>::__value, _Tp> __if_type; |
| 112 | |
| 113 | public: |
| 114 | typedef typename __if_type::__type __type; |
| 115 | }; |
| 116 | |
| 117 | template<> |
| 118 | struct __remove_unsigned<char> |
| 119 | { typedef signed char __type; }; |
| 120 | |
| 121 | template<> |
| 122 | struct __remove_unsigned<unsigned char> |
| 123 | { typedef signed char __type; }; |
| 124 | |
| 125 | template<> |
| 126 | struct __remove_unsigned<unsigned short> |
| 127 | { typedef short __type; }; |
| 128 | |
| 129 | template<> |
| 130 | struct __remove_unsigned<unsigned int> |
| 131 | { typedef int __type; }; |
| 132 | |
| 133 | template<> |
| 134 | struct __remove_unsigned<unsigned long> |
| 135 | { typedef long __type; }; |
| 136 | |
| 137 | template<> |
| 138 | struct __remove_unsigned<unsigned long long> |
| 139 | { typedef long long __type; }; |
| 140 | |
| 141 | // Declare but don't define. |
| 142 | template<> |
| 143 | struct __remove_unsigned<bool>; |
| 144 | |
| 145 | template<> |
| 146 | struct __remove_unsigned<wchar_t>; |
| 147 | |
| 148 | |
| 149 | // For use in string and vstring. |
| 150 | template<typename _Type> |
| 151 | _GLIBCXX_CONSTEXPR |
| 152 | inline bool |
| 153 | __is_null_pointer(_Type* __ptr) |
| 154 | { return __ptr == 0; } |
| 155 | |
| 156 | template<typename _Type> |
| 157 | _GLIBCXX_CONSTEXPR |
| 158 | inline bool |
| 159 | __is_null_pointer(_Type) |
| 160 | { return false; } |
| 161 | |
| 162 | #if __cplusplus >= 201103L |
| 163 | constexpr bool |
| 164 | __is_null_pointer(std::nullptr_t) |
| 165 | { return true; } |
| 166 | #endif |
| 167 | |
| 168 | // For arithmetic promotions in <complex> and <cmath> |
| 169 | |
| 170 | template<typename _Tp, bool = std::__is_integer<_Tp>::__value> |
| 171 | struct __promote |
| 172 | { typedef double __type; }; |
| 173 | |
| 174 | // No nested __type member for non-integer non-floating point types, |
| 175 | // allows this type to be used for SFINAE to constrain overloads in |
| 176 | // <cmath> and <complex> to only the intended types. |
| 177 | template<typename _Tp> |
| 178 | struct __promote<_Tp, false> |
| 179 | { }; |
| 180 | |
| 181 | template<> |
| 182 | struct __promote<long double> |
| 183 | { typedef long double __type; }; |
| 184 | |
| 185 | template<> |
| 186 | struct __promote<double> |
| 187 | { typedef double __type; }; |
| 188 | |
| 189 | template<> |
| 190 | struct __promote<float> |
| 191 | { typedef float __type; }; |
| 192 | |
| 193 | #ifdef __STDCPP_FLOAT16_T__ |
| 194 | template<> |
| 195 | struct __promote<_Float16> |
| 196 | { typedef _Float16 __type; }; |
| 197 | #endif |
| 198 | |
| 199 | #ifdef __STDCPP_FLOAT32_T__ |
| 200 | template<> |
| 201 | struct __promote<_Float32> |
| 202 | { typedef _Float32 __type; }; |
| 203 | #endif |
| 204 | |
| 205 | #ifdef __STDCPP_FLOAT64_T__ |
| 206 | template<> |
| 207 | struct __promote<_Float64> |
| 208 | { typedef _Float64 __type; }; |
| 209 | #endif |
| 210 | |
| 211 | #ifdef __STDCPP_FLOAT128_T__ |
| 212 | template<> |
| 213 | struct __promote<_Float128> |
| 214 | { typedef _Float128 __type; }; |
| 215 | #endif |
| 216 | |
| 217 | #ifdef __STDCPP_BFLOAT16_T__ |
| 218 | template<> |
| 219 | struct __promote<__gnu_cxx::__bfloat16_t> |
| 220 | { typedef __gnu_cxx::__bfloat16_t __type; }; |
| 221 | #endif |
| 222 | |
| 223 | #if __cpp_fold_expressions |
| 224 | |
| 225 | template<typename... _Tp> |
| 226 | using __promoted_t = decltype((typename __promote<_Tp>::__type(0) + ...)); |
| 227 | |
| 228 | // Deducing the promoted type is done by __promoted_t<_Tp...>, |
| 229 | // then __promote is used to provide the nested __type member. |
| 230 | template<typename _Tp, typename _Up> |
| 231 | using __promote_2 = __promote<__promoted_t<_Tp, _Up>>; |
| 232 | |
| 233 | template<typename _Tp, typename _Up, typename _Vp> |
| 234 | using __promote_3 = __promote<__promoted_t<_Tp, _Up, _Vp>>; |
| 235 | |
| 236 | template<typename _Tp, typename _Up, typename _Vp, typename _Wp> |
| 237 | using __promote_4 = __promote<__promoted_t<_Tp, _Up, _Vp, _Wp>>; |
| 238 | |
| 239 | #else |
| 240 | |
| 241 | template<typename _Tp, typename _Up, |
| 242 | typename _Tp2 = typename __promote<_Tp>::__type, |
| 243 | typename _Up2 = typename __promote<_Up>::__type> |
| 244 | struct __promote_2 |
| 245 | { |
| 246 | typedef __typeof__(_Tp2() + _Up2()) __type; |
| 247 | }; |
| 248 | |
| 249 | template<typename _Tp, typename _Up, typename _Vp, |
| 250 | typename _Tp2 = typename __promote<_Tp>::__type, |
| 251 | typename _Up2 = typename __promote<_Up>::__type, |
| 252 | typename _Vp2 = typename __promote<_Vp>::__type> |
| 253 | struct __promote_3 |
| 254 | { |
| 255 | typedef __typeof__(_Tp2() + _Up2() + _Vp2()) __type; |
| 256 | }; |
| 257 | |
| 258 | template<typename _Tp, typename _Up, typename _Vp, typename _Wp, |
| 259 | typename _Tp2 = typename __promote<_Tp>::__type, |
| 260 | typename _Up2 = typename __promote<_Up>::__type, |
| 261 | typename _Vp2 = typename __promote<_Vp>::__type, |
| 262 | typename _Wp2 = typename __promote<_Wp>::__type> |
| 263 | struct __promote_4 |
| 264 | { |
| 265 | typedef __typeof__(_Tp2() + _Up2() + _Vp2() + _Wp2()) __type; |
| 266 | }; |
| 267 | #endif |
| 268 | |
| 269 | _GLIBCXX_END_NAMESPACE_VERSION |
| 270 | } // namespace |
| 271 | } // extern "C++" |
| 272 | |
| 273 | #endif |
| 274 | |