# HG changeset patch # User Tero Marttila # Date 1241651551 -10800 # Node ID c56680e9e0216606bf78072ee70ac926e9ef0f56 # Parent 1a7afcd2dd1a64222ca7d2a530e6617a4b069227 implement a generic object/object-type thing diff -r 1a7afcd2dd1a -r c56680e9e021 src/object.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/object.c Thu May 07 02:12:31 2009 +0300 @@ -0,0 +1,53 @@ +#include "object.h" + +#include +#include + +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; +} diff -r 1a7afcd2dd1a -r c56680e9e021 src/object.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/object.h Thu May 07 02:12:31 2009 +0300 @@ -0,0 +1,56 @@ +#ifndef OBJECT_H +#define OBJECT_H + +/** + * @file + * + * The thing feared by every C-programmer - calling C structs "objects". + */ +#include + +/** + * 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 */