• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, 0);
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 
83  private:
84   // Use auto -> decltype as an enabler.
85   template <class Alloc, class P = Policy>
86   static auto transfer_impl(Alloc* alloc, slot_type* new_slot,
87                             slot_type* old_slot, int)
88       -> decltype((void)P::transfer(alloc, new_slot, old_slot)) {
89     P::transfer(alloc, new_slot, old_slot);
90   }
91   template <class Alloc>
transfer_implcommon_policy_traits92   static void transfer_impl(Alloc* alloc, slot_type* new_slot,
93                             slot_type* old_slot, char) {
94 #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
95     if (absl::is_trivially_relocatable<value_type>()) {
96       // TODO(b/247130232,b/251814870): remove casts after fixing warnings.
97       std::memcpy(static_cast<void*>(
98                       std::launder(const_cast<std::remove_const_t<value_type>*>(
99                           &element(new_slot)))),
100                   static_cast<const void*>(&element(old_slot)),
101                   sizeof(value_type));
102       return;
103     }
104 #endif
105 
106     construct(alloc, new_slot, std::move(element(old_slot)));
107     destroy(alloc, old_slot);
108   }
109 };
110 
111 }  // namespace container_internal
112 ABSL_NAMESPACE_END
113 }  // namespace absl
114 
115 #endif  // ABSL_CONTAINER_INTERNAL_COMMON_POLICY_TRAITS_H_
116