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

Global registry for type conversion functions. More...

#include <Converters.hpp>

Public Member Functions

 ~Converters ()
 Destructor.
 
bool canConvert (const TypeID &fromType, const TypeID &toType)
 Checks if conversion between two types is possible (runtime version).
 
Core::Baseconvert (const TypeID &fromType, const TypeID &toType, const Core::Base *from)
 Performs type conversion using registered converter (runtime version).
 
ConverterFn converter (const TypeID &fromType, const TypeID &toType)
 Retrieves the converter function for a type pair.
 
template<typename F , typename T >
T * convert (F *from)
 Performs type conversion with compile-time type safety.
 
template<typename F , typename T >
bool canConvert ()
 Checks if conversion is possible (compile-time version).
 
template<typename F , typename T >
void registerConversion (ConverterFn fn)
 Registers a conversion function between two types.
 

Static Public Member Functions

static Convertersinstance ()
 Gets the global Converters singleton instance.
 

Detailed Description

Global registry for type conversion functions.

Converters maintains a centralized registry of conversion functions between different types in the Infinity type system. It enables both compile-time (template-based) and runtime (TypeID-based) type conversions, supporting flexible data transformation pipelines in procedural generation workflows.

The registry allows:

  • Registering custom conversion functions between any two types
  • Checking whether conversion between two types is possible
  • Performing type-safe conversions at compile time or runtime
  • Retrieving converter functions for deferred execution

Converters is implemented as a singleton that persists for the application lifetime. Conversions are typically registered during initialization and used throughout the program execution.

Example usage:

// Register a converter function
[](const Core::Base* from) -> Core::Base* {
const PointCloud* pc = static_cast<const PointCloud*>(from);
Mesh* mesh = new Mesh();
// Perform conversion...
return mesh;
}
);
// Check if conversion is possible
if (Converters::instance().canConvert<PointCloud, Mesh>()) {
PointCloud pc = generatePointCloud();
Mesh* mesh = Converters::instance().convert<PointCloud, Mesh>(&pc);
// Use mesh...
delete mesh;
}
// Runtime type-based conversion
const TypeID& fromType = getTypeID<PointCloud>();
const TypeID& toType = getTypeID<Mesh>();
if (Converters::instance().canConvert(fromType, toType)) {
Core::Base* result = Converters::instance().convert(fromType, toType, pcBase);
}
static Converters & instance()
Gets the global Converters singleton instance.
bool canConvert()
Checks if conversion is possible (compile-time version).
Definition Converters.hpp:222
Core::Base * convert(const TypeID &fromType, const TypeID &toType, const Core::Base *from)
Performs type conversion using registered converter (runtime version).
bool canConvert(const TypeID &fromType, const TypeID &toType)
Checks if conversion between two types is possible (runtime version).
Definition Converters.hpp:123
void registerConversion(ConverterFn fn)
Registers a conversion function between two types.
Definition Converters.hpp:258
Abstract base class for all Infinity Engine data types.
Definition Base.hpp:43
Warning
Not thread-safe. Conversions should be registered during initialization before concurrent access. Lookup operations are safe once registration is complete.

Constructor & Destructor Documentation

◆ ~Converters()

Infinity::Types::Converters::~Converters ( )

Destructor.

Cleans up the converter registry and releases all registered conversion functions.

Member Function Documentation

◆ canConvert() [1/2]

template<typename F , typename T >
bool Infinity::Types::Converters::canConvert ( )
inline

Checks if conversion is possible (compile-time version).

Template version of canConvert() that uses compile-time type information. More convenient than the runtime version when types are known at compile time.

Template Parameters
FThe source type.
TThe target type.
Returns
true if a converter is registered for this type pair, false otherwise.
if (Converters::instance().canConvert<PointCloud, Mesh>()) {
// Safe to perform conversion
}

◆ canConvert() [2/2]

bool Infinity::Types::Converters::canConvert ( const TypeID fromType,
const TypeID toType 
)
inline

Checks if conversion between two types is possible (runtime version).

Queries the registry to determine whether a conversion function has been registered for the specified type pair.

Parameters
fromTypeThe source TypeID to convert from.
toTypeThe target TypeID to convert to.
Returns
true if a converter is registered for this type pair, false otherwise.
const TypeID& source = getTypeID<PointCloud>();
const TypeID& target = getTypeID<Mesh>();
if (Converters::instance().canConvert(source, target)) {
// Conversion is supported
}
Runtime type identifier for the Infinity type system.
Definition TypeID.hpp:71

◆ convert() [1/2]

Core::Base * Infinity::Types::Converters::convert ( const TypeID fromType,
const TypeID toType,
const Core::Base from 
)

Performs type conversion using registered converter (runtime version).

Executes the registered conversion function for the specified type pair, transforming the source object into a new target object. The returned object is heap-allocated and must be deleted by the caller.

Parameters
fromTypeThe source TypeID.
toTypeThe target TypeID.
fromPointer to the source object to convert.
Returns
Pointer to newly-created converted object, or nullptr if conversion fails.
Exceptions
std::runtime_errorif no converter is registered for this type pair.
Note
The caller is responsible for deleting the returned object.
The original object is not modified or deleted.
Core::Base* source = loadAsset();
getTypeID<PointCloud>(),
getTypeID<Mesh>(),
source
);

◆ convert() [2/2]

template<typename F , typename T >
T * Infinity::Types::Converters::convert ( F *  from)
inline

Performs type conversion with compile-time type safety.

Template version of convert() that provides compile-time type checking and automatic casting. More convenient and safer than the runtime version when types are known at compile time.

Template Parameters
FThe source type.
TThe target type.
Parameters
fromPointer to the source object.
Returns
Pointer to newly-created converted object of type T, or nullptr if conversion fails.
Note
The caller is responsible for deleting the returned object.
PointCloud* pc = generatePointCloud();
Mesh* mesh = Converters::instance().convert<PointCloud, Mesh>(pc);
if (mesh) {
// Use mesh...
delete mesh;
}

◆ converter()

ConverterFn Infinity::Types::Converters::converter ( const TypeID fromType,
const TypeID toType 
)

Retrieves the converter function for a type pair.

Returns the registered converter function without executing it, allowing for deferred conversion or custom conversion workflows.

Parameters
fromTypeThe source TypeID.
toTypeThe target TypeID.
Returns
The converter function for this type pair.
Exceptions
std::runtime_errorif no converter is registered for this type pair.
ConverterFn fn = Converters::instance().converter(fromType, toType);
// Execute later or conditionally
Core::Base* result = fn(source);
ConverterFn converter(const TypeID &fromType, const TypeID &toType)
Retrieves the converter function for a type pair.
std::function< Core::Base *(const Core::Base *)> ConverterFn
Type-erased function wrapper for runtime type conversion.
Definition Converters.hpp:41

◆ instance()

static Converters & Infinity::Types::Converters::instance ( )
static

Gets the global Converters singleton instance.

Returns a reference to the application-wide converter registry. The instance is created on first access and persists for the lifetime of the program.

Returns
Reference to the singleton Converters instance.
Warning
Not thread-safe for registration operations. Ensure all converters are registered before concurrent access begins.
// Access the singleton
registry.registerConversion<TypeA, TypeB>(converterFn);
Global registry for type conversion functions.
Definition Converters.hpp:95

◆ registerConversion()

template<typename F , typename T >
void Infinity::Types::Converters::registerConversion ( ConverterFn  fn)
inline

Registers a conversion function between two types.

Adds a new converter to the registry, enabling conversion from type F to type T. The converter function should create a new object of type T from the provided object of type F.

Template Parameters
FThe source type.
TThe target type.
Parameters
fnThe converter function to register. Must accept const Core::Base* and return Core::Base*. The function should perform appropriate casting and create a new heap-allocated object.
Note
Registering a converter for an already-registered type pair will overwrite the previous converter.
Warning
Not thread-safe. Should only be called during initialization.
[](const Core::Base* from) -> Core::Base* {
const PointCloud* pc = static_cast<const PointCloud*>(from);
Mesh* mesh = new Mesh();
// Convert point cloud to mesh...
return mesh;
}
);