src/object.c
changeset 174 c56680e9e021
--- /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 <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;
+}