core: move new object system to core module
this will allow a wider range of data structures (e.g. b_error, b_stream, b_stringstream) to make use of the new object system, and other modules and library users can use the object system without depending on the blue-object or blue-ds modules. blue-ds will become a simple library of data structures (string, hashmap, etc), built on top of the core object system.
This commit is contained in:
49
core/class.c
Normal file
49
core/class.c
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "class.h"
|
||||
|
||||
#include "type.h"
|
||||
|
||||
#include <blue/core/class.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void *z__b_class_get_interface(struct _b_class *c, const union b_type *id)
|
||||
{
|
||||
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;
|
||||
|
||||
b_queue_iterator q_it;
|
||||
b_queue_foreach (&q_it, &type->r_class_hierarchy) {
|
||||
struct b_type_component *comp
|
||||
= b_unbox(struct b_type_component, q_it.entry, c_entry);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
*out_class = out;
|
||||
return B_RESULT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user