nngn
|
Functions/macros for registering user types. More...
Go to the source code of this file.
Classes | |
class | nngn::lua::static_register |
Registers a function to be executed when the Lua state is initialized. More... | |
Namespaces | |
namespace | nngn |
namespace | nngn::lua |
Macros | |
#define | NNGN_LUA_PROXY(T, ...) NNGN_OVERLOAD(NNGN_LUA_PROXY, T __VA_OPT__(,) __VA_ARGS__) |
Macro to automatically create and register a user type meta table. | |
#define | NNGN_LUA_PROXY1(T) NNGN_LUA_PROXY2(T, [](auto){}) |
#define | NNGN_LUA_PROXY2(T, f) |
#define | NNGN_LUA_DECLARE_USER_TYPE(T, ...) NNGN_OVERLOAD(NNGN_LUA_DECLARE_USER_TYPE, T __VA_OPT__(,) __VA_ARGS__) |
Declares that T objects should be manipulated as a user type. | |
#define | NNGN_LUA_DECLARE_USER_TYPE1(T) NNGN_LUA_DECLARE_USER_TYPE2(T, #T) |
#define | NNGN_LUA_DECLARE_USER_TYPE2(T, N) |
Functions/macros for registering user types.
The macros NNGN_LUA_DECLARE_USER_TYPE and NNGN_LUA_PROXY can be used to create and automatically register a user data proxy for a type. The first declares that a type is to be pushed/popped as user data and declares its meta table key in the global table. Two versions are available:
NNGN_LUA_DECLARE_USER_TYPE(T)
declares the type T
with key "T"
(i.e. #T
).NNGN_LUA_DECLARE_USER_TYPE(T, "U")
uses a different name for the meta table, also useful when the argument has other characters (e.g. namespace qualifiers).The macro NNGN_LUA_PROXY defines the members of the meta table. It takes as parameters the type (T
) and a function which will be called to register the members:
The second line will arrange for the registration function to be called when the state is initialized (see nngn::lua::static_register). The nngn::lua::table_view argument is a reference to the meta table which will be:
T
pushed onto the stackNNGN_LUA_PROXY only needs to be called once for each type (the registration must be performed at run time before any user data values are used). NNGN_LUA_DECLARE_USER_TYPE has to appear in every translation unit before that type is used in stack operations.
Below is an example of a registration function:
The following fields are set in the meta table:
constant
is a Lua number set to the value of T::constant
.accessor
is a Lua function which takes a T*
and returns a pointer to its member_obj
member (note that S
also has to be registered as a user type in this case).value_accessor
is a Lua function which takes a T*
and returns (a copy of) its member_obj
member by value.member_fn
is a Lua function which takes a T*
and a float
(i.e. the same arguments as T::member_fn
) and returns an int
(the return value of the same function).lambda
is a Lua function which takes two lua_Number
s (one converted to float
) and returns a lua_Number
(a regular function or function pointer could also be used).c_fn
is a Lua function implemented as a lua_CFunction
. No special treatment of arguments or return values is done.Note that these macros declare members of the nngn::lua namespace, so they should appear in the global namespace. They do not affect the name resolution of T
itself, it will be resolved as if it had been used just outside the macro calls.
#define NNGN_LUA_DECLARE_USER_TYPE | ( | T, | |
... ) NNGN_OVERLOAD(NNGN_LUA_DECLARE_USER_TYPE, T __VA_OPT__(,) __VA_ARGS__) |
Declares that T
objects should be manipulated as a user type.
Required for stack operations involving user data values of this type. A second optional parameter N
can specify the name (default: #T
).
#define NNGN_LUA_DECLARE_USER_TYPE1 | ( | T | ) | NNGN_LUA_DECLARE_USER_TYPE2(T, #T) |
#define NNGN_LUA_PROXY | ( | T, | |
... ) NNGN_OVERLOAD(NNGN_LUA_PROXY, T __VA_OPT__(,) __VA_ARGS__) |
Macro to automatically create and register a user type meta table.
#define NNGN_LUA_PROXY1 | ( | T | ) | NNGN_LUA_PROXY2(T, [](auto){}) |