Part one of refactor of LS.ckint/LS.tryint/LS.isint

This commit is contained in:
2024-03-13 17:46:26 -04:00
parent fd8166f09c
commit 26d0715deb
8 changed files with 262 additions and 149 deletions

View File

@@ -96,95 +96,73 @@ void LuaCoreStack::argerr(const char *nm, const char *tp) const {
luaL_error(L_, "'%s' should be %s", nm, tp);
}
bool LuaCoreStack::isinteger(LuaSlot s) const {
if (lua_type(L_, s) == LUA_TNUMBER) {
lua_Number result = lua_tonumber(L_, s);
if (lua_Integer(result) == result) return true;
std::optional<bool> LuaCoreStack::tryboolean(LuaSlot s) const {
if (lua_type(L_, s) == LUA_TBOOLEAN) {
return lua_toboolean(L_, s);
}
return false;
return std::nullopt;
}
bool LuaCoreStack::isint(LuaSlot s) const {
std::optional<lua_Integer> LuaCoreStack::tryinteger(LuaSlot s) const {
if (lua_type(L_, s) == LUA_TNUMBER) {
lua_Number result = lua_tonumber(L_, s);
if (int(result) == result) return true;
}
return false;
}
bool LuaCoreStack::isxyz(LuaSlot s) const {
if (lua_istable(L_, s) && (lua_nkeys(L_, s) == 3)) {
int top = lua_gettop(L_);
lua_rawgeti(L_, s, 3);
lua_rawgeti(L_, s, 2);
lua_rawgeti(L_, s, 1);
if (lua_isnumber(L_, -1) && lua_isnumber(L_, -2) && lua_isnumber(L_, -3)) {
lua_settop(L_, top);
return true;
if (lua_Integer(result) == result) {
return lua_Integer(result);
}
lua_settop(L_, top);
}
return false;
return std::nullopt;
}
bool LuaCoreStack::ckboolean(LuaSlot s) const {
checkboolean(s, "value");
return lua_toboolean(L_, s) ? true:false;
}
lua_Integer LuaCoreStack::ckinteger(LuaSlot s) const {
checknumber(s, "value");
luaL_checktype(L_, s, LUA_TNUMBER);
lua_Number result = lua_tonumber(L_, s);
lua_Integer iresult(result);
if (iresult != result) {
luaL_error(L_, "not a valid integer");
return 0;
std::optional<int> LuaCoreStack::tryint(LuaSlot s) const {
if (lua_type(L_, s) == LUA_TNUMBER) {
lua_Number result = lua_tonumber(L_, s);
if (int(result) == result) {
return int(result);
}
}
return iresult;
return std::nullopt;
}
int LuaCoreStack::ckint(LuaSlot s) const {
checknumber(s, "value");
lua_Number result = lua_tonumber(L_, s);
int iresult(result);
if (iresult != result) {
luaL_error(L_, "not a valid int");
return 0;
std::optional<lua_Number> LuaCoreStack::trynumber(LuaSlot s) const {
if (lua_type(L_, s) == LUA_TNUMBER) {
return lua_tonumber(L_, s);
}
return iresult;
return std::nullopt;
}
lua_Number LuaCoreStack::cknumber(LuaSlot s) const {
checknumber(s, "value");
return lua_tonumber(L_, s);
std::optional<eng::string> LuaCoreStack::trystring(LuaSlot s) const {
if (lua_type(L_, s) == LUA_TSTRING) {
size_t len;
const char *str = lua_tolstring(L_, s, &len);
return eng::string(str, len);
}
return std::nullopt;
}
eng::string LuaCoreStack::ckstring(LuaSlot s) const {
checkstring(s, "value");
size_t len;
const char *str = lua_tolstring(L_, s, &len);
return eng::string(str, len);
std::optional<std::string_view> LuaCoreStack::trystringview(LuaSlot s) const {
if (lua_type(L_, s) == LUA_TSTRING) {
size_t len;
const char *str = lua_tolstring(L_, s, &len);
return std::string_view(str, len);
}
return std::nullopt;
}
std::string_view LuaCoreStack::ckstringview(LuaSlot s) const {
checkstring(s, "value");
size_t len;
const char *str = lua_tolstring(L_, s, &len);
return std::string_view(str, len);
std::optional<lua_State*> LuaCoreStack::trythread(LuaSlot s) const {
if (lua_type(L_, s) == LUA_TTHREAD) {
return lua_tothread(L_, s);
}
return std::nullopt;
}
lua_State *LuaCoreStack::ckthread(LuaSlot s) const {
checkthread(s, "value");
return lua_tothread(L_, s);
std::optional<LuaToken> LuaCoreStack::trytoken(LuaSlot s) const {
if (lua_type(L_, s) == LUA_TLIGHTUSERDATA) {
return LuaToken(lua_touserdata(L_, s));
}
return std::nullopt;
}
LuaToken LuaCoreStack::cktoken(LuaSlot s) const {
checktoken(s, "value");
return LuaToken(lua_touserdata(L_, s));
}
util::DXYZ LuaCoreStack::ckxyz(LuaSlot s) const {
std::optional<util::DXYZ> LuaCoreStack::tryxyz(LuaSlot s) const {
if (lua_istable(L_, s) && (lua_nkeys(L_, s) == 3)) {
int top = lua_gettop(L_);
lua_rawgeti(L_, s, 3);
@@ -200,10 +178,106 @@ util::DXYZ LuaCoreStack::ckxyz(LuaSlot s) const {
}
lua_settop(L_, top);
}
argerr("argument", "vector");
return util::DXYZ();
return std::nullopt;
}
bool LuaCoreStack::trytable(LuaSlot s) const {
return lua_istable(L_, s);
}
bool LuaCoreStack::trynil(LuaSlot s) const {
return lua_isnil(L_, s);
}
bool LuaCoreStack::tryfunction(LuaSlot s) const {
return lua_isfunction(L_, s);
}
bool LuaCoreStack::trycfunction(LuaSlot s) const {
return lua_iscfunction(L_, s);
}
bool LuaCoreStack::trytangible(LuaSlot s) const {
return (lua_istable(L_, s) && gettabletype(s) == LUA_TT_TANGIBLE);
}
bool LuaCoreStack::ckboolean(LuaSlot s, const char *argname) const {
auto result = tryboolean(s);
if (!result) argerr(argname, "boolean");
return *result;
}
lua_Integer LuaCoreStack::ckinteger(LuaSlot s, const char *argname) const {
auto result = tryinteger(s);
if (!result) argerr(argname, "integer");
return *result;
}
int LuaCoreStack::ckint(LuaSlot s, const char *argname) const {
auto result = tryint(s);
if (!result) argerr(argname, "int");
return *result;
}
lua_Number LuaCoreStack::cknumber(LuaSlot s, const char *argname) const {
auto result = trynumber(s);
if (!result) argerr(argname, "number");
return *result;
}
eng::string LuaCoreStack::ckstring(LuaSlot s, const char *argname) const {
auto result = trystring(s);
if (!result) argerr(argname, "string");
return *result;
}
std::string_view LuaCoreStack::ckstringview(LuaSlot s, const char *argname) const {
auto result = trystringview(s);
if (!result) argerr(argname, "string");
return *result;
}
lua_State * LuaCoreStack::ckthread(LuaSlot s, const char *argname) const {
auto result = trythread(s);
if (!result) argerr(argname, "thread");
return *result;
}
LuaToken LuaCoreStack::cktoken(LuaSlot s, const char *argname) const {
auto result = trytoken(s);
if (!result) argerr(argname, "token");
return *result;
}
util::DXYZ LuaCoreStack::ckxyz(LuaSlot s, const char *argname) const {
auto result = tryxyz(s);
if (!result) argerr(argname, "xyz");
return *result;
}
void LuaCoreStack::cktable(LuaSlot s, const char *argname) const {
if (!trytable(s)) argerr(argname, "table");
}
void LuaCoreStack::cknil(LuaSlot s, const char *argname) const {
if (!trynil(s)) argerr(argname, "nil");
}
void LuaCoreStack::ckfunction(LuaSlot s, const char *argname) const {
if (!tryfunction(s)) argerr(argname, "function");
}
void LuaCoreStack::ckcfunction(LuaSlot s, const char *argname) const {
if (!trycfunction(s)) argerr(argname, "c-function");
}
void LuaCoreStack::cktangible(LuaSlot s, const char *argname) const {
if (!trytangible(s)) argerr(argname, "tangible");
}
void LuaCoreStack::clearmetatable(LuaSlot tab) const {
lua_pushnil(L_);
lua_setmetatable(L_, tab);