1 // Copyright 2018 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_CONTAINER_H_ 16 #define ABSL_CONTAINER_INTERNAL_CONTAINER_H_ 17 18 #include <cassert> 19 #include <type_traits> 20 21 #include "absl/meta/type_traits.h" 22 #include "absl/types/optional.h" 23 24 namespace absl { 25 ABSL_NAMESPACE_BEGIN 26 namespace container_internal { 27 28 template <class, class = void> 29 struct IsTransparent : std::false_type {}; 30 template <class T> 31 struct IsTransparent<T, absl::void_t<typename T::is_transparent>> 32 : std::true_type {}; 33 34 template <bool is_transparent> 35 struct KeyArg { 36 // Transparent. Forward `K`. 37 template <typename K, typename key_type> 38 using type = K; 39 }; 40 41 template <> 42 struct KeyArg<false> { 43 // Not transparent. Always use `key_type`. 44 template <typename K, typename key_type> 45 using type = key_type; 46 }; 47 48 // The node_handle concept from C++17. 49 // We specialize node_handle for sets and maps. node_handle_base holds the 50 // common API of both. 51 template <typename PolicyTraits, typename Alloc> 52 class node_handle_base { 53 protected: 54 using slot_type = typename PolicyTraits::slot_type; 55 56 public: 57 using allocator_type = Alloc; 58 59 constexpr node_handle_base() = default; 60 node_handle_base(node_handle_base&& other) noexcept { 61 *this = std::move(other); 62 } 63 ~node_handle_base() { destroy(); } 64 node_handle_base& operator=(node_handle_base&& other) noexcept { 65 destroy(); 66 if (!other.empty()) { 67 alloc_ = other.alloc_; 68 PolicyTraits::transfer(alloc(), slot(), other.slot()); 69 other.reset(); 70 } 71 return *this; 72 } 73 74 bool empty() const noexcept { return !alloc_; } 75 explicit operator bool() const noexcept { return !empty(); } 76 allocator_type get_allocator() const { return *alloc_; } 77 78 protected: 79 friend struct CommonAccess; 80 81 struct transfer_tag_t {}; 82 node_handle_base(transfer_tag_t, const allocator_type& a, slot_type* s) 83 : alloc_(a) { 84 PolicyTraits::transfer(alloc(), slot(), s); 85 } 86 87 struct move_tag_t {}; 88 node_handle_base(move_tag_t, const allocator_type& a, slot_type* s) 89 : alloc_(a) { 90 PolicyTraits::construct(alloc(), slot(), s); 91 } 92 93 void destroy() { 94 if (!empty()) { 95 PolicyTraits::destroy(alloc(), slot()); 96 reset(); 97 } 98 } 99 100 void reset() { 101 assert(alloc_.has_value()); 102 alloc_ = absl::nullopt; 103 } 104 105 slot_type* slot() const { 106 assert(!empty()); 107 return reinterpret_cast<slot_type*>(std::addressof(slot_space_)); 108 } 109 allocator_type* alloc() { return std::addressof(*alloc_); } 110 111 private: 112 absl::optional<allocator_type> alloc_ = {}; 113 alignas(slot_type) mutable unsigned char slot_space_[sizeof(slot_type)] = {}; 114 }; 115 116 // For sets. 117 template <typename Policy, typename PolicyTraits, typename Alloc, 118 typename = void> 119 class node_handle : public node_handle_base<PolicyTraits, Alloc> { 120 using Base = node_handle_base<PolicyTraits, Alloc>; 121 122 public: 123 using value_type = typename PolicyTraits::value_type; 124 125 constexpr node_handle() {} 126 127 value_type& value() const { return PolicyTraits::element(this->slot()); } 128 129 private: 130 friend struct CommonAccess; 131 132 using Base::Base; 133 }; 134 135 // For maps. 136 template <typename Policy, typename PolicyTraits, typename Alloc> 137 class node_handle<Policy, PolicyTraits, Alloc, 138 absl::void_t<typename Policy::mapped_type>> 139 : public node_handle_base<PolicyTraits, Alloc> { 140 using Base = node_handle_base<PolicyTraits, Alloc>; 141 142 public: 143 using key_type = typename Policy::key_type; 144 using mapped_type = typename Policy::mapped_type; 145 146 constexpr node_handle() {} 147 148 auto key() const -> decltype(PolicyTraits::key(this->slot())) { 149 return PolicyTraits::key(this->slot()); 150 } 151 152 mapped_type& mapped() const { 153 return PolicyTraits::value(&PolicyTraits::element(this->slot())); 154 } 155 156 private: 157 friend struct CommonAccess; 158 159 using Base::Base; 160 }; 161 162 // Provide access to non-public node-handle functions. 163 struct CommonAccess { 164 template <typename Node> 165 static auto GetSlot(const Node& node) -> decltype(node.slot()) { 166 return node.slot(); 167 } 168 169 template <typename Node> 170 static void Destroy(Node* node) { 171 node->destroy(); 172 } 173 174 template <typename Node> 175 static void Reset(Node* node) { 176 node->reset(); 177 } 178 179 template <typename T, typename... Args> 180 static T Transfer(Args&&... args) { 181 return T(typename T::transfer_tag_t{}, std::forward<Args>(args)...); 182 } 183 184 template <typename T, typename... Args> 185 static T Move(Args&&... args) { 186 return T(typename T::move_tag_t{}, std::forward<Args>(args)...); 187 } 188 }; 189 190 // Implement the insert_return_type<> concept of C++17. 191 template <class Iterator, class NodeType> 192 struct InsertReturnType { 193 Iterator position; 194 bool inserted; 195 NodeType node; 196 }; 197 198 } // namespace container_internal 199 ABSL_NAMESPACE_END 200 } // namespace absl 201 202 #endif // ABSL_CONTAINER_INTERNAL_CONTAINER_H_ 203