common_policy_traits.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2022 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef Y_ABSL_CONTAINER_INTERNAL_COMMON_POLICY_TRAITS_H_
  15. #define Y_ABSL_CONTAINER_INTERNAL_COMMON_POLICY_TRAITS_H_
  16. #include <cstddef>
  17. #include <cstring>
  18. #include <memory>
  19. #include <new>
  20. #include <type_traits>
  21. #include <utility>
  22. #include "y_absl/meta/type_traits.h"
  23. namespace y_absl {
  24. Y_ABSL_NAMESPACE_BEGIN
  25. namespace container_internal {
  26. // Defines how slots are initialized/destroyed/moved.
  27. template <class Policy, class = void>
  28. struct common_policy_traits {
  29. // The actual object stored in the container.
  30. using slot_type = typename Policy::slot_type;
  31. using reference = decltype(Policy::element(std::declval<slot_type*>()));
  32. using value_type = typename std::remove_reference<reference>::type;
  33. // PRECONDITION: `slot` is UNINITIALIZED
  34. // POSTCONDITION: `slot` is INITIALIZED
  35. template <class Alloc, class... Args>
  36. static void construct(Alloc* alloc, slot_type* slot, Args&&... args) {
  37. Policy::construct(alloc, slot, std::forward<Args>(args)...);
  38. }
  39. // PRECONDITION: `slot` is INITIALIZED
  40. // POSTCONDITION: `slot` is UNINITIALIZED
  41. // Returns std::true_type in case destroy is trivial.
  42. template <class Alloc>
  43. static auto destroy(Alloc* alloc, slot_type* slot) {
  44. return Policy::destroy(alloc, slot);
  45. }
  46. // Transfers the `old_slot` to `new_slot`. Any memory allocated by the
  47. // allocator inside `old_slot` to `new_slot` can be transferred.
  48. //
  49. // OPTIONAL: defaults to:
  50. //
  51. // clone(new_slot, std::move(*old_slot));
  52. // destroy(old_slot);
  53. //
  54. // PRECONDITION: `new_slot` is UNINITIALIZED and `old_slot` is INITIALIZED
  55. // POSTCONDITION: `new_slot` is INITIALIZED and `old_slot` is
  56. // UNINITIALIZED
  57. template <class Alloc>
  58. static void transfer(Alloc* alloc, slot_type* new_slot, slot_type* old_slot) {
  59. transfer_impl(alloc, new_slot, old_slot, Rank2{});
  60. }
  61. // PRECONDITION: `slot` is INITIALIZED
  62. // POSTCONDITION: `slot` is INITIALIZED
  63. // Note: we use remove_const_t so that the two overloads have different args
  64. // in the case of sets with explicitly const value_types.
  65. template <class P = Policy>
  66. static auto element(y_absl::remove_const_t<slot_type>* slot)
  67. -> decltype(P::element(slot)) {
  68. return P::element(slot);
  69. }
  70. template <class P = Policy>
  71. static auto element(const slot_type* slot) -> decltype(P::element(slot)) {
  72. return P::element(slot);
  73. }
  74. static constexpr bool transfer_uses_memcpy() {
  75. return std::is_same<decltype(transfer_impl<std::allocator<char>>(
  76. nullptr, nullptr, nullptr, Rank2{})),
  77. std::true_type>::value;
  78. }
  79. // Returns true if destroy is trivial and can be omitted.
  80. template <class Alloc>
  81. static constexpr bool destroy_is_trivial() {
  82. return std::is_same<decltype(destroy<Alloc>(nullptr, nullptr)),
  83. std::true_type>::value;
  84. }
  85. private:
  86. // Use go/ranked-overloads for dispatching.
  87. struct Rank0 {};
  88. struct Rank1 : Rank0 {};
  89. struct Rank2 : Rank1 {};
  90. // Use auto -> decltype as an enabler.
  91. // P::transfer returns std::true_type if transfer uses memcpy (e.g. in
  92. // node_slot_policy).
  93. template <class Alloc, class P = Policy>
  94. static auto transfer_impl(Alloc* alloc, slot_type* new_slot,
  95. slot_type* old_slot,
  96. Rank2) -> decltype(P::transfer(alloc, new_slot,
  97. old_slot)) {
  98. return P::transfer(alloc, new_slot, old_slot);
  99. }
  100. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
  101. // This overload returns true_type for the trait below.
  102. // The conditional_t is to make the enabler type dependent.
  103. template <class Alloc,
  104. typename = std::enable_if_t<y_absl::is_trivially_relocatable<
  105. std::conditional_t<false, Alloc, value_type>>::value>>
  106. static std::true_type transfer_impl(Alloc*, slot_type* new_slot,
  107. slot_type* old_slot, Rank1) {
  108. // TODO(b/247130232): remove casts after fixing warnings.
  109. // TODO(b/251814870): remove casts after fixing warnings.
  110. std::memcpy(
  111. static_cast<void*>(std::launder(
  112. const_cast<std::remove_const_t<value_type>*>(&element(new_slot)))),
  113. static_cast<const void*>(&element(old_slot)), sizeof(value_type));
  114. return {};
  115. }
  116. #endif
  117. template <class Alloc>
  118. static void transfer_impl(Alloc* alloc, slot_type* new_slot,
  119. slot_type* old_slot, Rank0) {
  120. construct(alloc, new_slot, std::move(element(old_slot)));
  121. destroy(alloc, old_slot);
  122. }
  123. };
  124. } // namespace container_internal
  125. Y_ABSL_NAMESPACE_END
  126. } // namespace y_absl
  127. #endif // Y_ABSL_CONTAINER_INTERNAL_COMMON_POLICY_TRAITS_H_