Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame^] | 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 | // Adapts a policy for nodes. |
| 16 | // |
| 17 | // The node policy should model: |
| 18 | // |
| 19 | // struct Policy { |
| 20 | // // Returns a new node allocated and constructed using the allocator, using |
| 21 | // // the specified arguments. |
| 22 | // template <class Alloc, class... Args> |
| 23 | // value_type* new_element(Alloc* alloc, Args&&... args) const; |
| 24 | // |
| 25 | // // Destroys and deallocates node using the allocator. |
| 26 | // template <class Alloc> |
| 27 | // void delete_element(Alloc* alloc, value_type* node) const; |
| 28 | // }; |
| 29 | // |
| 30 | // It may also optionally define `value()` and `apply()`. For documentation on |
| 31 | // these, see hash_policy_traits.h. |
| 32 | |
| 33 | #ifndef ABSL_CONTAINER_INTERNAL_NODE_HASH_POLICY_H_ |
| 34 | #define ABSL_CONTAINER_INTERNAL_NODE_HASH_POLICY_H_ |
| 35 | |
| 36 | #include <cassert> |
| 37 | #include <cstddef> |
| 38 | #include <memory> |
| 39 | #include <type_traits> |
| 40 | #include <utility> |
| 41 | |
| 42 | namespace absl { |
| 43 | namespace container_internal { |
| 44 | |
| 45 | template <class Reference, class Policy> |
| 46 | struct node_hash_policy { |
| 47 | static_assert(std::is_lvalue_reference<Reference>::value, ""); |
| 48 | |
| 49 | using slot_type = typename std::remove_cv< |
| 50 | typename std::remove_reference<Reference>::type>::type*; |
| 51 | |
| 52 | template <class Alloc, class... Args> |
| 53 | static void construct(Alloc* alloc, slot_type* slot, Args&&... args) { |
| 54 | *slot = Policy::new_element(alloc, std::forward<Args>(args)...); |
| 55 | } |
| 56 | |
| 57 | template <class Alloc> |
| 58 | static void destroy(Alloc* alloc, slot_type* slot) { |
| 59 | Policy::delete_element(alloc, *slot); |
| 60 | } |
| 61 | |
| 62 | template <class Alloc> |
| 63 | static void transfer(Alloc*, slot_type* new_slot, slot_type* old_slot) { |
| 64 | *new_slot = *old_slot; |
| 65 | } |
| 66 | |
| 67 | static size_t space_used(const slot_type* slot) { |
| 68 | if (slot == nullptr) return Policy::element_space_used(nullptr); |
| 69 | return Policy::element_space_used(*slot); |
| 70 | } |
| 71 | |
| 72 | static Reference element(slot_type* slot) { return **slot; } |
| 73 | |
| 74 | template <class T, class P = Policy> |
| 75 | static auto value(T* elem) -> decltype(P::value(elem)) { |
| 76 | return P::value(elem); |
| 77 | } |
| 78 | |
| 79 | template <class... Ts, class P = Policy> |
| 80 | static auto apply(Ts&&... ts) -> decltype(P::apply(std::forward<Ts>(ts)...)) { |
| 81 | return P::apply(std::forward<Ts>(ts)...); |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | } // namespace container_internal |
| 86 | } // namespace absl |
| 87 | |
| 88 | #endif // ABSL_CONTAINER_INTERNAL_NODE_HASH_POLICY_H_ |