Files
fx/core/class.c

80 lines
1.6 KiB
C
Raw Permalink Normal View History

2025-08-16 16:03:55 +01:00
#include "class.h"
#include "type.h"
#include <assert.h>
2026-03-16 10:35:43 +00:00
#include <fx/core/class.h>
2025-08-16 16:03:55 +01:00
#include <stdlib.h>
#include <string.h>
2026-03-16 10:35:43 +00:00
void *fx_class_get(fx_type id)
{
2026-03-16 10:35:43 +00:00
struct fx_type_registration *r = fx_type_get_registration(id);
if (!r) {
return NULL;
}
return r->r_class;
}
2026-03-16 10:35:43 +00:00
const char *fx_class_get_name(const struct _fx_class *c)
{
if (!c) {
return NULL;
}
2026-03-16 10:35:43 +00:00
assert(c->c_magic == FX_CLASS_MAGIC);
return c->c_type->r_info->t_name;
}
2026-03-16 10:35:43 +00:00
void *fx_class_get_interface(const struct _fx_class *c, const union fx_type *id)
2025-08-16 16:03:55 +01:00
{
if (!c) {
return NULL;
}
2026-03-16 10:35:43 +00:00
assert(c->c_magic == FX_CLASS_MAGIC);
2026-03-16 10:35:43 +00:00
const struct fx_type_registration *type_reg = c->c_type;
struct fx_type_component *comp
= fx_type_get_component(&type_reg->r_components, id);
2025-08-16 16:03:55 +01:00
if (!comp) {
return NULL;
}
return (char *)c + comp->c_class_data_offset;
}
2026-03-16 10:35:43 +00:00
fx_result fx_class_instantiate(
struct fx_type_registration *type, struct _fx_class **out_class)
2025-08-16 16:03:55 +01:00
{
2026-03-16 10:35:43 +00:00
struct _fx_class *out = malloc(type->r_class_size);
2025-08-16 16:03:55 +01:00
if (!out) {
2026-03-16 10:35:43 +00:00
return FX_RESULT_ERR(NO_MEMORY);
2025-08-16 16:03:55 +01:00
}
memset(out, 0x0, type->r_class_size);
2026-03-16 10:35:43 +00:00
out->c_magic = FX_CLASS_MAGIC;
2025-08-16 16:03:55 +01:00
out->c_type = type;
2026-03-16 10:35:43 +00:00
struct fx_queue_entry *entry = fx_queue_first(&type->r_class_hierarchy);
while (entry) {
2026-03-16 10:35:43 +00:00
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;
2025-08-16 16:03:55 +01:00
void *class_data = (char *)out + comp->c_class_data_offset;
if (class_info->t_class_init) {
class_info->t_class_init(out, class_data);
}
2026-03-16 10:35:43 +00:00
entry = fx_queue_next(entry);
2025-08-16 16:03:55 +01:00
}
*out_class = out;
2026-03-16 10:35:43 +00:00
return FX_RESULT_SUCCESS;
2025-08-16 16:03:55 +01:00
}