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::tuplestd::variant(cannot contain:std::optional,std::tuple,std::variant)std::optional(u)int(8|16|32|64)_t,float,doublestd::stringstd::vector(cannot contain:std::optional,std::tuple)std::map(cannot contain:std::optional,std::tuple)- TODO:
std::tupleinstd::tuple - TODO: maybe
std::tupleinstd::vectorifstd::vectornot instd::variant - TODO: use of Reflection far in the future
- Describe the types of a function parameters in
std::tupleorstd::variantusing supported constructs. - Call
cArgParsewith passing to it thesestd::tupleorstd::variantand 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.