80 lines
1.6 KiB
C
80 lines
1.6 KiB
C
#include "class.h"
|
|
|
|
#include "type.h"
|
|
|
|
#include <assert.h>
|
|
#include <fx/core/class.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
void *fx_class_get(fx_type id)
|
|
{
|
|
struct fx_type_registration *r = fx_type_get_registration(id);
|
|
if (!r) {
|
|
return NULL;
|
|
}
|
|
|
|
return r->r_class;
|
|
}
|
|
|
|
const char *fx_class_get_name(const struct _fx_class *c)
|
|
{
|
|
if (!c) {
|
|
return NULL;
|
|
}
|
|
|
|
assert(c->c_magic == FX_CLASS_MAGIC);
|
|
|
|
return c->c_type->r_info->t_name;
|
|
}
|
|
|
|
void *fx_class_get_interface(const struct _fx_class *c, const union fx_type *id)
|
|
{
|
|
if (!c) {
|
|
return NULL;
|
|
}
|
|
|
|
assert(c->c_magic == FX_CLASS_MAGIC);
|
|
|
|
const struct fx_type_registration *type_reg = c->c_type;
|
|
struct fx_type_component *comp
|
|
= fx_type_get_component(&type_reg->r_components, id);
|
|
|
|
if (!comp) {
|
|
return NULL;
|
|
}
|
|
|
|
return (char *)c + comp->c_class_data_offset;
|
|
}
|
|
|
|
fx_result fx_class_instantiate(
|
|
struct fx_type_registration *type, struct _fx_class **out_class)
|
|
{
|
|
struct _fx_class *out = malloc(type->r_class_size);
|
|
if (!out) {
|
|
return FX_RESULT_ERR(NO_MEMORY);
|
|
}
|
|
|
|
memset(out, 0x0, type->r_class_size);
|
|
|
|
out->c_magic = FX_CLASS_MAGIC;
|
|
out->c_type = type;
|
|
|
|
struct fx_queue_entry *entry = fx_queue_first(&type->r_class_hierarchy);
|
|
while (entry) {
|
|
struct fx_type_component *comp
|
|
= fx_unbox(struct fx_type_component, entry, c_entry);
|
|
const struct fx_type_info *class_info = comp->c_type->r_info;
|
|
void *class_data = (char *)out + comp->c_class_data_offset;
|
|
|
|
if (class_info->t_class_init) {
|
|
class_info->t_class_init(out, class_data);
|
|
}
|
|
|
|
entry = fx_queue_next(entry);
|
|
}
|
|
|
|
*out_class = out;
|
|
return FX_RESULT_SUCCESS;
|
|
}
|