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/get.h>
15bdd1243dSDimitry Andric #include <__fwd/tuple.h>
1606c3fb27SDimitry Andric #include <__tuple/tuple_indices.h>
17bdd1243dSDimitry Andric #include <__type_traits/decay.h>
18bdd1243dSDimitry Andric #include <__type_traits/dependent_type.h>
19bdd1243dSDimitry Andric #include <__type_traits/enable_if.h>
20bdd1243dSDimitry Andric #include <__type_traits/is_default_constructible.h>
21bdd1243dSDimitry Andric #include <__type_traits/is_empty.h>
22bdd1243dSDimitry Andric #include <__type_traits/is_final.h>
23bdd1243dSDimitry Andric #include <__type_traits/is_same.h>
24bdd1243dSDimitry Andric #include <__type_traits/is_swappable.h>
25fe6060f1SDimitry Andric #include <__utility/forward.h>
2681ad6265SDimitry Andric #include <__utility/move.h>
27bdd1243dSDimitry Andric #include <__utility/piecewise_construct.h>
28bdd1243dSDimitry Andric #include <cstddef>
29fe6060f1SDimitry Andric 
30fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
31fe6060f1SDimitry Andric #  pragma GCC system_header
32fe6060f1SDimitry Andric #endif
33fe6060f1SDimitry Andric 
3406c3fb27SDimitry Andric _LIBCPP_PUSH_MACROS
3506c3fb27SDimitry Andric #include <__undef_macros>
3606c3fb27SDimitry Andric 
37fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
38fe6060f1SDimitry Andric 
39fe6060f1SDimitry Andric // Tag used to default initialize one or both of the pair's elements.
40fe6060f1SDimitry Andric struct __default_init_tag {};
41fe6060f1SDimitry Andric struct __value_init_tag {};
42fe6060f1SDimitry Andric 
4381ad6265SDimitry Andric template <class _Tp, int _Idx, bool _CanBeEmptyBase = is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
44fe6060f1SDimitry Andric struct __compressed_pair_elem {
4581ad6265SDimitry Andric   using _ParamT         = _Tp;
4681ad6265SDimitry Andric   using reference       = _Tp&;
4781ad6265SDimitry Andric   using const_reference = const _Tp&;
48fe6060f1SDimitry Andric 
__compressed_pair_elem__compressed_pair_elem4981ad6265SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__default_init_tag) {}
__compressed_pair_elem__compressed_pair_elem5081ad6265SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__value_init_tag) : __value_() {}
51fe6060f1SDimitry Andric 
5206c3fb27SDimitry Andric   template <class _Up, class = __enable_if_t<!is_same<__compressed_pair_elem, __decay_t<_Up> >::value> >
__compressed_pair_elem__compressed_pair_elem53*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(_Up&& __u)
54*cb14a3feSDimitry Andric       : __value_(std::forward<_Up>(__u)) {}
55fe6060f1SDimitry Andric 
56fe6060f1SDimitry Andric #ifndef _LIBCPP_CXX03_LANG
5781ad6265SDimitry Andric   template <class... _Args, size_t... _Indices>
__compressed_pair_elem__compressed_pair_elem58*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 explicit __compressed_pair_elem(
59*cb14a3feSDimitry Andric       piecewise_construct_t, tuple<_Args...> __args, __tuple_indices<_Indices...>)
6081ad6265SDimitry Andric       : __value_(std::forward<_Args>(std::get<_Indices>(__args))...) {}
61fe6060f1SDimitry Andric #endif
62fe6060f1SDimitry Andric 
__get__compressed_pair_elem63bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference __get() _NOEXCEPT { return __value_; }
__get__compressed_pair_elem6481ad6265SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference __get() const _NOEXCEPT { return __value_; }
65fe6060f1SDimitry Andric 
66fe6060f1SDimitry Andric private:
67fe6060f1SDimitry Andric   _Tp __value_;
68fe6060f1SDimitry Andric };
69fe6060f1SDimitry Andric 
70fe6060f1SDimitry Andric template <class _Tp, int _Idx>
71fe6060f1SDimitry Andric struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
7281ad6265SDimitry Andric   using _ParamT         = _Tp;
7381ad6265SDimitry Andric   using reference       = _Tp&;
7481ad6265SDimitry Andric   using const_reference = const _Tp&;
7581ad6265SDimitry Andric   using __value_type    = _Tp;
76fe6060f1SDimitry Andric 
7781ad6265SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem() = default;
7881ad6265SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__default_init_tag) {}
7981ad6265SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__value_init_tag) : __value_type() {}
80fe6060f1SDimitry Andric 
8106c3fb27SDimitry Andric   template <class _Up, class = __enable_if_t<!is_same<__compressed_pair_elem, __decay_t<_Up> >::value> >
82*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(_Up&& __u)
83*cb14a3feSDimitry Andric       : __value_type(std::forward<_Up>(__u)) {}
84fe6060f1SDimitry Andric 
85fe6060f1SDimitry Andric #ifndef _LIBCPP_CXX03_LANG
8681ad6265SDimitry Andric   template <class... _Args, size_t... _Indices>
87bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17
8881ad6265SDimitry Andric   __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args, __tuple_indices<_Indices...>)
8981ad6265SDimitry Andric       : __value_type(std::forward<_Args>(std::get<_Indices>(__args))...) {}
90fe6060f1SDimitry Andric #endif
91fe6060f1SDimitry Andric 
92bdd1243dSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference __get() _NOEXCEPT { return *this; }
9381ad6265SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference __get() const _NOEXCEPT { return *this; }
94fe6060f1SDimitry Andric };
95fe6060f1SDimitry Andric 
96fe6060f1SDimitry Andric template <class _T1, class _T2>
97*cb14a3feSDimitry Andric class __compressed_pair : private __compressed_pair_elem<_T1, 0>, private __compressed_pair_elem<_T2, 1> {
98fe6060f1SDimitry Andric public:
99fe6060f1SDimitry Andric   // NOTE: This static assert should never fire because __compressed_pair
100fe6060f1SDimitry Andric   // is *almost never* used in a scenario where it's possible for T1 == T2.
101fe6060f1SDimitry Andric   // (The exception is std::function where it is possible that the function
102fe6060f1SDimitry Andric   //  object and the allocator have the same type).
103*cb14a3feSDimitry Andric   static_assert(
104*cb14a3feSDimitry Andric       (!is_same<_T1, _T2>::value),
105fe6060f1SDimitry Andric       "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
10681ad6265SDimitry Andric       "The current implementation is NOT ABI-compatible with the previous implementation for this configuration");
107fe6060f1SDimitry Andric 
10881ad6265SDimitry Andric   using _Base1 _LIBCPP_NODEBUG = __compressed_pair_elem<_T1, 0>;
10981ad6265SDimitry Andric   using _Base2 _LIBCPP_NODEBUG = __compressed_pair_elem<_T2, 1>;
110fe6060f1SDimitry Andric 
111fe6060f1SDimitry Andric   template <bool _Dummy = true,
112*cb14a3feSDimitry Andric             class       = __enable_if_t< __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
113*cb14a3feSDimitry Andric                                    __dependent_type<is_default_constructible<_T2>, _Dummy>::value > >
114*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair()
115*cb14a3feSDimitry Andric       : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
116fe6060f1SDimitry Andric 
117fe6060f1SDimitry Andric   template <class _U1, class _U2>
118*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair(_U1&& __t1, _U2&& __t2)
119*cb14a3feSDimitry 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>
123*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 explicit __compressed_pair(
124*cb14a3feSDimitry 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 
129*cb14a3feSDimitry 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 
133*cb14a3feSDimitry 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 
137*cb14a3feSDimitry 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 
141*cb14a3feSDimitry 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 
145*cb14a3feSDimitry 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   }
148*cb14a3feSDimitry 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 
152*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void swap(__compressed_pair& __x)
1530eae32dcSDimitry Andric       _NOEXCEPT_(__is_nothrow_swappable<_T1>::value&& __is_nothrow_swappable<_T2>::value) {
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>
161*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
162*cb14a3feSDimitry Andric swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
1630eae32dcSDimitry Andric     _NOEXCEPT_(__is_nothrow_swappable<_T1>::value&& __is_nothrow_swappable<_T2>::value) {
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