src/lib/lua_type.h
branchnew-lib-errors
changeset 219 cefec18b8268
parent 145 a5582e1a83da
equal deleted inserted replaced
218:5229a5d098b2 219:cefec18b8268
       
     1 #ifndef LUA_TYPE_H
       
     2 #define LUA_TYPE_H
       
     3 
       
     4 /**
       
     5  * @file
       
     6  *
       
     7  * Convenience functions for defining "types" in lua
       
     8  */
       
     9 #include <lua5.1/lua.h>
       
    10 
       
    11 // XXX: remove
       
    12 #include <lua5.1/lauxlib.h>
       
    13 
       
    14 /**
       
    15  * A type's method
       
    16  *
       
    17  * XXX: a name field?
       
    18  */
       
    19 struct lua_method {
       
    20     /** The name of the method */
       
    21     const char *name;
       
    22 
       
    23     /** The function pointer */
       
    24     lua_CFunction func;
       
    25 
       
    26     /** The function definition, optional */
       
    27     const struct lua_func *info;
       
    28 };
       
    29 
       
    30 #define LUA_METHOD(name, func, info) \
       
    31     { (name), (func), (info) }
       
    32 
       
    33 #define LUA_METHODS(...) \
       
    34     { __VA_ARGS__, { NULL, NULL, NULL } }
       
    35 
       
    36 /**
       
    37  * A type
       
    38  */
       
    39 struct lua_type {
       
    40     /** The name of the type */
       
    41     const char *name;
       
    42 };
       
    43 
       
    44 #define LUA_TYPE(name) \
       
    45     { (name) }
       
    46 
       
    47 /**
       
    48  * Register a new metadata table for the given type in the given lua state.
       
    49  *
       
    50  * This leaves the new type (metatable) on the stack.
       
    51  */
       
    52 void lua_type_register (lua_State *L, const struct lua_type *type, const struct lua_method methods[]);
       
    53 
       
    54 /**
       
    55  * Create a new instance of the given type.
       
    56  *
       
    57  * This leaves the new userdata object on the stack.
       
    58  */
       
    59 void* lua_type_create (lua_State *L, const struct lua_type *type, size_t size);
       
    60 
       
    61 /**
       
    62  * Create a new userdata type, and also create an instance of it, register it as a global, and return it.
       
    63  *
       
    64  * This leaves the new userdata object on the stack.
       
    65  */
       
    66 void* lua_type_register_global (lua_State *L, const struct lua_type *type, const struct lua_method methods[],
       
    67         const char *global_name, size_t size);
       
    68 
       
    69 /**
       
    70  * Get an object of the given type from the given stack position
       
    71  */
       
    72 void* lua_type_get (lua_State *L, const struct lua_type *type, int index);
       
    73 
       
    74 #endif