common_policy_traits.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 ABSL_CONTAINER_INTERNAL_COMMON_POLICY_TRAITS_H_
  15. #define 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 "absl/meta/type_traits.h"
  23. namespace absl {
  24. 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(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. // P::transfer returns std::true_type if transfer uses memcpy (e.g. in
  85. // node_slot_policy).
  86. template <class Alloc, class P = Policy>
  87. static auto transfer_impl(Alloc* alloc, slot_type* new_slot,
  88. slot_type* old_slot, Rank0)
  89. -> decltype(P::transfer(alloc, new_slot, old_slot)) {
  90. return P::transfer(alloc, new_slot, old_slot);
  91. }
  92. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
  93. // This overload returns true_type for the trait below.
  94. // The conditional_t is to make the enabler type dependent.
  95. template <class Alloc,
  96. typename = std::enable_if_t<absl::is_trivially_relocatable<
  97. std::conditional_t<false, Alloc, value_type>>::value>>
  98. static std::true_type transfer_impl(Alloc*, slot_type* new_slot,
  99. slot_type* old_slot, Rank1) {
  100. // TODO(b/247130232): remove casts after fixing warnings.
  101. // TODO(b/251814870): remove casts after fixing warnings.
  102. std::memcpy(
  103. static_cast<void*>(std::launder(
  104. const_cast<std::remove_const_t<value_type>*>(&element(new_slot)))),
  105. static_cast<const void*>(&element(old_slot)), sizeof(value_type));
  106. return {};
  107. }
  108. #endif
  109. template <class Alloc>
  110. static void transfer_impl(Alloc* alloc, slot_type* new_slot,
  111. slot_type* old_slot, Rank2) {
  112. construct(alloc, new_slot, std::move(element(old_slot)));
  113. destroy(alloc, old_slot);
  114. }
  115. };
  116. } // namespace container_internal
  117. ABSL_NAMESPACE_END
  118. } // namespace absl
  119. #endif // ABSL_CONTAINER_INTERNAL_COMMON_POLICY_TRAITS_H_