terom@174: #include "object.h" terom@174: terom@174: #include terom@174: #include terom@174: terom@174: void object_init (struct object *obj, const struct object_type *type) terom@174: { terom@174: // trip on bugs terom@174: assert(!obj->type); terom@174: terom@174: // set type terom@174: obj->type = type; terom@174: } terom@174: terom@174: bool object_type_check (const struct object_type *obj_type, const struct object_type *type) terom@174: { terom@174: const struct object_type *t; terom@174: terom@174: // sanity-check terom@174: assert(obj_type && type); terom@174: terom@174: // look for a matching type in the type's inheritance tree terom@174: for (t = obj_type; t; t = t->parent) terom@174: if (t == type) terom@174: break; terom@174: terom@174: // t will be (parent == NULL) if we didn't find any matching type terom@174: return (t != NULL); terom@174: } terom@174: terom@174: bool object_check (struct object *obj, const struct object_type *type) terom@174: { terom@174: // sanity check terom@174: assert(obj && type); terom@174: terom@174: return object_type_check(obj->type, type); terom@174: } terom@174: terom@174: void* object_cast (struct object *obj, const struct object_type *type) terom@174: { terom@174: assert(object_type_check(obj->type, type)); terom@174: terom@174: // ok, return as void* terom@174: return obj; terom@174: } terom@174: terom@174: const void* object_type (struct object *obj, const struct object_type *type) terom@174: { terom@174: assert(object_type_check(obj->type, type)); terom@174: terom@174: // ok, return as void* terom@174: return obj->type; terom@174: }