src/lib/object.h
author Tero Marttila <terom@fixme.fi>
Wed, 27 May 2009 23:57:48 +0300
branchnew-lib-errors
changeset 217 7728d6ec3abf
parent 174 src/object.h@c56680e9e021
permissions -rw-r--r--
nexus.c compiles
#ifndef OBJECT_H
#define OBJECT_H

/**
 * @file
 *
 * The thing feared by every C-programmer - calling C structs "objects".
 */
#include <stdbool.h>

/**
 * Basic object type info
 */
struct object_type {
    /** Parent type for nested structs, NULL if the parent is object_type */
    const struct object_type *parent;
};

/**
 * Basic object state
 */
struct object {
    /** Object's type */
    const struct object_type *type;
};

/**
 * Initialize an object to the given type.
 */
void object_init (struct object *obj, const struct object_type *type);

/**
 * Check that the given object type is compatible with the given type.
 */
bool object_type_check (const struct object_type *obj_type, const struct object_type *type);

/**
 * Check that the given object "implements" the given type.
 */
bool object_check (struct object *obj, const struct object_type *type);

/**
 * Return the given object as a pointer suitable for casting to the struct for the given type.
 *
 * It is a bug to call this with an object that's not compatible with the given type.
 */
void* object_cast (struct object *obj, const struct object_type *type);

/**
 * Return a pointer to the object's type that is compatible with the given type.
 *
 * It is a bug to call this with a object that's not compatible with the given type.
 */
const void* object_type (struct object *obj, const struct object_type *type);

#endif /* OBJECT_H */