common_policy_traits.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. template <class Alloc>
  42. static void destroy(Alloc* alloc, slot_type* slot) {
  43. Policy::destroy(alloc, slot);
  44. }
  45. // Transfers the `old_slot` to `new_slot`. Any memory allocated by the
  46. // allocator inside `old_slot` to `new_slot` can be transferred.
  47. //
  48. // OPTIONAL: defaults to:
  49. //
  50. // clone(new_slot, std::move(*old_slot));
  51. // destroy(old_slot);
  52. //
  53. // PRECONDITION: `new_slot` is UNINITIALIZED and `old_slot` is INITIALIZED
  54. // POSTCONDITION: `new_slot` is INITIALIZED and `old_slot` is
  55. // UNINITIALIZED
  56. template <class Alloc>
  57. static void transfer(Alloc* alloc, slot_type* new_slot, slot_type* old_slot) {
  58. transfer_impl(alloc, new_slot, old_slot, Rank0{});
  59. }
  60. // PRECONDITION: `slot` is INITIALIZED
  61. // POSTCONDITION: `slot` is INITIALIZED
  62. // Note: we use remove_const_t so that the two overloads have different args
  63. // in the case of sets with explicitly const value_types.
  64. template <class P = Policy>
  65. static auto element(y_absl::remove_const_t<slot_type>* slot)
  66. -> decltype(P::element(slot)) {
  67. return P::element(slot);
  68. }
  69. template <class P = Policy>
  70. static auto element(const slot_type* slot) -> decltype(P::element(slot)) {
  71. return P::element(slot);
  72. }
  73. static constexpr bool transfer_uses_memcpy() {
  74. return std::is_same<decltype(transfer_impl<std::allocator<char>>(
  75. nullptr, nullptr, nullptr, Rank0{})),
  76. std::true_type>::value;
  77. }
  78. private:
  79. // To rank the overloads below for overload resolution. Rank0 is preferred.
  80. struct Rank2 {};
  81. struct Rank1 : Rank2 {};
  82. struct Rank0 : Rank1 {};
  83. // Use auto -> decltype as an enabler.
  84. template <class Alloc, class P = Policy>
  85. static auto transfer_impl(Alloc* alloc, slot_type* new_slot,
  86. slot_type* old_slot, Rank0)
  87. -> decltype((void)P::transfer(alloc, new_slot, old_slot)) {
  88. P::transfer(alloc, new_slot, old_slot);
  89. }
  90. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
  91. // This overload returns true_type for the trait below.
  92. // The conditional_t is to make the enabler type dependent.
  93. template <class Alloc,
  94. typename = std::enable_if_t<y_absl::is_trivially_relocatable<
  95. std::conditional_t<false, Alloc, value_type>>::value>>
  96. static std::true_type transfer_impl(Alloc*, slot_type* new_slot,
  97. slot_type* old_slot, Rank1) {
  98. // TODO(b/247130232): remove casts after fixing warnings.
  99. // TODO(b/251814870): remove casts after fixing warnings.
  100. std::memcpy(
  101. static_cast<void*>(std::launder(
  102. const_cast<std::remove_const_t<value_type>*>(&element(new_slot)))),
  103. static_cast<const void*>(&element(old_slot)), sizeof(value_type));
  104. return {};
  105. }
  106. #endif
  107. template <class Alloc>
  108. static void transfer_impl(Alloc* alloc, slot_type* new_slot,
  109. slot_type* old_slot, Rank2) {
  110. construct(alloc, new_slot, std::move(element(old_slot)));
  111. destroy(alloc, old_slot);
  112. }
  113. };
  114. } // namespace container_internal
  115. Y_ABSL_NAMESPACE_END
  116. } // namespace y_absl
  117. #endif // Y_ABSL_CONTAINER_INTERNAL_COMMON_POLICY_TRAITS_H_