1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_BASE_INTERACTION_TYPED_IDENTIFIER_H_
#define UI_BASE_INTERACTION_TYPED_IDENTIFIER_H_
#include "ui/base/interaction/element_identifier.h"
namespace ui {
// Identifier that also carries type information.
//
// Use the DECLARE/DEFINE macros below to create unique identifiers, similarly
// to how ElementIdentifier, etc. work.
template <typename Type>
class TypedIdentifier final {
public:
constexpr TypedIdentifier() = default;
explicit constexpr TypedIdentifier(ElementIdentifier identifier)
: identifier_(identifier) {}
constexpr ElementIdentifier identifier() const { return identifier_; }
constexpr explicit operator bool() const {
return static_cast<bool>(identifier_);
}
constexpr bool operator!() const { return !identifier_; }
friend constexpr bool operator==(const TypedIdentifier<Type>&,
const TypedIdentifier<Type>&) = default;
friend constexpr auto operator<=>(const TypedIdentifier<Type>&,
const TypedIdentifier<Type>&) = default;
private:
ElementIdentifier identifier_;
};
template <typename T>
extern void PrintTo(TypedIdentifier<T> identifier, std::ostream* os) {
*os << "TypedIdentifier " << identifier.identifier().GetRawValue() << " ["
<< identifier.identifier().GetName() << "]";
}
template <typename T>
extern std::ostream& operator<<(std::ostream& os,
TypedIdentifier<T> identifier) {
PrintTo(identifier, os);
return os;
}
} // namespace ui
// The following macros create a typed identifier value, and mimic the similar
// macros for ElementIdentifier, except that they also include a type.
#define DECLARE_TYPED_IDENTIFIER_VALUE(Type, Name) \
DECLARE_ELEMENT_IDENTIFIER_VALUE(Name##Impl); \
extern const ui::TypedIdentifier<Type> Name
#define DEFINE_TYPED_IDENTIFIER_VALUE(Type, Name) \
DEFINE_ELEMENT_IDENTIFIER_VALUE(Name##Impl); \
constexpr ui::TypedIdentifier<Type> Name(Name##Impl)
#define DECLARE_CLASS_TYPED_IDENTIFIER_VALUE(Type, Name) \
DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(Name##Impl); \
static constexpr ui::TypedIdentifier<Type> Name { \
Name##Impl \
}
#define DEFINE_CLASS_TYPED_IDENTIFIER_VALUE(Class, Type, Name) \
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(Class, Name##Impl); \
constexpr ui::TypedIdentifier<Type> Class::Name
#define DEFINE_LOCAL_TYPED_IDENTIFIER_VALUE(Type, Name) \
DEFINE_MACRO_ELEMENT_IDENTIFIER_VALUE(__FILE__, __LINE__, Name##Impl); \
constexpr ui::TypedIdentifier<Type> Name(Name##Impl)
#define DEFINE_MACRO_TYPED_IDENTIFIER_VALUE(File, Line, Type, Name) \
DEFINE_MACRO_ELEMENT_IDENTIFIER_VALUE(File, Line, Name##Impl); \
constexpr ui::TypedIdentifier<Type> Name(Name##Impl)
#endif // UI_BASE_INTERACTION_TYPED_IDENTIFIER_H_
|