1fe6060f1SDimitry Andric // -*- C++ -*- 2fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 3fe6060f1SDimitry Andric // 4fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 6fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7fe6060f1SDimitry Andric // 8fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 9fe6060f1SDimitry Andric 10fe6060f1SDimitry Andric #ifndef _LIBCPP___MEMORY_COMPRESSED_PAIR_H 11fe6060f1SDimitry Andric #define _LIBCPP___MEMORY_COMPRESSED_PAIR_H 12fe6060f1SDimitry Andric 13fe6060f1SDimitry Andric #include <__config> 14bdd1243dSDimitry Andric #include <__fwd/tuple.h> 1506c3fb27SDimitry Andric #include <__tuple/tuple_indices.h> 16bdd1243dSDimitry Andric #include <__type_traits/decay.h> 17bdd1243dSDimitry Andric #include <__type_traits/dependent_type.h> 18bdd1243dSDimitry Andric #include <__type_traits/enable_if.h> 19*0fca6ea1SDimitry Andric #include <__type_traits/is_constructible.h> 20bdd1243dSDimitry Andric #include <__type_traits/is_empty.h> 21bdd1243dSDimitry Andric #include <__type_traits/is_final.h> 22bdd1243dSDimitry Andric #include <__type_traits/is_same.h> 23bdd1243dSDimitry Andric #include <__type_traits/is_swappable.h> 24fe6060f1SDimitry Andric #include <__utility/forward.h> 2581ad6265SDimitry Andric #include <__utility/move.h> 26bdd1243dSDimitry Andric #include <__utility/piecewise_construct.h> 27bdd1243dSDimitry Andric #include <cstddef> 28fe6060f1SDimitry Andric 29fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 30fe6060f1SDimitry Andric # pragma GCC system_header 31fe6060f1SDimitry Andric #endif 32fe6060f1SDimitry Andric 3306c3fb27SDimitry Andric _LIBCPP_PUSH_MACROS 3406c3fb27SDimitry Andric #include <__undef_macros> 3506c3fb27SDimitry Andric 36fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 37fe6060f1SDimitry Andric 38fe6060f1SDimitry Andric // Tag used to default initialize one or both of the pair's elements. 39fe6060f1SDimitry Andric struct __default_init_tag {}; 40fe6060f1SDimitry Andric struct __value_init_tag {}; 41fe6060f1SDimitry Andric 4281ad6265SDimitry Andric template <class _Tp, int _Idx, bool _CanBeEmptyBase = is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value> 43fe6060f1SDimitry Andric struct __compressed_pair_elem { 4481ad6265SDimitry Andric using _ParamT = _Tp; 4581ad6265SDimitry Andric using reference = _Tp&; 4681ad6265SDimitry Andric using const_reference = const _Tp&; 47fe6060f1SDimitry Andric __compressed_pair_elem__compressed_pair_elem4881ad6265SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__default_init_tag) {} __compressed_pair_elem__compressed_pair_elem4981ad6265SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__value_init_tag) : __value_() {} 50fe6060f1SDimitry Andric 51*0fca6ea1SDimitry Andric template <class _Up, __enable_if_t<!is_same<__compressed_pair_elem, __decay_t<_Up> >::value, int> = 0> __compressed_pair_elem__compressed_pair_elem52cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(_Up&& __u) 53cb14a3feSDimitry Andric : __value_(std::forward<_Up>(__u)) {} 54fe6060f1SDimitry Andric 55fe6060f1SDimitry Andric #ifndef _LIBCPP_CXX03_LANG 5681ad6265SDimitry Andric template <class... _Args, size_t... _Indices> __compressed_pair_elem__compressed_pair_elem57cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 explicit __compressed_pair_elem( 58cb14a3feSDimitry Andric piecewise_construct_t, tuple<_Args...> __args, __tuple_indices<_Indices...>) 5981ad6265SDimitry Andric : __value_(std::forward<_Args>(std::get<_Indices>(__args))...) {} 60fe6060f1SDimitry Andric #endif 61fe6060f1SDimitry Andric __get__compressed_pair_elem62bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference __get() _NOEXCEPT { return __value_; } __get__compressed_pair_elem6381ad6265SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference __get() const _NOEXCEPT { return __value_; } 64fe6060f1SDimitry Andric 65fe6060f1SDimitry Andric private: 66fe6060f1SDimitry Andric _Tp __value_; 67fe6060f1SDimitry Andric }; 68fe6060f1SDimitry Andric 69fe6060f1SDimitry Andric template <class _Tp, int _Idx> 70fe6060f1SDimitry Andric struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp { 7181ad6265SDimitry Andric using _ParamT = _Tp; 7281ad6265SDimitry Andric using reference = _Tp&; 7381ad6265SDimitry Andric using const_reference = const _Tp&; 7481ad6265SDimitry Andric using __value_type = _Tp; 75fe6060f1SDimitry Andric 7681ad6265SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem() = default; 7781ad6265SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__default_init_tag) {} 7881ad6265SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__value_init_tag) : __value_type() {} 79fe6060f1SDimitry Andric 80*0fca6ea1SDimitry Andric template <class _Up, __enable_if_t<!is_same<__compressed_pair_elem, __decay_t<_Up> >::value, int> = 0> 81cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(_Up&& __u) 82cb14a3feSDimitry Andric : __value_type(std::forward<_Up>(__u)) {} 83fe6060f1SDimitry Andric 84fe6060f1SDimitry Andric #ifndef _LIBCPP_CXX03_LANG 8581ad6265SDimitry Andric template <class... _Args, size_t... _Indices> 86bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 8781ad6265SDimitry Andric __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args, __tuple_indices<_Indices...>) 8881ad6265SDimitry Andric : __value_type(std::forward<_Args>(std::get<_Indices>(__args))...) {} 89fe6060f1SDimitry Andric #endif 90fe6060f1SDimitry Andric 91bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference __get() _NOEXCEPT { return *this; } 9281ad6265SDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference __get() const _NOEXCEPT { return *this; } 93fe6060f1SDimitry Andric }; 94fe6060f1SDimitry Andric 95fe6060f1SDimitry Andric template <class _T1, class _T2> 96cb14a3feSDimitry Andric class __compressed_pair : private __compressed_pair_elem<_T1, 0>, private __compressed_pair_elem<_T2, 1> { 97fe6060f1SDimitry Andric public: 98fe6060f1SDimitry Andric // NOTE: This static assert should never fire because __compressed_pair 99fe6060f1SDimitry Andric // is *almost never* used in a scenario where it's possible for T1 == T2. 100fe6060f1SDimitry Andric // (The exception is std::function where it is possible that the function 101fe6060f1SDimitry Andric // object and the allocator have the same type). 102cb14a3feSDimitry Andric static_assert( 103cb14a3feSDimitry Andric (!is_same<_T1, _T2>::value), 104fe6060f1SDimitry Andric "__compressed_pair cannot be instantiated when T1 and T2 are the same type; " 10581ad6265SDimitry Andric "The current implementation is NOT ABI-compatible with the previous implementation for this configuration"); 106fe6060f1SDimitry Andric 10781ad6265SDimitry Andric using _Base1 _LIBCPP_NODEBUG = __compressed_pair_elem<_T1, 0>; 10881ad6265SDimitry Andric using _Base2 _LIBCPP_NODEBUG = __compressed_pair_elem<_T2, 1>; 109fe6060f1SDimitry Andric 110fe6060f1SDimitry Andric template <bool _Dummy = true, 111*0fca6ea1SDimitry Andric __enable_if_t< __dependent_type<is_default_constructible<_T1>, _Dummy>::value && 112*0fca6ea1SDimitry Andric __dependent_type<is_default_constructible<_T2>, _Dummy>::value, 113*0fca6ea1SDimitry Andric int> = 0> 114cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair() 115cb14a3feSDimitry Andric : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {} 116fe6060f1SDimitry Andric 117fe6060f1SDimitry Andric template <class _U1, class _U2> 118cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair(_U1&& __t1, _U2&& __t2) 119cb14a3feSDimitry Andric : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {} 120fe6060f1SDimitry Andric 121fe6060f1SDimitry Andric #ifndef _LIBCPP_CXX03_LANG 122fe6060f1SDimitry Andric template <class... _Args1, class... _Args2> 123cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 explicit __compressed_pair( 124cb14a3feSDimitry Andric piecewise_construct_t __pc, tuple<_Args1...> __first_args, tuple<_Args2...> __second_args) 12581ad6265SDimitry Andric : _Base1(__pc, std::move(__first_args), typename __make_tuple_indices<sizeof...(_Args1)>::type()), 12681ad6265SDimitry Andric _Base2(__pc, std::move(__second_args), typename __make_tuple_indices<sizeof...(_Args2)>::type()) {} 127fe6060f1SDimitry Andric #endif 128fe6060f1SDimitry Andric 129cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename _Base1::reference first() _NOEXCEPT { 130fe6060f1SDimitry Andric return static_cast<_Base1&>(*this).__get(); 131fe6060f1SDimitry Andric } 132fe6060f1SDimitry Andric 133cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR typename _Base1::const_reference first() const _NOEXCEPT { 134fe6060f1SDimitry Andric return static_cast<_Base1 const&>(*this).__get(); 135fe6060f1SDimitry Andric } 136fe6060f1SDimitry Andric 137cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename _Base2::reference second() _NOEXCEPT { 138fe6060f1SDimitry Andric return static_cast<_Base2&>(*this).__get(); 139fe6060f1SDimitry Andric } 140fe6060f1SDimitry Andric 141cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR typename _Base2::const_reference second() const _NOEXCEPT { 142fe6060f1SDimitry Andric return static_cast<_Base2 const&>(*this).__get(); 143fe6060f1SDimitry Andric } 144fe6060f1SDimitry Andric 145cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static _Base1* __get_first_base(__compressed_pair* __pair) _NOEXCEPT { 146fe6060f1SDimitry Andric return static_cast<_Base1*>(__pair); 147fe6060f1SDimitry Andric } 148cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static _Base2* __get_second_base(__compressed_pair* __pair) _NOEXCEPT { 149fe6060f1SDimitry Andric return static_cast<_Base2*>(__pair); 150fe6060f1SDimitry Andric } 151fe6060f1SDimitry Andric 152cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void swap(__compressed_pair& __x) 153*0fca6ea1SDimitry Andric _NOEXCEPT_(__is_nothrow_swappable_v<_T1>&& __is_nothrow_swappable_v<_T2>) { 15481ad6265SDimitry Andric using std::swap; 155fe6060f1SDimitry Andric swap(first(), __x.first()); 156fe6060f1SDimitry Andric swap(second(), __x.second()); 157fe6060f1SDimitry Andric } 158fe6060f1SDimitry Andric }; 159fe6060f1SDimitry Andric 160fe6060f1SDimitry Andric template <class _T1, class _T2> 161cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void 162cb14a3feSDimitry Andric swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y) 163*0fca6ea1SDimitry Andric _NOEXCEPT_(__is_nothrow_swappable_v<_T1>&& __is_nothrow_swappable_v<_T2>) { 164fe6060f1SDimitry Andric __x.swap(__y); 165fe6060f1SDimitry Andric } 166fe6060f1SDimitry Andric 167fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD 168fe6060f1SDimitry Andric 16906c3fb27SDimitry Andric _LIBCPP_POP_MACROS 17006c3fb27SDimitry Andric 171fe6060f1SDimitry Andric #endif // _LIBCPP___MEMORY_COMPRESSED_PAIR_H 172