xref: /freebsd/contrib/llvm-project/libcxx/include/__memory/compressed_pair.h (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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 <__fwd/get.h>
15 #include <__fwd/tuple.h>
16 #include <__tuple/tuple_indices.h>
17 #include <__type_traits/decay.h>
18 #include <__type_traits/dependent_type.h>
19 #include <__type_traits/enable_if.h>
20 #include <__type_traits/is_default_constructible.h>
21 #include <__type_traits/is_empty.h>
22 #include <__type_traits/is_final.h>
23 #include <__type_traits/is_same.h>
24 #include <__type_traits/is_swappable.h>
25 #include <__utility/forward.h>
26 #include <__utility/move.h>
27 #include <__utility/piecewise_construct.h>
28 #include <cstddef>
29 
30 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
31 #  pragma GCC system_header
32 #endif
33 
34 _LIBCPP_PUSH_MACROS
35 #include <__undef_macros>
36 
37 _LIBCPP_BEGIN_NAMESPACE_STD
38 
39 // Tag used to default initialize one or both of the pair's elements.
40 struct __default_init_tag {};
41 struct __value_init_tag {};
42 
43 template <class _Tp, int _Idx, bool _CanBeEmptyBase = is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
44 struct __compressed_pair_elem {
45   using _ParamT = _Tp;
46   using reference = _Tp&;
47   using const_reference = const _Tp&;
48 
49   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__default_init_tag) {}
50   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__value_init_tag) : __value_() {}
51 
52   template <class _Up, class = __enable_if_t<!is_same<__compressed_pair_elem, __decay_t<_Up> >::value> >
53   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
54   explicit __compressed_pair_elem(_Up&& __u) : __value_(std::forward<_Up>(__u)) {}
55 
56 #ifndef _LIBCPP_CXX03_LANG
57   template <class... _Args, size_t... _Indices>
58   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17
59   explicit __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args, __tuple_indices<_Indices...>)
60       : __value_(std::forward<_Args>(std::get<_Indices>(__args))...) {}
61 #endif
62 
63   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference __get() _NOEXCEPT { return __value_; }
64   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference __get() const _NOEXCEPT { return __value_; }
65 
66 private:
67   _Tp __value_;
68 };
69 
70 template <class _Tp, int _Idx>
71 struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
72   using _ParamT = _Tp;
73   using reference = _Tp&;
74   using const_reference = const _Tp&;
75   using __value_type = _Tp;
76 
77   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem() = default;
78   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__default_init_tag) {}
79   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__value_init_tag) : __value_type() {}
80 
81   template <class _Up, class = __enable_if_t<!is_same<__compressed_pair_elem, __decay_t<_Up> >::value> >
82   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
83   explicit __compressed_pair_elem(_Up&& __u) : __value_type(std::forward<_Up>(__u)) {}
84 
85 #ifndef _LIBCPP_CXX03_LANG
86   template <class... _Args, size_t... _Indices>
87   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17
88   __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args, __tuple_indices<_Indices...>)
89       : __value_type(std::forward<_Args>(std::get<_Indices>(__args))...) {}
90 #endif
91 
92   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference __get() _NOEXCEPT { return *this; }
93   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference __get() const _NOEXCEPT { return *this; }
94 };
95 
96 template <class _T1, class _T2>
97 class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
98                           private __compressed_pair_elem<_T2, 1> {
99 public:
100   // NOTE: This static assert should never fire because __compressed_pair
101   // is *almost never* used in a scenario where it's possible for T1 == T2.
102   // (The exception is std::function where it is possible that the function
103   //  object and the allocator have the same type).
104   static_assert((!is_same<_T1, _T2>::value),
105     "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
106     "The current implementation is NOT ABI-compatible with the previous implementation for this configuration");
107 
108   using _Base1 _LIBCPP_NODEBUG = __compressed_pair_elem<_T1, 0>;
109   using _Base2 _LIBCPP_NODEBUG = __compressed_pair_elem<_T2, 1>;
110 
111   template <bool _Dummy = true,
112     class = __enable_if_t<
113         __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
114         __dependent_type<is_default_constructible<_T2>, _Dummy>::value
115     >
116   >
117   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
118   explicit __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
119 
120   template <class _U1, class _U2>
121   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
122   explicit __compressed_pair(_U1&& __t1, _U2&& __t2) : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
123 
124 #ifndef _LIBCPP_CXX03_LANG
125   template <class... _Args1, class... _Args2>
126   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17
127   explicit __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
128                              tuple<_Args2...> __second_args)
129       : _Base1(__pc, std::move(__first_args), typename __make_tuple_indices<sizeof...(_Args1)>::type()),
130         _Base2(__pc, std::move(__second_args), typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
131 #endif
132 
133   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
134   typename _Base1::reference first() _NOEXCEPT {
135     return static_cast<_Base1&>(*this).__get();
136   }
137 
138   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
139   typename _Base1::const_reference first() const _NOEXCEPT {
140     return static_cast<_Base1 const&>(*this).__get();
141   }
142 
143   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
144   typename _Base2::reference second() _NOEXCEPT {
145     return static_cast<_Base2&>(*this).__get();
146   }
147 
148   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
149   typename _Base2::const_reference second() const _NOEXCEPT {
150     return static_cast<_Base2 const&>(*this).__get();
151   }
152 
153   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static
154   _Base1* __get_first_base(__compressed_pair* __pair) _NOEXCEPT {
155     return static_cast<_Base1*>(__pair);
156   }
157   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static
158   _Base2* __get_second_base(__compressed_pair* __pair) _NOEXCEPT {
159     return static_cast<_Base2*>(__pair);
160   }
161 
162   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
163   void swap(__compressed_pair& __x)
164       _NOEXCEPT_(__is_nothrow_swappable<_T1>::value && __is_nothrow_swappable<_T2>::value) {
165     using std::swap;
166     swap(first(), __x.first());
167     swap(second(), __x.second());
168   }
169 };
170 
171 template <class _T1, class _T2>
172 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
173 void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
174     _NOEXCEPT_(__is_nothrow_swappable<_T1>::value && __is_nothrow_swappable<_T2>::value) {
175   __x.swap(__y);
176 }
177 
178 _LIBCPP_END_NAMESPACE_STD
179 
180 _LIBCPP_POP_MACROS
181 
182 #endif // _LIBCPP___MEMORY_COMPRESSED_PAIR_H
183