Infinity Engine v0.6.20
C++ API Documentation
Loading...
Searching...
No Matches
Converters.hpp
1#pragma once
2
3#include <Infinity/api.h>
4#include <Infinity/Types/TypeID.hpp>
5#include <Infinity/Types/Core/Base.hpp>
6#include <Infinity/Util/unordered_dense.h>
7
8namespace Infinity::Types
9{
28 template<typename F, typename T>
29 using Converter = T(const F&);
30
41 using ConverterFn = std::function<Core::Base*(const Core::Base*)>;
42
94 class INFINITY_API_PUBLIC Converters
95 {
96 public:
104
123 bool canConvert(const TypeID& fromType, const TypeID& toType)
124 {
125 return _converters.find(hashIDs(fromType, toType)) != _converters.end();
126 }
127
153 Core::Base* convert(const TypeID& fromType, const TypeID& toType, const Core::Base* from);
154
172 ConverterFn converter(const TypeID& fromType, const TypeID& toType);
173
197 template<typename F, typename T>
198 T* convert(F* from)
199 {
200 const TypeID& fid = getTypeID<F>();
201 const TypeID& tid = getTypeID<T>();
202 return static_cast<T*>(convert(fid, tid, from));
203 }
204
221 template<typename F, typename T>
223 {
224 const TypeID& fid = getTypeID<F>();
225 const TypeID& tid = getTypeID<T>();
226 return canConvert(fid, tid);
227 }
228
257 template<typename F, typename T>
259 {
260 const TypeID& fid = getTypeID<F>();
261 const TypeID& tid = getTypeID<T>();
262
263 _converters[hashIDs(fid, tid)] = fn;
264 }
265
285
286 private:
293 Converters();
294
306 TypeHash hashIDs(const TypeID& id1, const TypeID& id2);
307
314 ankerl::unordered_dense::map<TypeHash, ConverterFn> _converters;
315 };
316
317}
Global registry for type conversion functions.
Definition Converters.hpp:95
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
T * convert(F *from)
Performs type conversion with compile-time type safety.
Definition Converters.hpp:198
void registerConversion(ConverterFn fn)
Registers a conversion function between two types.
Definition Converters.hpp:258
ConverterFn converter(const TypeID &fromType, const TypeID &toType)
Retrieves the converter function for a type pair.
Abstract base class for all Infinity Engine data types.
Definition Base.hpp:43
Definition Array.hpp:25
std::function< Core::Base *(const Core::Base *)> ConverterFn
Type-erased function wrapper for runtime type conversion.
Definition Converters.hpp:41
T(const F &) Converter
Function signature for type conversion between two specific types.
Definition Converters.hpp:29
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