Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:27:12

0001 // Copyright 2018 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 // Adapts a policy for nodes.
0016 //
0017 // The node policy should model:
0018 //
0019 // struct Policy {
0020 //   // Returns a new node allocated and constructed using the allocator, using
0021 //   // the specified arguments.
0022 //   template <class Alloc, class... Args>
0023 //   value_type* new_element(Alloc* alloc, Args&&... args) const;
0024 //
0025 //   // Destroys and deallocates node using the allocator.
0026 //   template <class Alloc>
0027 //   void delete_element(Alloc* alloc, value_type* node) const;
0028 // };
0029 //
0030 // It may also optionally define `value()` and `apply()`. For documentation on
0031 // these, see hash_policy_traits.h.
0032 
0033 #ifndef ABSL_CONTAINER_INTERNAL_NODE_SLOT_POLICY_H_
0034 #define ABSL_CONTAINER_INTERNAL_NODE_SLOT_POLICY_H_
0035 
0036 #include <cassert>
0037 #include <cstddef>
0038 #include <memory>
0039 #include <type_traits>
0040 #include <utility>
0041 
0042 #include "absl/base/config.h"
0043 
0044 namespace absl {
0045 ABSL_NAMESPACE_BEGIN
0046 namespace container_internal {
0047 
0048 template <class Reference, class Policy>
0049 struct node_slot_policy {
0050   static_assert(std::is_lvalue_reference<Reference>::value, "");
0051 
0052   using slot_type = typename std::remove_cv<
0053       typename std::remove_reference<Reference>::type>::type*;
0054 
0055   template <class Alloc, class... Args>
0056   static void construct(Alloc* alloc, slot_type* slot, Args&&... args) {
0057     *slot = Policy::new_element(alloc, std::forward<Args>(args)...);
0058   }
0059 
0060   template <class Alloc>
0061   static void destroy(Alloc* alloc, slot_type* slot) {
0062     Policy::delete_element(alloc, *slot);
0063   }
0064 
0065   // Returns true_type to indicate that transfer can use memcpy.
0066   template <class Alloc>
0067   static std::true_type transfer(Alloc*, slot_type* new_slot,
0068                                  slot_type* old_slot) {
0069     *new_slot = *old_slot;
0070     return {};
0071   }
0072 
0073   static size_t space_used(const slot_type* slot) {
0074     if (slot == nullptr) return Policy::element_space_used(nullptr);
0075     return Policy::element_space_used(*slot);
0076   }
0077 
0078   static Reference element(slot_type* slot) { return **slot; }
0079 
0080   template <class T, class P = Policy>
0081   static auto value(T* elem) -> decltype(P::value(elem)) {
0082     return P::value(elem);
0083   }
0084 
0085   template <class... Ts, class P = Policy>
0086   static auto apply(Ts&&... ts) -> decltype(P::apply(std::forward<Ts>(ts)...)) {
0087     return P::apply(std::forward<Ts>(ts)...);
0088   }
0089 };
0090 
0091 }  // namespace container_internal
0092 ABSL_NAMESPACE_END
0093 }  // namespace absl
0094 
0095 #endif  // ABSL_CONTAINER_INTERNAL_NODE_SLOT_POLICY_H_