Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
Infinity::Types::TypeID Struct Reference

Runtime type identifier for the Infinity type system. More...

#include <TypeID.hpp>

Public Member Functions

 TypeID ()
 Default constructor.
 
 TypeID (TypeHash hash)
 Constructs a TypeID from a hash value.
 
constexpr TypeID (TypeTag tag, TypeHash hash, const char *name)
 Constructs a complete TypeID with all components.
 
bool canConvert (const TypeID &other) const
 Checks if this type can be converted to another type.
 
 operator TypeHash () const
 Implicit conversion to TypeHash.
 
void write (std::ostream &os) const
 Serializes the TypeID to an output stream.
 
void read (std::istream &is)
 Deserializes a TypeID from an input stream.
 
std::string str () const
 Gets the human-readable type name string.
 
std::string hashStr () const
 Gets the hash value as a hexadecimal string.
 
constexpr bool operator== (const TypeID &other) const
 Equality comparison operator.
 
constexpr bool operator!= (const TypeID &other) const
 Inequality comparison operator.
 

Static Public Member Functions

static TypeHash create_stable_hash (const char *name, size_t len)
 Computes a stable hash from a type name string.
 

Public Attributes

TypeTag tag
 Unique compile-time identifier.
 
TypeHash hash
 Stable hash of the type name for serialization.
 
const char * name
 Human-readable type name string.
 

Detailed Description

Runtime type identifier for the Infinity type system.

TypeID provides a lightweight, efficient runtime type identification system that combines compile-time uniqueness (via TypeTag) with runtime stability (via TypeHash). It enables type-safe polymorphism, serialization, and type conversion checking throughout the Infinity Engine.

Each type in the system has a unique TypeID that includes:

  • A compile-time unique tag (memory address of static data)
  • A stable hash computed from the type name (consistent across runs)
  • A human-readable type name string

TypeIDs are typically obtained using the getTypeID<T>() template function and should be registered using the INFINITY_TYPE_IMPL macro.

Example usage:

// Get TypeID for a type
const TypeID& meshType = getTypeID<Mesh>();
const TypeID& textureType = getTypeID<Texture>();
// Compare types
if (meshType == textureType) {
// Types are the same
}
// Check type conversion compatibility
if (sourceType.canConvert(targetType)) {
// Conversion is supported
}
// Use as hash key
TypeHash hash = meshType; // Implicit conversion
std::cout << "Type: " << meshType.str() << std::endl;
uint64_t TypeHash
64-bit hash value computed from a type's name.
Definition TypeID.hpp:30
Runtime type identifier for the Infinity type system.
Definition TypeID.hpp:71
TypeHash hash
Stable hash of the type name for serialization.
Definition TypeID.hpp:73
std::string str() const
Gets the human-readable type name string.

Constructor & Destructor Documentation

◆ TypeID() [1/3]

Infinity::Types::TypeID::TypeID ( )

Default constructor.

Creates an invalid/empty TypeID with null tag and zero hash.

◆ TypeID() [2/3]

Infinity::Types::TypeID::TypeID ( TypeHash  hash)

Constructs a TypeID from a hash value.

Creates a TypeID with only the hash component set. Used primarily for deserialization or lookup operations where only the hash is known.

Parameters
hashThe 64-bit hash value identifying the type.

◆ TypeID() [3/3]

constexpr Infinity::Types::TypeID::TypeID ( TypeTag  tag,
TypeHash  hash,
const char *  name 
)
inlineconstexpr

Constructs a complete TypeID with all components.

This is the primary constructor used by INFINITY_TYPE_IMPL macro to create fully-initialized TypeIDs at compile time.

Parameters
tagUnique compile-time identifier (typically address of static name).
hashStable hash computed from the type name.
nameNull-terminated string containing the type name.

Member Function Documentation

◆ canConvert()

bool Infinity::Types::TypeID::canConvert ( const TypeID other) const

Checks if this type can be converted to another type.

Determines whether a value of this type can be safely converted to the target type. This enables runtime type checking for polymorphic operations and type-safe casting.

Parameters
otherThe target TypeID to check conversion compatibility against.
Returns
true if conversion is possible, false otherwise.
Note
Conversion rules are defined by the type system and may include inheritance relationships, registered conversions, and built-in type promotion rules.

◆ create_stable_hash()

static TypeHash Infinity::Types::TypeID::create_stable_hash ( const char *  name,
size_t  len 
)
static

Computes a stable hash from a type name string.

Creates a 64-bit hash that is consistent across compilations and program executions. This hash is used for serialization and for identifying types in persistent storage or network protocols.

Parameters
nameNull-terminated string containing the type name.
lenLength of the name string (excluding null terminator).
Returns
64-bit hash value uniquely identifying the type name.
Note
The hash algorithm is designed to minimize collisions while maintaining stability across different platforms and compilers.

◆ hashStr()

std::string Infinity::Types::TypeID::hashStr ( ) const

Gets the hash value as a hexadecimal string.

Returns the TypeHash formatted as a hexadecimal string, useful for debugging hash collisions or logging type identifiers.

Returns
Hexadecimal string representation of the hash.

◆ operator TypeHash()

Infinity::Types::TypeID::operator TypeHash ( ) const
inline

Implicit conversion to TypeHash.

Allows TypeID to be used directly as a hash value in contexts requiring numeric type identifiers.

Returns
The 64-bit hash value of this type.

◆ operator!=()

constexpr bool Infinity::Types::TypeID::operator!= ( const TypeID other) const
inlineconstexpr

Inequality comparison operator.

Parameters
otherThe TypeID to compare against.
Returns
true if the types are different, false otherwise.

◆ operator==()

constexpr bool Infinity::Types::TypeID::operator== ( const TypeID other) const
inlineconstexpr

Equality comparison operator.

Compares two TypeIDs for equality based on their compile-time tags. This is a fast pointer comparison operation.

Parameters
otherThe TypeID to compare against.
Returns
true if the types are identical, false otherwise.

◆ read()

void Infinity::Types::TypeID::read ( std::istream &  is)

Deserializes a TypeID from an input stream.

Reads a previously serialized TypeID from a binary stream. Reconstructs the TypeID from its hash representation.

Parameters
isInput stream to read from.

◆ str()

std::string Infinity::Types::TypeID::str ( ) const

Gets the human-readable type name string.

Returns the full qualified name of the type as a string, useful for debugging and logging.

Returns
String representation of the type name.

◆ write()

void Infinity::Types::TypeID::write ( std::ostream &  os) const

Serializes the TypeID to an output stream.

Writes the TypeID in a format suitable for binary serialization. Typically writes the hash value for compact representation.

Parameters
osOutput stream to write to.

Member Data Documentation

◆ hash

TypeHash Infinity::Types::TypeID::hash

Stable hash of the type name for serialization.

◆ name

const char* Infinity::Types::TypeID::name

Human-readable type name string.

◆ tag

TypeTag Infinity::Types::TypeID::tag

Unique compile-time identifier.