94 lines
1.7 KiB
C
94 lines
1.7 KiB
C
#include "class.h"
|
|
|
|
#include "type.h"
|
|
|
|
#include <assert.h>
|
|
#include <fx/class.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
void *fx_class_get(fx_type_id id)
|
|
{
|
|
struct fx_type_info *ty = fx_type_info_get_by_id(id);
|
|
if (!ty) {
|
|
return NULL;
|
|
}
|
|
|
|
return ty->ty_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->ty_name;
|
|
}
|
|
|
|
void *fx_class_get_interface(
|
|
const struct _fx_class *c,
|
|
const union fx_type_id *id)
|
|
{
|
|
if (!c) {
|
|
return NULL;
|
|
}
|
|
|
|
assert(c->c_magic == FX_CLASS_MAGIC);
|
|
|
|
const struct fx_type_info *type_reg = c->c_type;
|
|
struct fx_type_component *comp = fx_type_get_component(
|
|
&type_reg->ty_components,
|
|
id);
|
|
|
|
if (!comp) {
|
|
return NULL;
|
|
}
|
|
|
|
return (char *)c + comp->c_class_data_offset;
|
|
}
|
|
|
|
fx_result fx_class_instantiate(
|
|
struct fx_type_info *type,
|
|
struct _fx_class **out_class)
|
|
{
|
|
struct _fx_class *out = malloc(type->ty_class_size);
|
|
if (!out) {
|
|
return FX_RESULT_ERR(NO_MEMORY);
|
|
}
|
|
|
|
memset(out, 0x0, type->ty_class_size);
|
|
|
|
out->c_magic = FX_CLASS_MAGIC;
|
|
out->c_type = type;
|
|
|
|
struct fx_queue_entry *entry = fx_queue_first(
|
|
&type->ty_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;
|
|
void *class_data = (char *)out + comp->c_class_data_offset;
|
|
struct fx_type_info *main_class_param = NULL;
|
|
if (class_info == type) {
|
|
main_class_param = type;
|
|
}
|
|
|
|
if (class_info->ty_class_init) {
|
|
class_info->ty_class_init(
|
|
out,
|
|
main_class_param,
|
|
class_data);
|
|
}
|
|
|
|
entry = fx_queue_next(entry);
|
|
}
|
|
|
|
*out_class = out;
|
|
return FX_RESULT_SUCCESS;
|
|
}
|