feat: added macros for casting typeclass exporting mirror
This commit is contained in:
parent
986ab06478
commit
9597d612af
|
@ -43,6 +43,22 @@ extern const void* mirror_get_typeclass(void* data, IMirror const* tc, const cha
|
||||||
//
|
//
|
||||||
extern void* mirror_get_function(void* data, IMirror const* tc, const char* typeclass_name);
|
extern void* mirror_get_function(void* data, IMirror const* tc, const char* typeclass_name);
|
||||||
|
|
||||||
|
// macro reexport of mirror_get_function which will cast the function so it can be called immediately
|
||||||
|
// example:
|
||||||
|
// MIRROR_GET_WRAP_FUNC(physics_entity.data, physics_entity.mirror, BehaviourEntity)(physics_entity.data)
|
||||||
|
#define MIRROR_GET_WRAP_FUNC(Data_, MirrorTc_, Typeclass_)\
|
||||||
|
((Typeclass_(*)(void*))mirror_get_function(Data_, MirrorTc_, #Typeclass_))
|
||||||
|
|
||||||
|
// casting only works if the typeclass in question exposes IMirror as .mirror
|
||||||
|
// will segfault if the Mirror does not expose To_
|
||||||
|
// example:
|
||||||
|
// TC_CAST(physics_entity, BehaviourEntity)
|
||||||
|
#define TC_CAST(From_, To_)\
|
||||||
|
MIRROR_GET_WRAP_FUNC(From_.data, From_.mirror, To_)(From_.data)
|
||||||
|
|
||||||
|
#define TC_MIRRORS(From_, To_)\
|
||||||
|
MIRROR_GET_WRAP_FUNC(From_.data, From_.mirror, To_) != NULL
|
||||||
|
|
||||||
#define MIRROR_TRY_WRAP(Into_, Mirror_, Typeclass_){\
|
#define MIRROR_TRY_WRAP(Into_, Mirror_, Typeclass_){\
|
||||||
MirroredTypeclassWrapFunc fn_ = mirror_get_typeclass(Mirror_, #Typeclass_);\
|
MirroredTypeclassWrapFunc fn_ = mirror_get_typeclass(Mirror_, #Typeclass_);\
|
||||||
if(fn_ != NULL) {\
|
if(fn_ != NULL) {\
|
||||||
|
@ -89,7 +105,8 @@ List* T##_get_typeclasses(T* self) {\
|
||||||
if(!init_flag) {\
|
if(!init_flag) {\
|
||||||
init_flag = 1;\
|
init_flag = 1;\
|
||||||
typeclasses = list_init(sizeof(MirroredTypeclass));\
|
typeclasses = list_init(sizeof(MirroredTypeclass));\
|
||||||
MirroredTypeclass tc;
|
MirroredTypeclass tc;\
|
||||||
|
REFLECT_TYPECLASS(T, Mirror)
|
||||||
|
|
||||||
#define REFLECT_TYPECLASS(T, TypeClass_)\
|
#define REFLECT_TYPECLASS(T, TypeClass_)\
|
||||||
tc = (MirroredTypeclass){\
|
tc = (MirroredTypeclass){\
|
||||||
|
|
Loading…
Reference in a new issue