diff --git a/core/src/mirror.h b/core/src/mirror.h index 8f4a2af..b9fce0d 100644 --- a/core/src/mirror.h +++ b/core/src/mirror.h @@ -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); +// 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_){\ MirroredTypeclassWrapFunc fn_ = mirror_get_typeclass(Mirror_, #Typeclass_);\ if(fn_ != NULL) {\ @@ -89,7 +105,8 @@ List* T##_get_typeclasses(T* self) {\ if(!init_flag) {\ init_flag = 1;\ typeclasses = list_init(sizeof(MirroredTypeclass));\ - MirroredTypeclass tc; + MirroredTypeclass tc;\ + REFLECT_TYPECLASS(T, Mirror) #define REFLECT_TYPECLASS(T, TypeClass_)\ tc = (MirroredTypeclass){\