Files
bluelib/object/object.c

66 lines
1.1 KiB
C
Raw Normal View History

2024-10-24 19:24:54 +01:00
#include "object.h"
#include <blue/object/object.h>
#include <blue/object/type.h>
#include <blue/object/string.h>
#include <blue/core/stringstream.h>
#include <stdlib.h>
#include <stdio.h>
void b_object_init(struct b_object *obj, struct b_object_type *type)
{
obj->ob_ref = 1;
obj->ob_type = type;
}
struct b_object *b_make_rvalue(struct b_object *obj)
{
obj->ob_ref = 0;
return obj;
}
struct b_object *b_retain(struct b_object *obj)
{
obj->ob_ref++;
return obj;
}
void b_release(struct b_object *obj)
{
if (obj->ob_ref > 1) {
obj->ob_ref--;
return;
}
obj->ob_ref = 0;
if (obj->ob_type && obj->ob_type->t_release) {
obj->ob_type->t_release(obj);
}
free(obj);
}
void b_to_string(struct b_object *obj, struct b_stringstream *out)
{
if (obj->ob_type->t_to_string) {
obj->ob_type->t_to_string(obj, out);
return;
}
const char *name = "corelib::object";
if (obj->ob_type) {
name = obj->ob_type->t_name;
}
b_stringstream_addf(out, "<%s@%p>", name, obj);
}
b_object_type_id b_typeid(const struct b_object *obj)
{
if (obj->ob_type->t_flags & B_OBJECT_FUNDAMENTAL) {
return obj->ob_type->t_id;
}
return (b_object_type_id)obj->ob_type;
}