src/object.c
author Tero Marttila <terom@fixme.fi>
Thu, 21 May 2009 16:57:56 +0300
changeset 213 f0e52e026197
parent 174 c56680e9e021
permissions -rw-r--r--
implement lua_console_on_interrupt to abort any executing thread
#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;
}