Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:35

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CXX03___MEMORY_COMPRESSED_PAIR_H
0011 #define _LIBCPP___CXX03___MEMORY_COMPRESSED_PAIR_H
0012 
0013 #include <__cxx03/__config>
0014 #include <__cxx03/__fwd/tuple.h>
0015 #include <__cxx03/__tuple/tuple_indices.h>
0016 #include <__cxx03/__type_traits/decay.h>
0017 #include <__cxx03/__type_traits/dependent_type.h>
0018 #include <__cxx03/__type_traits/enable_if.h>
0019 #include <__cxx03/__type_traits/is_constructible.h>
0020 #include <__cxx03/__type_traits/is_empty.h>
0021 #include <__cxx03/__type_traits/is_final.h>
0022 #include <__cxx03/__type_traits/is_same.h>
0023 #include <__cxx03/__type_traits/is_swappable.h>
0024 #include <__cxx03/__utility/forward.h>
0025 #include <__cxx03/__utility/move.h>
0026 #include <__cxx03/__utility/piecewise_construct.h>
0027 #include <__cxx03/cstddef>
0028 
0029 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0030 #  pragma GCC system_header
0031 #endif
0032 
0033 _LIBCPP_PUSH_MACROS
0034 #include <__cxx03/__undef_macros>
0035 
0036 _LIBCPP_BEGIN_NAMESPACE_STD
0037 
0038 // Tag used to default initialize one or both of the pair's elements.
0039 struct __default_init_tag {};
0040 struct __value_init_tag {};
0041 
0042 template <class _Tp, int _Idx, bool _CanBeEmptyBase = is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
0043 struct __compressed_pair_elem {
0044   using _ParamT         = _Tp;
0045   using reference       = _Tp&;
0046   using const_reference = const _Tp&;
0047 
0048   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__default_init_tag) {}
0049   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__value_init_tag) : __value_() {}
0050 
0051   template <class _Up, __enable_if_t<!is_same<__compressed_pair_elem, __decay_t<_Up> >::value, int> = 0>
0052   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(_Up&& __u)
0053       : __value_(std::forward<_Up>(__u)) {}
0054 
0055 #ifndef _LIBCPP_CXX03_LANG
0056   template <class... _Args, size_t... _Indices>
0057   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 explicit __compressed_pair_elem(
0058       piecewise_construct_t, tuple<_Args...> __args, __tuple_indices<_Indices...>)
0059       : __value_(std::forward<_Args>(std::get<_Indices>(__args))...) {}
0060 #endif
0061 
0062   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference __get() _NOEXCEPT { return __value_; }
0063   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference __get() const _NOEXCEPT { return __value_; }
0064 
0065 private:
0066   _Tp __value_;
0067 };
0068 
0069 template <class _Tp, int _Idx>
0070 struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
0071   using _ParamT         = _Tp;
0072   using reference       = _Tp&;
0073   using const_reference = const _Tp&;
0074   using __value_type    = _Tp;
0075 
0076   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem() = default;
0077   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__default_init_tag) {}
0078   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__value_init_tag) : __value_type() {}
0079 
0080   template <class _Up, __enable_if_t<!is_same<__compressed_pair_elem, __decay_t<_Up> >::value, int> = 0>
0081   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(_Up&& __u)
0082       : __value_type(std::forward<_Up>(__u)) {}
0083 
0084 #ifndef _LIBCPP_CXX03_LANG
0085   template <class... _Args, size_t... _Indices>
0086   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17
0087   __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args, __tuple_indices<_Indices...>)
0088       : __value_type(std::forward<_Args>(std::get<_Indices>(__args))...) {}
0089 #endif
0090 
0091   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference __get() _NOEXCEPT { return *this; }
0092   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference __get() const _NOEXCEPT { return *this; }
0093 };
0094 
0095 template <class _T1, class _T2>
0096 class __compressed_pair : private __compressed_pair_elem<_T1, 0>, private __compressed_pair_elem<_T2, 1> {
0097 public:
0098   // NOTE: This static assert should never fire because __compressed_pair
0099   // is *almost never* used in a scenario where it's possible for T1 == T2.
0100   // (The exception is std::function where it is possible that the function
0101   //  object and the allocator have the same type).
0102   static_assert(
0103       (!is_same<_T1, _T2>::value),
0104       "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
0105       "The current implementation is NOT ABI-compatible with the previous implementation for this configuration");
0106 
0107   using _Base1 _LIBCPP_NODEBUG = __compressed_pair_elem<_T1, 0>;
0108   using _Base2 _LIBCPP_NODEBUG = __compressed_pair_elem<_T2, 1>;
0109 
0110   template <bool _Dummy         = true,
0111             __enable_if_t< __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
0112                                __dependent_type<is_default_constructible<_T2>, _Dummy>::value,
0113                            int> = 0>
0114   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair()
0115       : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
0116 
0117   template <class _U1, class _U2>
0118   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair(_U1&& __t1, _U2&& __t2)
0119       : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
0120 
0121 #ifndef _LIBCPP_CXX03_LANG
0122   template <class... _Args1, class... _Args2>
0123   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 explicit __compressed_pair(
0124       piecewise_construct_t __pc, tuple<_Args1...> __first_args, tuple<_Args2...> __second_args)
0125       : _Base1(__pc, std::move(__first_args), typename __make_tuple_indices<sizeof...(_Args1)>::type()),
0126         _Base2(__pc, std::move(__second_args), typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
0127 #endif
0128 
0129   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename _Base1::reference first() _NOEXCEPT {
0130     return static_cast<_Base1&>(*this).__get();
0131   }
0132 
0133   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR typename _Base1::const_reference first() const _NOEXCEPT {
0134     return static_cast<_Base1 const&>(*this).__get();
0135   }
0136 
0137   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename _Base2::reference second() _NOEXCEPT {
0138     return static_cast<_Base2&>(*this).__get();
0139   }
0140 
0141   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR typename _Base2::const_reference second() const _NOEXCEPT {
0142     return static_cast<_Base2 const&>(*this).__get();
0143   }
0144 
0145   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static _Base1* __get_first_base(__compressed_pair* __pair) _NOEXCEPT {
0146     return static_cast<_Base1*>(__pair);
0147   }
0148   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static _Base2* __get_second_base(__compressed_pair* __pair) _NOEXCEPT {
0149     return static_cast<_Base2*>(__pair);
0150   }
0151 
0152   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void swap(__compressed_pair& __x)
0153       _NOEXCEPT_(__is_nothrow_swappable_v<_T1>&& __is_nothrow_swappable_v<_T2>) {
0154     using std::swap;
0155     swap(first(), __x.first());
0156     swap(second(), __x.second());
0157   }
0158 };
0159 
0160 template <class _T1, class _T2>
0161 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
0162 swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
0163     _NOEXCEPT_(__is_nothrow_swappable_v<_T1>&& __is_nothrow_swappable_v<_T2>) {
0164   __x.swap(__y);
0165 }
0166 
0167 _LIBCPP_END_NAMESPACE_STD
0168 
0169 _LIBCPP_POP_MACROS
0170 
0171 #endif // _LIBCPP___CXX03___MEMORY_COMPRESSED_PAIR_H