compressed_pair.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #ifndef _LIBCPP___MEMORY_COMPRESSED_PAIR_H
  10. #define _LIBCPP___MEMORY_COMPRESSED_PAIR_H
  11. #include <__config>
  12. #include <__fwd/get.h>
  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_default_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>
  27. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  28. # pragma GCC system_header
  29. #endif
  30. _LIBCPP_PUSH_MACROS
  31. #include <__undef_macros>
  32. _LIBCPP_BEGIN_NAMESPACE_STD
  33. // Tag used to default initialize one or both of the pair's elements.
  34. struct __default_init_tag {};
  35. struct __value_init_tag {};
  36. template <class _Tp, int _Idx, bool _CanBeEmptyBase = is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
  37. struct __compressed_pair_elem {
  38. using _ParamT = _Tp;
  39. using reference = _Tp&;
  40. using const_reference = const _Tp&;
  41. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__default_init_tag) {}
  42. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__value_init_tag) : __value_() {}
  43. template <class _Up, class = __enable_if_t<!is_same<__compressed_pair_elem, __decay_t<_Up> >::value> >
  44. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
  45. explicit __compressed_pair_elem(_Up&& __u) : __value_(std::forward<_Up>(__u)) {}
  46. #ifndef _LIBCPP_CXX03_LANG
  47. template <class... _Args, size_t... _Indices>
  48. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17
  49. explicit __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args, __tuple_indices<_Indices...>)
  50. : __value_(std::forward<_Args>(std::get<_Indices>(__args))...) {}
  51. #endif
  52. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference __get() _NOEXCEPT { return __value_; }
  53. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference __get() const _NOEXCEPT { return __value_; }
  54. private:
  55. _Tp __value_;
  56. };
  57. template <class _Tp, int _Idx>
  58. struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
  59. using _ParamT = _Tp;
  60. using reference = _Tp&;
  61. using const_reference = const _Tp&;
  62. using __value_type = _Tp;
  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. template <class _Up, class = __enable_if_t<!is_same<__compressed_pair_elem, __decay_t<_Up> >::value> >
  67. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
  68. explicit __compressed_pair_elem(_Up&& __u) : __value_type(std::forward<_Up>(__u)) {}
  69. #ifndef _LIBCPP_CXX03_LANG
  70. template <class... _Args, size_t... _Indices>
  71. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17
  72. __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args, __tuple_indices<_Indices...>)
  73. : __value_type(std::forward<_Args>(std::get<_Indices>(__args))...) {}
  74. #endif
  75. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference __get() _NOEXCEPT { return *this; }
  76. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference __get() const _NOEXCEPT { return *this; }
  77. };
  78. template <class _T1, class _T2>
  79. class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
  80. private __compressed_pair_elem<_T2, 1> {
  81. public:
  82. // NOTE: This static assert should never fire because __compressed_pair
  83. // is *almost never* used in a scenario where it's possible for T1 == T2.
  84. // (The exception is std::function where it is possible that the function
  85. // object and the allocator have the same type).
  86. static_assert((!is_same<_T1, _T2>::value),
  87. "__compressed_pair cannot be instantiated when T1 and T2 are the same type; "
  88. "The current implementation is NOT ABI-compatible with the previous implementation for this configuration");
  89. using _Base1 _LIBCPP_NODEBUG = __compressed_pair_elem<_T1, 0>;
  90. using _Base2 _LIBCPP_NODEBUG = __compressed_pair_elem<_T2, 1>;
  91. template <bool _Dummy = true,
  92. class = __enable_if_t<
  93. __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
  94. __dependent_type<is_default_constructible<_T2>, _Dummy>::value
  95. >
  96. >
  97. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
  98. explicit __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
  99. template <class _U1, class _U2>
  100. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
  101. explicit __compressed_pair(_U1&& __t1, _U2&& __t2) : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
  102. #ifndef _LIBCPP_CXX03_LANG
  103. template <class... _Args1, class... _Args2>
  104. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17
  105. explicit __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
  106. tuple<_Args2...> __second_args)
  107. : _Base1(__pc, std::move(__first_args), typename __make_tuple_indices<sizeof...(_Args1)>::type()),
  108. _Base2(__pc, std::move(__second_args), typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
  109. #endif
  110. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  111. typename _Base1::reference first() _NOEXCEPT {
  112. return static_cast<_Base1&>(*this).__get();
  113. }
  114. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
  115. typename _Base1::const_reference first() const _NOEXCEPT {
  116. return static_cast<_Base1 const&>(*this).__get();
  117. }
  118. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  119. typename _Base2::reference second() _NOEXCEPT {
  120. return static_cast<_Base2&>(*this).__get();
  121. }
  122. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
  123. typename _Base2::const_reference second() const _NOEXCEPT {
  124. return static_cast<_Base2 const&>(*this).__get();
  125. }
  126. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static
  127. _Base1* __get_first_base(__compressed_pair* __pair) _NOEXCEPT {
  128. return static_cast<_Base1*>(__pair);
  129. }
  130. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static
  131. _Base2* __get_second_base(__compressed_pair* __pair) _NOEXCEPT {
  132. return static_cast<_Base2*>(__pair);
  133. }
  134. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  135. void swap(__compressed_pair& __x)
  136. _NOEXCEPT_(__is_nothrow_swappable<_T1>::value && __is_nothrow_swappable<_T2>::value) {
  137. using std::swap;
  138. swap(first(), __x.first());
  139. swap(second(), __x.second());
  140. }
  141. };
  142. template <class _T1, class _T2>
  143. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14
  144. void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
  145. _NOEXCEPT_(__is_nothrow_swappable<_T1>::value && __is_nothrow_swappable<_T2>::value) {
  146. __x.swap(__y);
  147. }
  148. _LIBCPP_END_NAMESPACE_STD
  149. _LIBCPP_POP_MACROS
  150. #endif // _LIBCPP___MEMORY_COMPRESSED_PAIR_H