C++17, declarative arguments type checking, metaprogramming.
When writing C functions that are registered and called from Lua, it is often necessary to write checks on the received arguments against the required data types. By declaratively describing and passing parameter types to this library, it takes care of the bulk of these checks.
static int32_t test(lua_State* L) {
std::tuple<std::variant<int32_t, std::string>, std::optional<float>> args;
std::string errorStr;
if (!utils::lua::cArgParse(L, args, errorStr)) {
luaL_error(L, errorStr.c_str());
return 0;
}
// Don't call lua_pop(L, lua_gettop(L)) - it's already in cArgParse.
return process(args);
}
...
lua_register(L, "test", test);
assert(luaL_dostring(L, "test(123)") == LUA_OK);
More examples can be found in tests/test.cpp.
std::tuple
std::variant
(cannot contain:std::optional
,std::tuple
,std::variant
)std::optional
(u)int(8|16|32|64)_t
,float
,double
std::string
std::vector
(cannot contain:std::optional
,std::tuple
)std::map
(cannot contain:std::optional
,std::tuple
)- TODO:
std::tuple
instd::tuple
- TODO: maybe
std::tuple
instd::vector
ifstd::vector
not instd::variant
- TODO: use of Reflection far in the future
- Describe the types of a function parameters in
std::tuple
orstd::variant
using supported constructs. - Call
cArgParse
with passing to it thesestd::tuple
orstd::variant
and check the arguments parsing result through the returned ErrorString or Boolean. An empty ErrorString means successful parsing. - If there was a parsing error occured, handle the error string as you want -
pass to
luaL_error
, pass to a logger, etc.