#include "object.h" #include #include #include #include #include #include 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(const struct b_object *obj, struct b_stream *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_stream_write_fmt(out, NULL, "<%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; }