src/lib/lua_type.h
branchnew-lib-errors
changeset 219 cefec18b8268
parent 145 a5582e1a83da
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/lua_type.h	Thu May 28 01:17:36 2009 +0300
@@ -0,0 +1,74 @@
+#ifndef LUA_TYPE_H
+#define LUA_TYPE_H
+
+/**
+ * @file
+ *
+ * Convenience functions for defining "types" in lua
+ */
+#include <lua5.1/lua.h>
+
+// XXX: remove
+#include <lua5.1/lauxlib.h>
+
+/**
+ * A type's method
+ *
+ * XXX: a name field?
+ */
+struct lua_method {
+    /** The name of the method */
+    const char *name;
+
+    /** The function pointer */
+    lua_CFunction func;
+
+    /** The function definition, optional */
+    const struct lua_func *info;
+};
+
+#define LUA_METHOD(name, func, info) \
+    { (name), (func), (info) }
+
+#define LUA_METHODS(...) \
+    { __VA_ARGS__, { NULL, NULL, NULL } }
+
+/**
+ * A type
+ */
+struct lua_type {
+    /** The name of the type */
+    const char *name;
+};
+
+#define LUA_TYPE(name) \
+    { (name) }
+
+/**
+ * Register a new metadata table for the given type in the given lua state.
+ *
+ * This leaves the new type (metatable) on the stack.
+ */
+void lua_type_register (lua_State *L, const struct lua_type *type, const struct lua_method methods[]);
+
+/**
+ * Create a new instance of the given type.
+ *
+ * This leaves the new userdata object on the stack.
+ */
+void* lua_type_create (lua_State *L, const struct lua_type *type, size_t size);
+
+/**
+ * Create a new userdata type, and also create an instance of it, register it as a global, and return it.
+ *
+ * This leaves the new userdata object on the stack.
+ */
+void* lua_type_register_global (lua_State *L, const struct lua_type *type, const struct lua_method methods[],
+        const char *global_name, size_t size);
+
+/**
+ * Get an object of the given type from the given stack position
+ */
+void* lua_type_get (lua_State *L, const struct lua_type *type, int index);
+
+#endif