feat: mirror now uses dictionary instead of list
This commit is contained in:
parent
05451c6ca3
commit
23741714f2
|
@ -1,12 +1,7 @@
|
||||||
#include "mirror.h"
|
#include "mirror.h"
|
||||||
|
|
||||||
MirroredTypeclass* internal_mirror_get_typeclass(void* self, IMirror const* tc, const char* typeclass) {
|
MirroredTypeclass* internal_mirror_get_typeclass(void* self, IMirror const* tc, const char* typeclass) {
|
||||||
uintptr_t target_hash = strhash(typeclass);
|
return dictionary_get_raw(tc->get_typeclasses(self), 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const void* mirror_get_typeclass(void* data, IMirror const* tc, const char* typeclass) {
|
const void* mirror_get_typeclass(void* data, IMirror const* tc, const char* typeclass) {
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include "stdint.h"
|
#include "stdint.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
#include "dictionary.h"
|
||||||
#include "strutil.h" // included because the impl macros require strhash
|
#include "strutil.h" // included because the impl macros require strhash
|
||||||
|
|
||||||
typedef uintptr_t typeid;
|
typedef uintptr_t typeid;
|
||||||
|
@ -12,7 +13,7 @@ typedef uintptr_t typeid;
|
||||||
typedef struct {
|
typedef struct {
|
||||||
const char* (*const get_typestring)(void* self);
|
const char* (*const get_typestring)(void* self);
|
||||||
typeid (*const get_typeid)(void* self);
|
typeid (*const get_typeid)(void* self);
|
||||||
List* (*const get_typeclasses)(void* self);
|
Dictionary* (*const get_typeclasses)(void* self);
|
||||||
} IMirror;
|
} IMirror;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -25,8 +26,6 @@ typedef struct {
|
||||||
} Mirror;
|
} Mirror;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
const char* typeclass_name;
|
|
||||||
uintptr_t name_hash;
|
|
||||||
const void* typeclass;
|
const void* typeclass;
|
||||||
void* function;
|
void* function;
|
||||||
} MirroredTypeclass;
|
} MirroredTypeclass;
|
||||||
|
@ -74,11 +73,11 @@ MIRROR_GET_WRAP_FUNC(From_.data, From_.mirror, To_) != NULL
|
||||||
Mirror T##_as_Mirror(T* x) {\
|
Mirror T##_as_Mirror(T* x) {\
|
||||||
TC_FN_TYPECHECK(const char*, get_typestring_f, T*);\
|
TC_FN_TYPECHECK(const char*, get_typestring_f, T*);\
|
||||||
TC_FN_TYPECHECK(typeid, get_typeid_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 = {\
|
static IMirror const tc = {\
|
||||||
.get_typestring = (const char*(*const)(void*)) get_typestring_f,\
|
.get_typestring = (const char*(*const)(void*)) get_typestring_f,\
|
||||||
.get_typeid = (typeid (*const)(void*)) get_typeid_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};\
|
return (Mirror){.tc = &tc, .data = x};\
|
||||||
}
|
}
|
||||||
|
@ -86,7 +85,7 @@ Mirror T##_as_Mirror(T* x) {\
|
||||||
#define DECL_REFLECT(T)\
|
#define DECL_REFLECT(T)\
|
||||||
extern const char* T##_get_typestring(T* self);\
|
extern const char* T##_get_typestring(T* self);\
|
||||||
extern typeid T##_get_typeid(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)
|
decl_typeclass_impl(Mirror, T)
|
||||||
|
|
||||||
#define START_REFLECT(T)\
|
#define START_REFLECT(T)\
|
||||||
|
@ -103,23 +102,21 @@ typeid T##_get_typeid(T* self) {\
|
||||||
}\
|
}\
|
||||||
return id;\
|
return id;\
|
||||||
}\
|
}\
|
||||||
List* T##_get_typeclasses(T* self) {\
|
Dictionary* T##_get_typeclasses(T* self) {\
|
||||||
static char init_flag = 0;\
|
static char init_flag = 0;\
|
||||||
static List typeclasses;\
|
static Dictionary typeclasses;\
|
||||||
if(!init_flag) {\
|
if(!init_flag) {\
|
||||||
init_flag = 1;\
|
init_flag = 1;\
|
||||||
typeclasses = list_init(sizeof(MirroredTypeclass));\
|
typeclasses = dictionary_new(sizeof(MirroredTypeclass));\
|
||||||
MirroredTypeclass tc;\
|
MirroredTypeclass tc;\
|
||||||
REFLECT_TYPECLASS(T, Mirror)
|
REFLECT_TYPECLASS(T, Mirror)
|
||||||
|
|
||||||
#define REFLECT_TYPECLASS(T, TypeClass_)\
|
#define REFLECT_TYPECLASS(T, TypeClass_)\
|
||||||
tc = (MirroredTypeclass){\
|
tc = (MirroredTypeclass){\
|
||||||
.name_hash = strhash(#TypeClass_),\
|
|
||||||
.typeclass_name = #TypeClass_,\
|
|
||||||
.typeclass = (void*)T##_as_##TypeClass_(NULL).tc,\
|
.typeclass = (void*)T##_as_##TypeClass_(NULL).tc,\
|
||||||
.function = (void*)T##_as_##TypeClass_\
|
.function = (void*)T##_as_##TypeClass_\
|
||||||
};\
|
};\
|
||||||
list_add(&typeclasses, &tc);
|
dictionary_set_raw(&typeclasses, #TypeClass_, &tc);
|
||||||
|
|
||||||
#define END_REFLECT(T)\
|
#define END_REFLECT(T)\
|
||||||
}\
|
}\
|
||||||
|
|
Loading…
Reference in a new issue