src/lib/object.c
author Tero Marttila <terom@fixme.fi>
Wed, 27 May 2009 23:57:48 +0300
branchnew-lib-errors
changeset 217 7728d6ec3abf
parent 174 src/object.c@c56680e9e021
permissions -rw-r--r--
nexus.c compiles
#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;
}