Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // Copyright 2023 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 // Common code shared between absl/hash/hash_test.cc and
0016 // absl/hash/hash_instantiated_test.cc.
0017 
0018 #ifndef ABSL_HASH_INTERNAL_HASH_TEST_H_
0019 #define ABSL_HASH_INTERNAL_HASH_TEST_H_
0020 
0021 #include <type_traits>
0022 #include <utility>
0023 
0024 #include "absl/base/config.h"
0025 #include "absl/hash/hash.h"
0026 
0027 namespace absl {
0028 ABSL_NAMESPACE_BEGIN
0029 namespace hash_test_internal {
0030 
0031 // Utility wrapper of T for the purposes of testing the `AbslHash` type erasure
0032 // mechanism.  `TypeErasedValue<T>` can be constructed with a `T`, and can
0033 // be compared and hashed.  However, all hashing goes through the hashing
0034 // type-erasure framework.
0035 template <typename T>
0036 class TypeErasedValue {
0037  public:
0038   TypeErasedValue() = default;
0039   TypeErasedValue(const TypeErasedValue&) = default;
0040   TypeErasedValue(TypeErasedValue&&) = default;
0041   explicit TypeErasedValue(const T& n) : n_(n) {}
0042 
0043   template <typename H>
0044   friend H AbslHashValue(H hash_state, const TypeErasedValue& v) {
0045     v.HashValue(absl::HashState::Create(&hash_state));
0046     return hash_state;
0047   }
0048 
0049   void HashValue(absl::HashState state) const {
0050     absl::HashState::combine(std::move(state), n_);
0051   }
0052 
0053   bool operator==(const TypeErasedValue& rhs) const { return n_ == rhs.n_; }
0054   bool operator!=(const TypeErasedValue& rhs) const { return !(*this == rhs); }
0055 
0056  private:
0057   T n_;
0058 };
0059 
0060 // A TypeErasedValue refinement, for containers.  It exposes the wrapped
0061 // `value_type` and is constructible from an initializer list.
0062 template <typename T>
0063 class TypeErasedContainer : public TypeErasedValue<T> {
0064  public:
0065   using value_type = typename T::value_type;
0066   TypeErasedContainer() = default;
0067   TypeErasedContainer(const TypeErasedContainer&) = default;
0068   TypeErasedContainer(TypeErasedContainer&&) = default;
0069   explicit TypeErasedContainer(const T& n) : TypeErasedValue<T>(n) {}
0070   TypeErasedContainer(std::initializer_list<value_type> init_list)
0071       : TypeErasedContainer(T(init_list.begin(), init_list.end())) {}
0072   // one-argument constructor of value type T, to appease older toolchains that
0073   // get confused by one-element initializer lists in some contexts
0074   explicit TypeErasedContainer(const value_type& v)
0075       : TypeErasedContainer(T(&v, &v + 1)) {}
0076 };
0077 
0078 // Helper trait to verify if T is hashable. We use absl::Hash's poison status to
0079 // detect it.
0080 template <typename T>
0081 using is_hashable = std::is_default_constructible<absl::Hash<T>>;
0082 
0083 }  // namespace hash_test_internal
0084 ABSL_NAMESPACE_END
0085 }  // namespace absl
0086 
0087 #endif  // ABSL_HASH_INTERNAL_HASH_TEST_H_