mirror of
https://github.com/abseil/abseil-cpp.git
synced 2026-06-04 12:07:05 +08:00
Convert FastTypeIdType from an alias to a struct.
PiperOrigin-RevId: 853275176 Change-Id: I03979c49739d1664ede2fa9be1034ddeb379447d
This commit is contained in:
committed by
Copybara-Service
parent
6d8e1a5cf0
commit
237c42c78f
@@ -16,6 +16,8 @@
|
||||
#ifndef ABSL_BASE_FAST_TYPE_ID_H_
|
||||
#define ABSL_BASE_FAST_TYPE_ID_H_
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "absl/base/config.h"
|
||||
|
||||
namespace absl {
|
||||
@@ -29,14 +31,40 @@ struct FastTypeTag {
|
||||
} // namespace base_internal
|
||||
|
||||
// The type returned by `absl::FastTypeId<T>()`.
|
||||
using FastTypeIdType = const void*;
|
||||
class FastTypeIdType {
|
||||
public:
|
||||
// Creates a value that does not correspond to any type. This value is
|
||||
// distinct from any value returned by `FastTypeId<T>()`.
|
||||
constexpr FastTypeIdType() = default;
|
||||
|
||||
template <typename H>
|
||||
friend H AbslHashValue(H h, FastTypeIdType x) {
|
||||
return H::combine(std::move(h), x.ptr_);
|
||||
}
|
||||
|
||||
friend constexpr bool operator==(FastTypeIdType a, FastTypeIdType b) {
|
||||
return a.ptr_ == b.ptr_;
|
||||
}
|
||||
friend constexpr bool operator!=(FastTypeIdType a, FastTypeIdType b) {
|
||||
return a.ptr_ != b.ptr_;
|
||||
}
|
||||
|
||||
private:
|
||||
// `FastTypeId<T>()` is the generator method for FastTypeIdType values.
|
||||
template <typename T>
|
||||
friend constexpr FastTypeIdType FastTypeId();
|
||||
|
||||
explicit constexpr FastTypeIdType(const void* ptr) : ptr_(ptr) {}
|
||||
|
||||
const void* ptr_ = nullptr;
|
||||
};
|
||||
|
||||
// `absl::FastTypeId<Type>()` evaluates at compile-time to a unique id for the
|
||||
// passed-in type. These are meant to be good match for keys into maps or
|
||||
// straight up comparisons.
|
||||
template <typename Type>
|
||||
constexpr FastTypeIdType FastTypeId() {
|
||||
return &base_internal::FastTypeTag<Type>::kDummyVar;
|
||||
return FastTypeIdType(&base_internal::FastTypeTag<Type>::kDummyVar);
|
||||
}
|
||||
|
||||
ABSL_NAMESPACE_END
|
||||
|
||||
@@ -24,6 +24,9 @@
|
||||
|
||||
namespace {
|
||||
|
||||
// Ensure that absl::bit_cast works between FastTypeIdType and void*.
|
||||
static_assert(sizeof(absl::FastTypeIdType) == sizeof(void*));
|
||||
|
||||
// NOLINTBEGIN(runtime/int)
|
||||
#define PRIM_TYPES(A) \
|
||||
A(bool) \
|
||||
|
||||
@@ -135,7 +135,7 @@ inline size_t Sizeof(FlagOpFn op) {
|
||||
}
|
||||
// Returns fast type id corresponding to the value type.
|
||||
inline FlagFastTypeId FastTypeId(FlagOpFn op) {
|
||||
return reinterpret_cast<FlagFastTypeId>(
|
||||
return absl::bit_cast<FlagFastTypeId>(
|
||||
op(FlagOp::kFastTypeId, nullptr, nullptr, nullptr));
|
||||
}
|
||||
// Returns fast type id corresponding to the value type.
|
||||
@@ -168,7 +168,7 @@ inline const std::type_info* GenRuntimeTypeId() {
|
||||
// Flag help auxiliary structs.
|
||||
|
||||
// This is help argument for absl::Flag encapsulating the string literal pointer
|
||||
// or pointer to function generating it as well as enum descriminating two
|
||||
// or pointer to function generating it as well as enum discriminating two
|
||||
// cases.
|
||||
using HelpGenFunc = std::string (*)();
|
||||
|
||||
@@ -901,7 +901,7 @@ void* FlagOps(FlagOp op, const void* v1, void* v2, void* v3) {
|
||||
case FlagOp::kSizeof:
|
||||
return reinterpret_cast<void*>(static_cast<uintptr_t>(sizeof(T)));
|
||||
case FlagOp::kFastTypeId:
|
||||
return const_cast<void*>(absl::FastTypeId<T>());
|
||||
return absl::bit_cast<void*>(absl::FastTypeId<T>());
|
||||
case FlagOp::kRuntimeTypeId:
|
||||
return const_cast<std::type_info*>(GenRuntimeTypeId<T>());
|
||||
case FlagOp::kParse: {
|
||||
|
||||
Reference in New Issue
Block a user