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

@@ -149,6 +149,7 @@
#include <cstring>
#include <type_traits>
#include <cassert>
#include <optional>
#include "lua.h"
#include "lauxlib.h"
@@ -354,7 +355,6 @@ private:
}
void argerr(const char *arg, const char *tp) const;
public:
LuaCoreStack(lua_State *L) : L_(L) {}
@@ -368,43 +368,69 @@ public:
int type(LuaSlot s) const { return lua_type(L_, s); }
void checktype(LuaSlot s, int type) const { luaL_checktype(L_, s, type); }
// These functions try to turn a Lua value into a C++ value.
// If the lua value doesn't match the desired type, then these return
// false or an empty optional. The ones that return bool only verify
// the value's type, they don't actually fetch the value.
std::optional<bool> tryboolean(LuaSlot s) const;
std::optional<lua_Integer> tryinteger(LuaSlot s) const;
std::optional<int> tryint(LuaSlot s) const;
std::optional<lua_Number> trynumber(LuaSlot s) const;
std::optional<eng::string> trystring(LuaSlot s) const;
std::optional<std::string_view> trystringview(LuaSlot s) const;
std::optional<lua_State*> trythread(LuaSlot s) const;
std::optional<LuaToken> trytoken(LuaSlot s) const;
std::optional<util::DXYZ> tryxyz(LuaSlot s) const;
bool trytable(LuaSlot s) const;
bool trynil(LuaSlot s) const;
bool tryfunction(LuaSlot s) const;
bool trycfunction(LuaSlot s) const;
bool trytangible(LuaSlot s) const;
// These functions turn a Lua value into a C++ value.
// If the lua value doesn't match the desired type,
// then these throw a lua error. The argname is used
// for making a nice error message.
bool istable(LuaSlot s) const { return lua_type(L_, s) == LUA_TTABLE; }
bool isstring(LuaSlot s) const { return lua_type(L_, s) == LUA_TSTRING; }
bool isnumber(LuaSlot s) const { return lua_type(L_, s) == LUA_TNUMBER; }
bool isinteger(LuaSlot s) const;
bool isint(LuaSlot s) const;
bool isthread(LuaSlot s) const { return lua_type(L_, s) == LUA_TTHREAD; }
bool isfunction(LuaSlot s) const { return lua_type(L_, s) == LUA_TFUNCTION; }
bool iscfunction(LuaSlot s) const { return lua_iscfunction(L_, s) != 0; }
bool ckboolean(LuaSlot s, const char *argname = "value") const;
lua_Integer ckinteger(LuaSlot s, const char *argname = "value") const;
int ckint(LuaSlot s, const char *argname = "value") const;
lua_Number cknumber(LuaSlot s, const char *argname = "value") const;
eng::string ckstring(LuaSlot s, const char *argname = "value") const;
std::string_view ckstringview(LuaSlot s, const char *argname = "value") const;
lua_State * ckthread(LuaSlot s, const char *argname = "value") const;
LuaToken cktoken(LuaSlot s, const char *argname = "value") const;
util::DXYZ ckxyz(LuaSlot s, const char *argname = "value") const;
void cktable(LuaSlot s, const char *argname = "value") const;
void cknil(LuaSlot s, const char *argname = "value") const;
void ckfunction(LuaSlot s, const char *argname = "value") const;
void ckcfunction(LuaSlot s, const char *argname = "value") const;
void cktangible(LuaSlot s, const char *argname = "value") const;
// These functions check if a value can be converted
// to a C++ value. They don't actually return the C++ value.
// It is more efficient to use the 'try' or 'ck' functions above if
// you also want the value itself.
bool isboolean(LuaSlot s) const { return lua_type(L_, s) == LUA_TBOOLEAN; }
bool isnil(LuaSlot s) const { return lua_type(L_, s) == LUA_TNIL; }
bool isinteger(LuaSlot s) const { return bool(tryinteger(s)); }
bool isint(LuaSlot s) const { return bool(tryint(s)); }
bool isnumber(LuaSlot s) const { return lua_type(L_, s) == LUA_TNUMBER; }
bool isstring(LuaSlot s) const { return lua_type(L_, s) == LUA_TSTRING; }
bool isstringview(LuaSlot s) const { return lua_type(L_, s) == LUA_TSTRING; }
bool isthread(LuaSlot s) const { return lua_type(L_, s) == LUA_TTHREAD; }
bool istoken(LuaSlot s) const { return lua_islightuserdata(L_, s) != 0; }
bool isxyz(LuaSlot s) const;
bool isxyz(LuaSlot s) const { return bool(tryxyz(s)); }
bool istable(LuaSlot s) const { return lua_type(L_, s) == LUA_TTABLE; }
bool isnil(LuaSlot s) const { return lua_type(L_, s) == LUA_TNIL; }
bool isfunction(LuaSlot s) const { return lua_isfunction(L_, s); }
bool iscfunction(LuaSlot s) const { return lua_iscfunction(L_, s) != 0; }
bool istangible(LuaSlot s) const { return trytangible(s); }
void checktable(LuaSlot s, const char *n) const { if (!istable(s)) argerr(n, "table"); }
void checkstring(LuaSlot s, const char *n) const { if (!isstring(s)) argerr(n, "string"); }
void checknumber(LuaSlot s, const char *n) const { if (!isnumber(s)) argerr(n, "number"); }
void checkinteger(LuaSlot s, const char *n) const { if (!isinteger(s)) argerr(n, "integer"); }
void checkint(LuaSlot s, const char *n) const { if (!isint(s)) argerr(n, "int"); }
void checkthread(LuaSlot s, const char *n) const { if (!isthread(s)) argerr(n, "thread"); }
void checkfunction(LuaSlot s, const char *n) const { if (!isfunction(s)) argerr(n, "function"); }
void checkcfunction(LuaSlot s, const char *n) const { if (!iscfunction(s)) argerr(n, "cfunction"); }
void checkboolean(LuaSlot s, const char *n) const { if (!isboolean(s)) argerr(n, "boolean"); }
void checknil(LuaSlot s, const char *n) const { if (!isnil(s)) argerr(n, "nil"); }
void checktoken(LuaSlot s, const char *n) const { if (!istoken(s)) argerr(n, "token"); }
void checkxyz(LuaSlot s, const char *n) const { if (!isxyz(s)) argerr(n, "xyz"); }
bool ckboolean(LuaSlot s) const;
lua_Integer ckinteger(LuaSlot s) const;
int ckint(LuaSlot s) const;
lua_Number cknumber(LuaSlot s) const;
eng::string ckstring(LuaSlot s) const;
std::string_view ckstringview(LuaSlot s) const;
lua_State *ckthread(LuaSlot s) const;
LuaToken cktoken(LuaSlot s) const;
util::DXYZ ckxyz(LuaSlot s) const;
void clearmetatable(LuaSlot tab) const;
void setmetatable(LuaSlot tab, LuaSlot mt) const;
bool getmetatable(LuaSlot mt, LuaSlot tab) const;