implement a generic object/object-type thing
authorTero Marttila <terom@fixme.fi>
Thu, 07 May 2009 02:12:31 +0300
changeset 174 c56680e9e021
parent 173 1a7afcd2dd1a
child 175 a816950a6548
implement a generic object/object-type thing
src/object.c
src/object.h
--- /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;
+}
--- /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 <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 */