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

Global registry for runtime type information and factory functions. More...

#include <TypeRegistry.hpp>

Static Public Member Functions

static Core::Basecreate (const TypeID &type)
 Creates a default-constructed instance of the specified type.
 
static const TypeIDfindID (TypeHash hash)
 Finds a TypeID by its hash value.
 
static const TypeIDfindID (std::string_view name)
 Finds a TypeID by its string name.
 
static const TypeIDemptyID ()
 Gets the sentinel TypeID representing an empty/invalid type.
 

Detailed Description

Global registry for runtime type information and factory functions.

TypeRegistry provides centralized access to type metadata and default construction capabilities for all registered types in the Infinity type system. It maintains a mapping between TypeIDs, type hashes, and type names, enabling runtime type lookup and instantiation.

The registry supports:

  • Creating default instances of registered types by TypeID
  • Looking up TypeIDs from hash values (for deserialization)
  • Looking up TypeIDs from string names (for dynamic type resolution)
  • Providing a sentinel "empty" TypeID for null/invalid type references

All methods are static as the registry is a singleton that is initialized automatically by the type system. Types are registered when their INFINITY_TYPE_IMPL macro is first invoked.

Example usage:

// Create a new instance of a type by ID
const TypeID& meshType = getTypeID<Mesh>();
Core::Base* instance = TypeRegistry::create(meshType);
Mesh* mesh = dynamic_cast<Mesh*>(instance);
// Look up a type by hash (e.g., from deserialized data)
TypeHash hash = 0x123456789ABCDEF0;
const TypeID& type = TypeRegistry::findID(hash);
std::cout << "Found type: " << type.str() << std::endl;
// Look up a type by name
const TypeID& textureType = TypeRegistry::findID("Infinity::Types::Texture");
// Check for empty/invalid type
if (someType == TypeRegistry::emptyID()) {
std::cout << "Type is empty/invalid" << std::endl;
}
Abstract base class for all Infinity Engine data types.
Definition Base.hpp:43
static const TypeID & findID(TypeHash hash)
Finds a TypeID by its hash value.
static const TypeID & emptyID()
Gets the sentinel TypeID representing an empty/invalid type.
static Core::Base * create(const TypeID &type)
Creates a default-constructed instance of the specified type.
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
std::string str() const
Gets the human-readable type name string.
Note
This is a static-only class and cannot be instantiated.
Thread-safe for lookup operations; registration occurs during static initialization.

Member Function Documentation

◆ create()

static Core::Base * Infinity::Types::TypeRegistry::create ( const TypeID type)
static

Creates a default-constructed instance of the specified type.

Instantiates a new object of the type identified by the given TypeID using the type's registered factory function. The returned pointer is owned by the caller and must be properly managed (typically via smart pointers).

Parameters
typeThe TypeID of the type to instantiate.
Returns
Pointer to a newly-created instance of the type, or nullptr if the type is not registered or does not support default construction.
Note
The returned pointer must be deleted by the caller.
The actual type can be recovered using dynamic_cast.
Core::Base* base = TypeRegistry::create(getTypeID<Mesh>());
if (base) {
Mesh* mesh = dynamic_cast<Mesh*>(base);
// Use mesh...
delete base; // Or use smart pointer
}

◆ emptyID()

static const TypeID & Infinity::Types::TypeRegistry::emptyID ( )
static

Gets the sentinel TypeID representing an empty/invalid type.

Returns a special TypeID that represents the absence of a valid type. This is useful as a null value for optional type references or to indicate uninitialized type fields.

Returns
Const reference to the empty TypeID singleton.
Note
The empty TypeID compares equal only to itself.
Attempting to create an instance of the empty type will return nullptr.
TypeID currentType = TypeRegistry::emptyID();
void setType(const TypeID& type = TypeRegistry::emptyID()) {
if (type != TypeRegistry::emptyID()) {
// Valid type provided
}
}

◆ findID() [1/2]

static const TypeID & Infinity::Types::TypeRegistry::findID ( std::string_view  name)
static

Finds a TypeID by its string name.

Performs a lookup to find the TypeID associated with a given type name. This enables dynamic type resolution from string identifiers, useful for scripting interfaces, configuration files, or user input.

Parameters
nameThe fully-qualified type name to search for (e.g., "Infinity::Types::Mesh").
Returns
Const reference to the TypeID with the matching name. The empty type if no match is found.
Note
The name must exactly match the type name used in INFINITY_TYPE_IMPL, including namespace qualifiers.
// Dynamic type lookup from config
std::string typeName = "Infinity::Types::Mesh";
const TypeID& type = TypeRegistry::findID(typeName);

◆ findID() [2/2]

static const TypeID & Infinity::Types::TypeRegistry::findID ( TypeHash  hash)
static

Finds a TypeID by its hash value.

Performs a reverse lookup to find the complete TypeID associated with a given hash value. This is primarily used during deserialization when only the hash is available (e.g., from binary data or network protocols).

Parameters
hashThe 64-bit hash value to search for.
Returns
Const reference to the TypeID with the matching hash. The empty type if no match is found.
Note
If multiple types somehow have the same hash (collision), the behavior is undefined. The hash algorithm is designed to minimize this possibility.
// Read hash from serialized data
TypeHash hash = 0x123456789ABCDEF0;
const TypeID& type = TypeRegistry::findID(hash);