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