Path: blob/main/contrib/llvm-project/libcxx/include/__memory/compressed_pair.h
35233 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___MEMORY_COMPRESSED_PAIR_H10#define _LIBCPP___MEMORY_COMPRESSED_PAIR_H1112#include <__config>13#include <__fwd/tuple.h>14#include <__tuple/tuple_indices.h>15#include <__type_traits/decay.h>16#include <__type_traits/dependent_type.h>17#include <__type_traits/enable_if.h>18#include <__type_traits/is_constructible.h>19#include <__type_traits/is_empty.h>20#include <__type_traits/is_final.h>21#include <__type_traits/is_same.h>22#include <__type_traits/is_swappable.h>23#include <__utility/forward.h>24#include <__utility/move.h>25#include <__utility/piecewise_construct.h>26#include <cstddef>2728#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)29# pragma GCC system_header30#endif3132_LIBCPP_PUSH_MACROS33#include <__undef_macros>3435_LIBCPP_BEGIN_NAMESPACE_STD3637// Tag used to default initialize one or both of the pair's elements.38struct __default_init_tag {};39struct __value_init_tag {};4041template <class _Tp, int _Idx, bool _CanBeEmptyBase = is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>42struct __compressed_pair_elem {43using _ParamT = _Tp;44using reference = _Tp&;45using const_reference = const _Tp&;4647_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__default_init_tag) {}48_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__value_init_tag) : __value_() {}4950template <class _Up, __enable_if_t<!is_same<__compressed_pair_elem, __decay_t<_Up> >::value, int> = 0>51_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(_Up&& __u)52: __value_(std::forward<_Up>(__u)) {}5354#ifndef _LIBCPP_CXX03_LANG55template <class... _Args, size_t... _Indices>56_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 explicit __compressed_pair_elem(57piecewise_construct_t, tuple<_Args...> __args, __tuple_indices<_Indices...>)58: __value_(std::forward<_Args>(std::get<_Indices>(__args))...) {}59#endif6061_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference __get() _NOEXCEPT { return __value_; }62_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference __get() const _NOEXCEPT { return __value_; }6364private:65_Tp __value_;66};6768template <class _Tp, int _Idx>69struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {70using _ParamT = _Tp;71using reference = _Tp&;72using const_reference = const _Tp&;73using __value_type = _Tp;7475_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem() = default;76_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__default_init_tag) {}77_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__value_init_tag) : __value_type() {}7879template <class _Up, __enable_if_t<!is_same<__compressed_pair_elem, __decay_t<_Up> >::value, int> = 0>80_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(_Up&& __u)81: __value_type(std::forward<_Up>(__u)) {}8283#ifndef _LIBCPP_CXX03_LANG84template <class... _Args, size_t... _Indices>85_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX1786__compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args, __tuple_indices<_Indices...>)87: __value_type(std::forward<_Args>(std::get<_Indices>(__args))...) {}88#endif8990_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference __get() _NOEXCEPT { return *this; }91_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference __get() const _NOEXCEPT { return *this; }92};9394template <class _T1, class _T2>95class __compressed_pair : private __compressed_pair_elem<_T1, 0>, private __compressed_pair_elem<_T2, 1> {96public:97// NOTE: This static assert should never fire because __compressed_pair98// is *almost never* used in a scenario where it's possible for T1 == T2.99// (The exception is std::function where it is possible that the function100// object and the allocator have the same type).101static_assert(102(!is_same<_T1, _T2>::value),103"__compressed_pair cannot be instantiated when T1 and T2 are the same type; "104"The current implementation is NOT ABI-compatible with the previous implementation for this configuration");105106using _Base1 _LIBCPP_NODEBUG = __compressed_pair_elem<_T1, 0>;107using _Base2 _LIBCPP_NODEBUG = __compressed_pair_elem<_T2, 1>;108109template <bool _Dummy = true,110__enable_if_t< __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&111__dependent_type<is_default_constructible<_T2>, _Dummy>::value,112int> = 0>113_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair()114: _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}115116template <class _U1, class _U2>117_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair(_U1&& __t1, _U2&& __t2)118: _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}119120#ifndef _LIBCPP_CXX03_LANG121template <class... _Args1, class... _Args2>122_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 explicit __compressed_pair(123piecewise_construct_t __pc, tuple<_Args1...> __first_args, tuple<_Args2...> __second_args)124: _Base1(__pc, std::move(__first_args), typename __make_tuple_indices<sizeof...(_Args1)>::type()),125_Base2(__pc, std::move(__second_args), typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}126#endif127128_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename _Base1::reference first() _NOEXCEPT {129return static_cast<_Base1&>(*this).__get();130}131132_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR typename _Base1::const_reference first() const _NOEXCEPT {133return static_cast<_Base1 const&>(*this).__get();134}135136_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename _Base2::reference second() _NOEXCEPT {137return static_cast<_Base2&>(*this).__get();138}139140_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR typename _Base2::const_reference second() const _NOEXCEPT {141return static_cast<_Base2 const&>(*this).__get();142}143144_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static _Base1* __get_first_base(__compressed_pair* __pair) _NOEXCEPT {145return static_cast<_Base1*>(__pair);146}147_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static _Base2* __get_second_base(__compressed_pair* __pair) _NOEXCEPT {148return static_cast<_Base2*>(__pair);149}150151_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void swap(__compressed_pair& __x)152_NOEXCEPT_(__is_nothrow_swappable_v<_T1>&& __is_nothrow_swappable_v<_T2>) {153using std::swap;154swap(first(), __x.first());155swap(second(), __x.second());156}157};158159template <class _T1, class _T2>160inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void161swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)162_NOEXCEPT_(__is_nothrow_swappable_v<_T1>&& __is_nothrow_swappable_v<_T2>) {163__x.swap(__y);164}165166_LIBCPP_END_NAMESPACE_STD167168_LIBCPP_POP_MACROS169170#endif // _LIBCPP___MEMORY_COMPRESSED_PAIR_H171172173