feat: mirror now uses dictionary instead of list

This commit is contained in:
Sara 2024-01-25 00:14:13 +01:00
parent 05451c6ca3
commit 23741714f2
2 changed files with 10 additions and 18 deletions

View file

@ -1,12 +1,7 @@
#include "mirror.h"
MirroredTypeclass* internal_mirror_get_typeclass(void* self, IMirror const* tc, const char* typeclass) {
uintptr_t target_hash = strhash(typeclass);
list_foreach(MirroredTypeclass*, class, tc->get_typeclasses(self)) {
if(target_hash == class->name_hash && strcmp(typeclass, class->typeclass_name) == 0)
return class;
}
return NULL;
return dictionary_get_raw(tc->get_typeclasses(self), typeclass);
}
const void* mirror_get_typeclass(void* data, IMirror const* tc, const char* typeclass) {

View file

@ -5,6 +5,7 @@
#include "stdint.h"
#include "string.h"
#include "list.h"
#include "dictionary.h"
#include "strutil.h" // included because the impl macros require strhash
typedef uintptr_t typeid;
@ -12,7 +13,7 @@ typedef uintptr_t typeid;
typedef struct {
const char* (*const get_typestring)(void* self);
typeid (*const get_typeid)(void* self);
List* (*const get_typeclasses)(void* self);
Dictionary* (*const get_typeclasses)(void* self);
} IMirror;
typedef struct {
@ -25,8 +26,6 @@ typedef struct {
} Mirror;
typedef struct {
const char* typeclass_name;
uintptr_t name_hash;
const void* typeclass;
void* function;
} MirroredTypeclass;
@ -74,11 +73,11 @@ MIRROR_GET_WRAP_FUNC(From_.data, From_.mirror, To_) != NULL
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(List*, get_typeclasses_f, T*);\
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_typeclasses = (List* (*const)(void*)) get_typeclasses_f,\
.get_typeclasses = (Dictionary* (*const)(void*)) get_typeclasses_f,\
};\
return (Mirror){.tc = &tc, .data = x};\
}
@ -86,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 List* T##_get_typeclasses(T* self);\
extern Dictionary* T##_get_typeclasses(T* self);\
decl_typeclass_impl(Mirror, T)
#define START_REFLECT(T)\
@ -103,23 +102,21 @@ typeid T##_get_typeid(T* self) {\
}\
return id;\
}\
List* T##_get_typeclasses(T* self) {\
Dictionary* T##_get_typeclasses(T* self) {\
static char init_flag = 0;\
static List typeclasses;\
static Dictionary typeclasses;\
if(!init_flag) {\
init_flag = 1;\
typeclasses = list_init(sizeof(MirroredTypeclass));\
typeclasses = dictionary_new(sizeof(MirroredTypeclass));\
MirroredTypeclass tc;\
REFLECT_TYPECLASS(T, Mirror)
#define REFLECT_TYPECLASS(T, TypeClass_)\
tc = (MirroredTypeclass){\
.name_hash = strhash(#TypeClass_),\
.typeclass_name = #TypeClass_,\
.typeclass = (void*)T##_as_##TypeClass_(NULL).tc,\
.function = (void*)T##_as_##TypeClass_\
};\
list_add(&typeclasses, &tc);
dictionary_set_raw(&typeclasses, #TypeClass_, &tc);
#define END_REFLECT(T)\
}\