1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___MEMORY_COMPRESSED_PAIR_H
11 #define _LIBCPP___MEMORY_COMPRESSED_PAIR_H
12 
13 #include <__config>
14 #include <__utility/forward.h>
15 #include <__utility/move.h>
16 #include <tuple> // needed in c++03 for some constructors
17 #include <type_traits>
18 
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #  pragma GCC system_header
21 #endif
22 
23 _LIBCPP_BEGIN_NAMESPACE_STD
24 
25 // Tag used to default initialize one or both of the pair's elements.
26 struct __default_init_tag {};
27 struct __value_init_tag {};
28 
29 template <class _Tp, int _Idx, bool _CanBeEmptyBase = is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
30 struct __compressed_pair_elem {
31   using _ParamT = _Tp;
32   using reference = _Tp&;
33   using const_reference = const _Tp&;
34 
35   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__default_init_tag) {}
36   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__value_init_tag) : __value_() {}
37 
38   template <class _Up, class = __enable_if_t<!is_same<__compressed_pair_elem, typename decay<_Up>::type>::value> >
39   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
40   explicit __compressed_pair_elem(_Up&& __u) : __value_(std::forward<_Up>(__u)) {}
41 
42 #ifndef _LIBCPP_CXX03_LANG
43   template <class... _Args, size_t... _Indices>
44   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
45   explicit __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args, __tuple_indices<_Indices...>)
46       : __value_(std::forward<_Args>(std::get<_Indices>(__args))...) {}
47 #endif
48 
49   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 reference __get() _NOEXCEPT { return __value_; }
50   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference __get() const _NOEXCEPT { return __value_; }
51 
52 private:
53   _Tp __value_;
54 };
55 
56 template <class _Tp, int _Idx>
57 struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
58   using _ParamT = _Tp;
59   using reference = _Tp&;
60   using const_reference = const _Tp&;
61   using __value_type = _Tp;
62 
63   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem() = default;
64   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__default_init_tag) {}
65   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__value_init_tag) : __value_type() {}
66 
67   template <class _Up, class = __enable_if_t<!is_same<__compressed_pair_elem, typename decay<_Up>::type>::value> >
68   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
69   explicit __compressed_pair_elem(_Up&& __u) : __value_type(std::forward<_Up>(__u)) {}
70 
71 #ifndef _LIBCPP_CXX03_LANG
72   template <class... _Args, size_t... _Indices>
73   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
74   __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args, __tuple_indices<_Indices...>)
75       : __value_type(std::forward<_Args>(std::get<_Indices>(__args))...) {}
76 #endif
77 
78   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 reference __get() _NOEXCEPT { return *this; }
79   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference __get() const _NOEXCEPT { return *this; }
80 };
81 
82 template <class _T1, class _T2>
83 class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
84                           private __compressed_pair_elem<_T2, 1> {
85 public:
86   // NOTE: This static assert should never fire because __compressed_pair
87   // is *almost never* used in a scenario where it's possible for T1 == T2.
88   // (The exception is std::function where it is possible that the function
89   //  object and the allocator have the same type).
90   static_assert((!is_same<_T1, _T2>::value),
91     "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
92     "The current implementation is NOT ABI-compatible with the previous implementation for this configuration");
93 
94   using _Base1 _LIBCPP_NODEBUG = __compressed_pair_elem<_T1, 0>;
95   using _Base2 _LIBCPP_NODEBUG = __compressed_pair_elem<_T2, 1>;
96 
97   template <bool _Dummy = true,
98     class = __enable_if_t<
99         __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
100         __dependent_type<is_default_constructible<_T2>, _Dummy>::value
101     >
102   >
103   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
104   explicit __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
105 
106   template <class _U1, class _U2>
107   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
108   explicit __compressed_pair(_U1&& __t1, _U2&& __t2) : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
109 
110 #ifndef _LIBCPP_CXX03_LANG
111   template <class... _Args1, class... _Args2>
112   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
113   explicit __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
114                              tuple<_Args2...> __second_args)
115       : _Base1(__pc, std::move(__first_args), typename __make_tuple_indices<sizeof...(_Args1)>::type()),
116         _Base2(__pc, std::move(__second_args), typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
117 #endif
118 
119   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
120   typename _Base1::reference first() _NOEXCEPT {
121     return static_cast<_Base1&>(*this).__get();
122   }
123 
124   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
125   typename _Base1::const_reference first() const _NOEXCEPT {
126     return static_cast<_Base1 const&>(*this).__get();
127   }
128 
129   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
130   typename _Base2::reference second() _NOEXCEPT {
131     return static_cast<_Base2&>(*this).__get();
132   }
133 
134   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
135   typename _Base2::const_reference second() const _NOEXCEPT {
136     return static_cast<_Base2 const&>(*this).__get();
137   }
138 
139   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static
140   _Base1* __get_first_base(__compressed_pair* __pair) _NOEXCEPT {
141     return static_cast<_Base1*>(__pair);
142   }
143   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static
144   _Base2* __get_second_base(__compressed_pair* __pair) _NOEXCEPT {
145     return static_cast<_Base2*>(__pair);
146   }
147 
148   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
149   void swap(__compressed_pair& __x)
150       _NOEXCEPT_(__is_nothrow_swappable<_T1>::value && __is_nothrow_swappable<_T2>::value) {
151     using std::swap;
152     swap(first(), __x.first());
153     swap(second(), __x.second());
154   }
155 };
156 
157 template <class _T1, class _T2>
158 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
159 void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
160     _NOEXCEPT_(__is_nothrow_swappable<_T1>::value && __is_nothrow_swappable<_T2>::value) {
161   __x.swap(__y);
162 }
163 
164 _LIBCPP_END_NAMESPACE_STD
165 
166 #endif // _LIBCPP___MEMORY_COMPRESSED_PAIR_H
167