Compare commits

...

2 Commits

2 changed files with 11 additions and 9 deletions

View File

@ -11,7 +11,7 @@ typedef uintptr_t typeid;
typedef struct {
const char* (*const get_typestring)(void* self);
typeid (*const get_typeid)(void* self);
typeid (*const get_typeid)();
Dictionary* (*const get_typeclasses)(void* self);
} IMirror;
@ -30,13 +30,13 @@ typedef struct {
} MirroredTypeclass;
static inline int mirror_is_typeid(const Mirror* mirror, typeid id) {
return mirror->tc->get_typeid(mirror->data) == id;
return mirror->tc->get_typeid() == id;
}
static inline int mirror_is_typestring(const Mirror* mirror, const char* id) {
return strcmp(id, mirror->tc->get_typestring(mirror->data)) == 0;
}
static inline int mirror_eq(const Mirror* lhs, const Mirror* rhs) {
return lhs->tc->get_typeid(lhs->data) == rhs->tc->get_typeid(rhs->data);
return lhs->tc == rhs->tc;
}
extern const void* mirror_get_typeclass(void* data, IMirror const* tc, const char* typeclass);
@ -45,6 +45,8 @@ extern const void* mirror_get_typeclass(void* data, IMirror const* tc, const cha
// mirror_get_function(physics_entity.data, physics_entity.mirror, "BehaviourEntity")
extern void* mirror_get_function(void* data, IMirror const* tc, const char* typeclass_name);
#define GET_TYPEID(Type__) (Type__##_as_Mirror(NULL).tc->get_typeid())
// 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)
@ -71,11 +73,11 @@ extern void* mirror_get_function(void* data, IMirror const* tc, const char* type
#define impl_Mirror_for(T, get_typestring_f, get_typeid_f, get_typeclasses_f)\
Mirror T##_as_Mirror(T* x) {\
TC_FN_TYPECHECK(const char*, get_typestring_f, T*);\
TC_FN_TYPECHECK(typeid, get_typeid_f, T*);\
TC_FN_TYPECHECK(typeid, get_typeid_f);\
TC_FN_TYPECHECK(Dictionary*, get_typeclasses_f, T*);\
static IMirror const tc = {\
.get_typestring = (const char*(*const)(void*)) get_typestring_f,\
.get_typeid = (typeid (*const)(void*)) get_typeid_f,\
.get_typeid = (typeid (*const)()) get_typeid_f,\
.get_typeclasses = (Dictionary* (*const)(void*)) get_typeclasses_f,\
};\
return (Mirror){.tc = &tc, .data = x};\
@ -83,7 +85,7 @@ Mirror T##_as_Mirror(T* x) {\
#define DECL_REFLECT(T)\
extern const char* T##_get_typestring(T* self);\
extern typeid T##_get_typeid(T* self);\
extern typeid T##_get_typeid();\
extern Dictionary* T##_get_typeclasses(T* self);\
decl_typeclass_impl(Mirror, T)
@ -92,7 +94,7 @@ const char* T##_get_typestring(T* self) {\
static const char* const typestring = #T;\
return typestring;\
}\
typeid T##_get_typeid(T* self) {\
typeid T##_get_typeid() {\
static char init_flag = 0;\
static typeid id = 0;\
if(!init_flag) {\

View File

@ -2,10 +2,10 @@
#include "string.h"
uintptr_t strnhash(const char* s, size_t n) {
static const size_t shift = sizeof(uintptr_t) * 8 - 4;
uintptr_t hash = 0;
static size_t const a = 33;
for(size_t i = 0; i < n; ++i) {
hash = ((hash << 4) | s[i]) ^ (((uintptr_t)0xF << shift) & hash);
hash = ((hash*a) + s[i]);
}
return hash;
}