node_hash_policy.h
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 //
00015 // Adapts a policy for nodes.
00016 //
00017 // The node policy should model:
00018 //
00019 // struct Policy {
00020 //   // Returns a new node allocated and constructed using the allocator, using
00021 //   // the specified arguments.
00022 //   template <class Alloc, class... Args>
00023 //   value_type* new_element(Alloc* alloc, Args&&... args) const;
00024 //
00025 //   // Destroys and deallocates node using the allocator.
00026 //   template <class Alloc>
00027 //   void delete_element(Alloc* alloc, value_type* node) const;
00028 // };
00029 //
00030 // It may also optionally define `value()` and `apply()`. For documentation on
00031 // these, see hash_policy_traits.h.
00032 
00033 #ifndef ABSL_CONTAINER_INTERNAL_NODE_HASH_POLICY_H_
00034 #define ABSL_CONTAINER_INTERNAL_NODE_HASH_POLICY_H_
00035 
00036 #include <cassert>
00037 #include <cstddef>
00038 #include <memory>
00039 #include <type_traits>
00040 #include <utility>
00041 
00042 namespace absl {
00043 namespace container_internal {
00044 
00045 template <class Reference, class Policy>
00046 struct node_hash_policy {
00047   static_assert(std::is_lvalue_reference<Reference>::value, "");
00048 
00049   using slot_type = typename std::remove_cv<
00050       typename std::remove_reference<Reference>::type>::type*;
00051 
00052   template <class Alloc, class... Args>
00053   static void construct(Alloc* alloc, slot_type* slot, Args&&... args) {
00054     *slot = Policy::new_element(alloc, std::forward<Args>(args)...);
00055   }
00056 
00057   template <class Alloc>
00058   static void destroy(Alloc* alloc, slot_type* slot) {
00059     Policy::delete_element(alloc, *slot);
00060   }
00061 
00062   template <class Alloc>
00063   static void transfer(Alloc*, slot_type* new_slot, slot_type* old_slot) {
00064     *new_slot = *old_slot;
00065   }
00066 
00067   static size_t space_used(const slot_type* slot) {
00068     if (slot == nullptr) return Policy::element_space_used(nullptr);
00069     return Policy::element_space_used(*slot);
00070   }
00071 
00072   static Reference element(slot_type* slot) { return **slot; }
00073 
00074   template <class T, class P = Policy>
00075   static auto value(T* elem) -> decltype(P::value(elem)) {
00076     return P::value(elem);
00077   }
00078 
00079   template <class... Ts, class P = Policy>
00080   static auto apply(Ts&&... ts) -> decltype(P::apply(std::forward<Ts>(ts)...)) {
00081     return P::apply(std::forward<Ts>(ts)...);
00082   }
00083 };
00084 
00085 }  // namespace container_internal
00086 }  // namespace absl
00087 
00088 #endif  // ABSL_CONTAINER_INTERNAL_NODE_HASH_POLICY_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15