runtime: add assembly system to collect and export type definitions
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
set(namespace_include_paths ${CMAKE_CURRENT_SOURCE_DIR}/include PARENT_SCOPE)
|
||||
|
||||
file(GLOB namespace_sources
|
||||
*.c *.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include/fx/reflection/*.h)
|
||||
set(namespace_sources ${namespace_sources} PARENT_SCOPE)
|
||||
@@ -0,0 +1,264 @@
|
||||
#include <fx/bst.h>
|
||||
#include <fx/hash.h>
|
||||
#include <fx/macros.h>
|
||||
#include <fx/queue.h>
|
||||
#include <fx/reflection/assembly.h>
|
||||
|
||||
enum map_entry_type {
|
||||
MAP_ENTRY_NONE = 0,
|
||||
MAP_ENTRY_ITEM,
|
||||
MAP_ENTRY_BUCKET,
|
||||
};
|
||||
|
||||
struct map_entry {
|
||||
enum map_entry_type e_type;
|
||||
uint64_t e_hash;
|
||||
union {
|
||||
fx_queue_entry e_entry;
|
||||
fx_bst_node e_node;
|
||||
};
|
||||
};
|
||||
|
||||
struct map_bucket {
|
||||
struct map_entry b_entry;
|
||||
fx_queue b_items;
|
||||
};
|
||||
|
||||
struct map_item {
|
||||
struct map_entry i_entry;
|
||||
const char *i_name;
|
||||
};
|
||||
|
||||
struct map {
|
||||
fx_bst m_entries;
|
||||
};
|
||||
|
||||
struct type {
|
||||
struct map_item e_map_item;
|
||||
fx_type e_type;
|
||||
};
|
||||
|
||||
struct fx_assembly_p {
|
||||
const char *a_name;
|
||||
struct map a_types;
|
||||
|
||||
struct {
|
||||
long v_major;
|
||||
long v_minor;
|
||||
long v_build;
|
||||
long v_revision;
|
||||
} a_version;
|
||||
};
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
FX_BST_DEFINE_SIMPLE_GET(
|
||||
struct map_entry,
|
||||
uint64_t,
|
||||
e_node,
|
||||
e_hash,
|
||||
map_get_entry);
|
||||
FX_BST_DEFINE_SIMPLE_INSERT(struct map_entry, e_node, e_hash, map_put_entry);
|
||||
|
||||
static struct map_bucket *map_item_convert_to_bucket(
|
||||
struct map *map,
|
||||
struct map_item *item)
|
||||
{
|
||||
struct map_bucket *bucket = malloc(sizeof *bucket);
|
||||
|
||||
memset(bucket, 0x0, sizeof *bucket);
|
||||
|
||||
bucket->b_entry.e_hash = item->i_entry.e_hash;
|
||||
bucket->b_entry.e_type = MAP_ENTRY_BUCKET;
|
||||
|
||||
fx_bst_delete(&map->m_entries, &item->i_entry.e_node);
|
||||
fx_queue_push_back(&bucket->b_items, &item->i_entry.e_entry);
|
||||
map_put_entry(&map->m_entries, &bucket->b_entry);
|
||||
|
||||
return bucket;
|
||||
}
|
||||
|
||||
static void map_put(struct map *map, const char *name, struct map_item *item)
|
||||
{
|
||||
uint64_t hash = fx_hash_cstr(name);
|
||||
|
||||
item->i_entry.e_type = MAP_ENTRY_ITEM;
|
||||
item->i_entry.e_hash = hash;
|
||||
item->i_name = name;
|
||||
|
||||
struct map_entry *entry = map_get_entry(&map->m_entries, hash);
|
||||
if (!entry) {
|
||||
map_put_entry(&map->m_entries, &item->i_entry);
|
||||
return;
|
||||
}
|
||||
|
||||
struct map_item *existing_item = NULL;
|
||||
struct map_bucket *bucket = NULL;
|
||||
switch (entry->e_type) {
|
||||
case MAP_ENTRY_ITEM:
|
||||
existing_item = (struct map_item *)entry;
|
||||
bucket = map_item_convert_to_bucket(map, existing_item);
|
||||
fx_queue_push_back(&bucket->b_items, &item->i_entry.e_entry);
|
||||
break;
|
||||
case MAP_ENTRY_BUCKET:
|
||||
bucket = (struct map_bucket *)entry;
|
||||
fx_queue_push_back(&bucket->b_items, &item->i_entry.e_entry);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void assembly_set_name(struct fx_assembly_p *asm, const char *name)
|
||||
{
|
||||
asm->a_name = name;
|
||||
}
|
||||
|
||||
static void assembly_set_version(
|
||||
struct fx_assembly_p *asm,
|
||||
long major,
|
||||
long minor,
|
||||
long build,
|
||||
long revision)
|
||||
{
|
||||
asm->a_version.v_major = major;
|
||||
asm->a_version.v_minor = minor;
|
||||
asm->a_version.v_build = build;
|
||||
asm->a_version.v_revision = revision;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
static void assembly_add_type(
|
||||
struct fx_assembly_p *asm,
|
||||
const char *full_name,
|
||||
fx_type type_id)
|
||||
{
|
||||
struct type *type = malloc(sizeof *type);
|
||||
memset(type, 0x0, sizeof *type);
|
||||
|
||||
map_put(&asm->a_types, full_name, &type->e_map_item);
|
||||
}
|
||||
|
||||
static void map_item_dump(struct map_item *item)
|
||||
{
|
||||
struct type *type = (struct type *)item;
|
||||
printf(" Type: %s\n", type->e_map_item.i_name);
|
||||
}
|
||||
|
||||
static void assembly_dump(struct fx_assembly_p *asm)
|
||||
{
|
||||
printf("Loaded assembly:\n");
|
||||
printf(" %s, Version=%ld.%ld.%ld.%ld\n",
|
||||
asm->a_name,
|
||||
asm->a_version.v_major,
|
||||
asm->a_version.v_minor,
|
||||
asm->a_version.v_build,
|
||||
asm->a_version.v_revision);
|
||||
|
||||
fx_bst_node *cur_node = fx_bst_first(&asm->a_types.m_entries);
|
||||
while (cur_node) {
|
||||
struct map_entry *entry
|
||||
= fx_unbox(struct map_entry, cur_node, e_node);
|
||||
switch (entry->e_type) {
|
||||
case MAP_ENTRY_ITEM: {
|
||||
struct map_item *item = (struct map_item *)entry;
|
||||
map_item_dump(item);
|
||||
break;
|
||||
}
|
||||
|
||||
case MAP_ENTRY_BUCKET: {
|
||||
struct map_bucket *bucket = (struct map_bucket *)entry;
|
||||
fx_queue_entry *cur_qentry
|
||||
= fx_queue_first(&bucket->b_items);
|
||||
while (cur_qentry) {
|
||||
struct map_item *item = fx_unbox(
|
||||
struct map_item,
|
||||
cur_qentry,
|
||||
i_entry.e_entry);
|
||||
map_item_dump(item);
|
||||
cur_qentry = fx_queue_next(cur_qentry);
|
||||
}
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
cur_node = fx_bst_next(cur_node);
|
||||
}
|
||||
}
|
||||
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
void fx_assembly_set_name(fx_assembly *asm, const char *name)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_V(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_set_name,
|
||||
asm,
|
||||
name);
|
||||
}
|
||||
|
||||
void fx_assembly_set_version(
|
||||
fx_assembly *asm,
|
||||
long major,
|
||||
long minor,
|
||||
long build,
|
||||
long revision)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_V(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_set_version,
|
||||
asm,
|
||||
major,
|
||||
minor,
|
||||
build,
|
||||
revision);
|
||||
}
|
||||
|
||||
void fx_assembly_add_type(
|
||||
fx_assembly *asm,
|
||||
const char *full_name,
|
||||
fx_type type_id)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_V(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_add_type,
|
||||
asm,
|
||||
full_name,
|
||||
type_id);
|
||||
}
|
||||
|
||||
void fx_assembly_dump(const fx_assembly *asm)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_REFLECTION_TYPE_ASSEMBLY,
|
||||
assembly_dump,
|
||||
asm);
|
||||
}
|
||||
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
static void assembly_init(fx_object *obj, void *priv)
|
||||
{
|
||||
}
|
||||
|
||||
static void assembly_fini(fx_object *obj, void *priv)
|
||||
{
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
// ---- fx_string DEFINITION
|
||||
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_assembly)
|
||||
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = NULL;
|
||||
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
FX_TYPE_CLASS_DEFINITION_END(fx_assembly)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(fx_assembly)
|
||||
FX_TYPE_ID(0xf6690c30, 0x6642, 0x42f0, 0xb79f, 0xe2baf3684b1b);
|
||||
FX_TYPE_CLASS(fx_assembly_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct fx_assembly_p);
|
||||
FX_TYPE_INSTANCE_INIT(assembly_init);
|
||||
FX_TYPE_INSTANCE_FINI(assembly_fini);
|
||||
FX_TYPE_DEFINITION_END(fx_assembly)
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef FX_REFLECTION_ASSEMBLY_H_
|
||||
#define FX_REFLECTION_ASSEMBLY_H_
|
||||
|
||||
#include <fx/macros.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
#define FX_REFLECTION_TYPE_ASSEMBLY (fx_assembly_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(fx_assembly);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_assembly)
|
||||
FX_TYPE_CLASS_DECLARATION_END(fx_assembly)
|
||||
|
||||
FX_API fx_type fx_assembly_get_type();
|
||||
|
||||
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_assembly, FX_REFLECTION_TYPE_ASSEMBLY);
|
||||
|
||||
FX_API void fx_assembly_set_name(fx_assembly *asm, const char *name);
|
||||
FX_API void fx_assembly_set_version(
|
||||
fx_assembly *asm,
|
||||
long major,
|
||||
long minor,
|
||||
long build,
|
||||
long revision);
|
||||
|
||||
FX_API void fx_assembly_add_type(
|
||||
fx_assembly *asm,
|
||||
const char *full_name,
|
||||
fx_type type_id);
|
||||
|
||||
FX_API void fx_assembly_dump(const fx_assembly *asm);
|
||||
|
||||
FX_DECLS_END
|
||||
;
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user