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 15 #ifndef ABSL_CONTAINER_INTERNAL_COMMON_POLICY_TRAITS_H_ 16 #define ABSL_CONTAINER_INTERNAL_COMMON_POLICY_TRAITS_H_ 17 18 #include <cstddef> 19 #include <cstring> 20 #include <memory> 21 #include <new> 22 #include <type_traits> 23 #include <utility> 24 25 #include "absl/meta/type_traits.h" 26 27 namespace absl { 28 ABSL_NAMESPACE_BEGIN 29 namespace container_internal { 30 31 // Defines how slots are initialized/destroyed/moved. 32 template <class Policy, class = void> 33 struct common_policy_traits { 34 // The actual object stored in the container. 35 using slot_type = typename Policy::slot_type; 36 using reference = decltype(Policy::element(std::declval<slot_type*>())); 37 using value_type = typename std::remove_reference<reference>::type; 38 39 // PRECONDITION: `slot` is UNINITIALIZED 40 // POSTCONDITION: `slot` is INITIALIZED 41 template <class Alloc, class... Args> constructcommon_policy_traits42 static void construct(Alloc* alloc, slot_type* slot, Args&&... args) { 43 Policy::construct(alloc, slot, std::forward<Args>(args)...); 44 } 45 46 // PRECONDITION: `slot` is INITIALIZED 47 // POSTCONDITION: `slot` is UNINITIALIZED 48 template <class Alloc> destroycommon_policy_traits49 static void destroy(Alloc* alloc, slot_type* slot) { 50 Policy::destroy(alloc, slot); 51 } 52 53 // Transfers the `old_slot` to `new_slot`. Any memory allocated by the 54 // allocator inside `old_slot` to `new_slot` can be transferred. 55 // 56 // OPTIONAL: defaults to: 57 // 58 // clone(new_slot, std::move(*old_slot)); 59 // destroy(old_slot); 60 // 61 // PRECONDITION: `new_slot` is UNINITIALIZED and `old_slot` is INITIALIZED 62 // POSTCONDITION: `new_slot` is INITIALIZED and `old_slot` is 63 // UNINITIALIZED 64 template <class Alloc> transfercommon_policy_traits65 static void transfer(Alloc* alloc, slot_type* new_slot, slot_type* old_slot) { 66 transfer_impl(alloc, new_slot, old_slot, Rank0{}); 67 } 68 69 // PRECONDITION: `slot` is INITIALIZED 70 // POSTCONDITION: `slot` is INITIALIZED 71 // Note: we use remove_const_t so that the two overloads have different args 72 // in the case of sets with explicitly const value_types. 73 template <class P = Policy> 74 static auto element(absl::remove_const_t<slot_type>* slot) 75 -> decltype(P::element(slot)) { 76 return P::element(slot); 77 } 78 template <class P = Policy> 79 static auto element(const slot_type* slot) -> decltype(P::element(slot)) { 80 return P::element(slot); 81 } 82 transfer_uses_memcpycommon_policy_traits83 static constexpr bool transfer_uses_memcpy() { 84 return std::is_same<decltype(transfer_impl<std::allocator<char>>( 85 nullptr, nullptr, nullptr, Rank0{})), 86 std::true_type>::value; 87 } 88 89 private: 90 // To rank the overloads below for overload resolution. Rank0 is preferred. 91 struct Rank2 {}; 92 struct Rank1 : Rank2 {}; 93 struct Rank0 : Rank1 {}; 94 95 // Use auto -> decltype as an enabler. 96 // P::transfer returns std::true_type if transfer uses memcpy (e.g. in 97 // node_slot_policy). 98 template <class Alloc, class P = Policy> 99 static auto transfer_impl(Alloc* alloc, slot_type* new_slot, 100 slot_type* old_slot, Rank0) 101 -> decltype(P::transfer(alloc, new_slot, old_slot)) { 102 return P::transfer(alloc, new_slot, old_slot); 103 } 104 #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606 105 // This overload returns true_type for the trait below. 106 // The conditional_t is to make the enabler type dependent. 107 template <class Alloc, 108 typename = std::enable_if_t<absl::is_trivially_relocatable< 109 std::conditional_t<false, Alloc, value_type>>::value>> transfer_implcommon_policy_traits110 static std::true_type transfer_impl(Alloc*, slot_type* new_slot, 111 slot_type* old_slot, Rank1) { 112 // TODO(b/247130232): remove casts after fixing warnings. 113 // TODO(b/251814870): remove casts after fixing warnings. 114 std::memcpy( 115 static_cast<void*>(std::launder( 116 const_cast<std::remove_const_t<value_type>*>(&element(new_slot)))), 117 static_cast<const void*>(&element(old_slot)), sizeof(value_type)); 118 return {}; 119 } 120 #endif 121 122 template <class Alloc> transfer_implcommon_policy_traits123 static void transfer_impl(Alloc* alloc, slot_type* new_slot, 124 slot_type* old_slot, Rank2) { 125 construct(alloc, new_slot, std::move(element(old_slot))); 126 destroy(alloc, old_slot); 127 } 128 }; 129 130 } // namespace container_internal 131 ABSL_NAMESPACE_END 132 } // namespace absl 133 134 #endif // ABSL_CONTAINER_INTERNAL_COMMON_POLICY_TRAITS_H_ 135