2025-08-16 16:03:55 +01:00
|
|
|
#include "class.h"
|
|
|
|
|
|
|
|
|
|
#include "type.h"
|
|
|
|
|
|
2025-11-01 09:56:52 +00:00
|
|
|
#include <assert.h>
|
2025-10-15 10:38:18 +01:00
|
|
|
#include <blue/core/class.h>
|
2025-08-16 16:03:55 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2025-10-28 15:11:54 +00:00
|
|
|
void *b_class_get(b_type id)
|
|
|
|
|
{
|
|
|
|
|
struct b_type_registration *r = b_type_get_registration(id);
|
|
|
|
|
if (!r) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r->r_class;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *b_class_get_name(const struct _b_class *c)
|
|
|
|
|
{
|
2025-11-01 09:56:52 +00:00
|
|
|
if (!c) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(c->c_magic == B_CLASS_MAGIC);
|
|
|
|
|
|
2025-10-28 15:11:54 +00:00
|
|
|
return c->c_type->r_info->t_name;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-19 10:21:43 +01:00
|
|
|
void *b_class_get_interface(const struct _b_class *c, const union b_type *id)
|
2025-08-16 16:03:55 +01:00
|
|
|
{
|
2025-11-01 09:56:52 +00:00
|
|
|
if (!c) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(c->c_magic == B_CLASS_MAGIC);
|
|
|
|
|
|
2025-08-16 16:03:55 +01:00
|
|
|
const struct b_type_registration *type_reg = c->c_type;
|
|
|
|
|
struct b_type_component *comp
|
|
|
|
|
= b_type_get_component(&type_reg->r_components, id);
|
|
|
|
|
|
|
|
|
|
if (!comp) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (char *)c + comp->c_class_data_offset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b_result b_class_instantiate(
|
|
|
|
|
struct b_type_registration *type, struct _b_class **out_class)
|
|
|
|
|
{
|
|
|
|
|
struct _b_class *out = malloc(type->r_class_size);
|
|
|
|
|
if (!out) {
|
|
|
|
|
return B_RESULT_ERR(NO_MEMORY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(out, 0x0, type->r_class_size);
|
|
|
|
|
|
|
|
|
|
out->c_magic = B_CLASS_MAGIC;
|
|
|
|
|
out->c_type = type;
|
|
|
|
|
|
2025-10-29 14:31:09 +00:00
|
|
|
struct b_queue_entry *entry = b_queue_first(&type->r_class_hierarchy);
|
|
|
|
|
while (entry) {
|
2025-08-16 16:03:55 +01:00
|
|
|
struct b_type_component *comp
|
2025-10-29 14:31:09 +00:00
|
|
|
= b_unbox(struct b_type_component, entry, c_entry);
|
2025-08-16 16:03:55 +01:00
|
|
|
const struct b_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);
|
|
|
|
|
}
|
2025-10-29 14:31:09 +00:00
|
|
|
|
|
|
|
|
entry = b_queue_next(entry);
|
2025-08-16 16:03:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*out_class = out;
|
|
|
|
|
return B_RESULT_SUCCESS;
|
|
|
|
|
}
|