compressed_pair.h 7.1 KB

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