Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
TypeID.hpp
1#pragma once
2
3#include <Infinity/api.h>
4#include <Infinity/Types/Util/Hash.hpp>
5
6#include <iostream>
7#include <cstring>
8
9namespace Infinity::Types
10{
19 typedef const void* TypeTag;
20
30 typedef uint64_t TypeHash;
31
70 struct INFINITY_API_PUBLIC TypeID
71 {
74 const char* name;
75
82
92
103 constexpr TypeID(TypeTag tag, TypeHash hash, const char* name)
104 : tag(tag), hash(hash), name(name) { }
105
120 bool canConvert(const TypeID& other) const;
121
130 operator TypeHash() const
131 {
132 return hash;
133 }
134
143 void write(std::ostream& os) const;
144
153 void read(std::istream& is);
154
163 std::string str() const;
164
173 std::string hashStr() const;
174
189 static TypeHash create_stable_hash(const char* name, size_t len);
190
200 constexpr bool operator==(const TypeID& other) const
201 {
202 return tag == other.tag;
203 }
204
211 constexpr bool operator!=(const TypeID& other) const
212 {
213 return !(*this == other);
214 }
215 };
216
238 template<typename T>
240}
241
242namespace std
243{
251 template<>
252 struct hash<Infinity::Types::TypeID>
253 {
260 size_t operator()(const Infinity::Types::TypeID& id) const
261 {
262 return reinterpret_cast<size_t>(id.tag);
263 }
264 };
265}
266
285#define INFINITY_TYPE(...) \
286 template <> INFINITY_API_PUBLIC const ::Infinity::Types::TypeID& ::Infinity::Types::getTypeID<__VA_ARGS__>();
287
313#define INFINITY_TYPE_IMPL(...) \
314 template<> \
315 const ::Infinity::Types::TypeID& ::Infinity::Types::getTypeID<__VA_ARGS__>() { \
316 static const char* name = #__VA_ARGS__; \
317 static ::Infinity::Types::TypeID id((::Infinity::Types::TypeTag)name, \
318 ::Infinity::Types::TypeID::create_stable_hash(name, std::strlen(name)), \
319 name); \
320 return id; \
321 }
322
332#define INFINITY_TYPE_NAME(...)
333
343#define INFINITY_TYPE_CONVERT(...)
Definition Array.hpp:25
const void * TypeTag
Opaque pointer used as a unique compile-time identifier for types.
Definition TypeID.hpp:19
const TypeID & getTypeID()
Gets the TypeID for a given type T.
uint64_t TypeHash
64-bit hash value computed from a type's name.
Definition TypeID.hpp:30
Definition Asset.hpp:11
Definition Span.hpp:599
Runtime type identifier for the Infinity type system.
Definition TypeID.hpp:71
bool canConvert(const TypeID &other) const
Checks if this type can be converted to another type.
constexpr bool operator!=(const TypeID &other) const
Inequality comparison operator.
Definition TypeID.hpp:211
TypeID()
Default constructor.
constexpr TypeID(TypeTag tag, TypeHash hash, const char *name)
Constructs a complete TypeID with all components.
Definition TypeID.hpp:103
TypeID(TypeHash hash)
Constructs a TypeID from a hash value.
TypeHash hash
Stable hash of the type name for serialization.
Definition TypeID.hpp:73
constexpr bool operator==(const TypeID &other) const
Equality comparison operator.
Definition TypeID.hpp:200
std::string hashStr() const
Gets the hash value as a hexadecimal string.
void read(std::istream &is)
Deserializes a TypeID from an input stream.
const char * name
Human-readable type name string.
Definition TypeID.hpp:74
void write(std::ostream &os) const
Serializes the TypeID to an output stream.
static TypeHash create_stable_hash(const char *name, size_t len)
Computes a stable hash from a type name string.
TypeTag tag
Unique compile-time identifier.
Definition TypeID.hpp:72
std::string str() const
Gets the human-readable type name string.
size_t operator()(const Infinity::Types::TypeID &id) const
Computes hash value for a TypeID.
Definition TypeID.hpp:260