Bifrost SDK
Bifrost SDK documentation
TypeId.h
Go to the documentation of this file.
1//-
2// =============================================================================
3// Copyright 2024 Autodesk, Inc. All rights reserved.
4//
5// Use of this software is subject to the terms of the Autodesk license
6// agreement provided at the time of installation or download, or which
7// otherwise accompanies this software in either electronic or hard copy form.
8// =============================================================================
9//+
10
11#ifndef AMINO_CORE_TYPE_ID_H
12#define AMINO_CORE_TYPE_ID_H
13
16
17#include "internal/TypeInfo.h"
18
19//==============================================================================
20// NAMESPACE Amino
21//==============================================================================
22
23namespace Amino {
24
25//==============================================================================
26// FORWARD DECLARATIONS
27//==============================================================================
28
29class TypeId;
30
32template <typename T>
33TypeId getTypeId() noexcept;
34
35//==============================================================================
36// CLASS TypeId
37//==============================================================================
38
45class TypeId {
46public:
47 /*----- member functions -----*/
48
50 TypeId() = delete;
51
58 size_t computeHash() const { return info().computeHash(); }
59
62 bool operator==(TypeId o) const noexcept { return info() == o.info(); }
63 bool operator!=(TypeId o) const noexcept { return info() != o.info(); }
65
75 // clang-format off
76 bool operator< (TypeId o) const noexcept { return info() < o.info(); }
77 bool operator<=(TypeId o) const noexcept { return info() <= o.info(); }
78 bool operator> (TypeId o) const noexcept { return info() > o.info(); }
79 bool operator>=(TypeId o) const noexcept { return info() >= o.info(); }
80 // clang-format on
82
83private:
85
86 /*----- friend declarations -----*/
87
88 template <typename T>
89 friend TypeId getTypeId() noexcept;
90 friend ::Amino::Internal::TypeInfoDetails;
91
92 /*----- static member functions -----*/
93
95 template <typename T>
96 static TypeId get() noexcept;
97
98 /*----- member functions -----*/
99
103 explicit constexpr TypeId(std::nullptr_t) : m_info(nullptr) {}
104
107 explicit constexpr TypeId(Internal::TypeInfo const* info) : m_info(info) {
108 assert(info);
109 }
110
112 Internal::TypeInfo const& info() const {
113 assert(m_info);
114 return *m_info;
115 }
116
117 /*----- data members -----*/
118
120 Internal::TypeInfo const* m_info = nullptr;
121
123};
124
125//------------------------------------------------------------------------------
126//
127static_assert(
128 sizeof(TypeId) == sizeof(void*), "TypeId must be the size of a pointer.");
129static_assert(
130 alignof(TypeId) == alignof(void*), "TypeId must be aligned like pointer.");
131
132//------------------------------------------------------------------------------
133//
135template <typename T>
136TypeId TypeId::get() noexcept {
137#if AMINO_INTERNAL_OPTION_HAS_RTTI
138 return TypeId(&Internal::TypeInfo::get<T>());
139#else
140 // If no RTTI, fail at compile time, not link time.
141 // It's ok to use Amino::TypeId without RTTI, but not ok to create them.
142 // This isRttiEnabled will always be false if this function is instantiated,
143 // but the dummy expression must depend on the template type T, otherwise
144 // the static_assert will fail even if the function isn't instantiated.
145 constexpr bool isRttiEnabled = (alignof(T) == -42); // always false
146 static_assert(isRttiEnabled, "Can't use getTypeId() without rtti enabled!");
147 AMINO_INTERNAL_UNREACHABLE("Can't be instantiated");
148#endif
149}
155template <>
156inline TypeId TypeId::get<void>() noexcept {
157 return TypeId(&Internal::TypeInfo::get<void>());
158}
160
161//==============================================================================
162// FUNCTION getTypeId<T>()
163//==============================================================================
164
165//------------------------------------------------------------------------------
166//
167template <typename T>
168TypeId getTypeId() noexcept {
169 return TypeId::get<T>();
170}
171
174template <>
175inline constexpr TypeId getTypeId<Internal::NullTypeInfo>() noexcept {
176 return TypeId(nullptr);
177}
179
180} // namespace Amino
181
182#endif
Definition: HostData.h:33
TypeId getTypeId() noexcept
Returns the TypeId for the given Type.
Definition: TypeId.h:168
bool operator<(Ptr< T > const &x, Ptr< U > const &y) noexcept
Compares the pointer values.
Definition: Ptr.h:1485
bool operator>(Ptr< T > const &x, Ptr< U > const &y) noexcept
Compares the pointer values.
Definition: Ptr.h:1518
Definition: Ptr.h:2080
Type identifier for a type.
Definition: TypeId.h:45
bool operator>=(TypeId o) const noexcept
TypeId's ordering comparison operators, to allow storing TypeId in ordered containers (ONLY!...
Definition: TypeId.h:79
bool operator==(TypeId o) const noexcept
TypeId's equality comparison operators.
Definition: TypeId.h:62
bool operator<=(TypeId o) const noexcept
TypeId's ordering comparison operators, to allow storing TypeId in ordered containers (ONLY!...
Definition: TypeId.h:77
bool operator!=(TypeId o) const noexcept
TypeId's equality comparison operators.
Definition: TypeId.h:63
TypeId()=delete
Can't default construct a TypeId.
size_t computeHash() const
Computes a hash for this TypeId.
Definition: TypeId.h:58