diff options
author | IronClawTrem <louie.nutman@gmail.com> | 2020-02-16 03:40:06 +0000 |
---|---|---|
committer | IronClawTrem <louie.nutman@gmail.com> | 2020-02-16 03:40:06 +0000 |
commit | 425decdf7e9284d15aa726e3ae96b9942fb0e3ea (patch) | |
tree | 6c0dd7edfefff1be7b9e75fe0b3a0a85fe1595f3 /src/script/rapidjson/luax.hpp | |
parent | ccb0b2e4d6674a7a00c9bf491f08fc73b6898c54 (diff) |
create tremded branch
Diffstat (limited to 'src/script/rapidjson/luax.hpp')
-rw-r--r-- | src/script/rapidjson/luax.hpp | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/script/rapidjson/luax.hpp b/src/script/rapidjson/luax.hpp new file mode 100644 index 0000000..e8ea7c6 --- /dev/null +++ b/src/script/rapidjson/luax.hpp @@ -0,0 +1,82 @@ +#ifndef __LUA_RAPIDJSION_LUACOMPAT_H__ +#define __LUA_RAPIDJSION_LUACOMPAT_H__ + +#include <cmath> +#include <limits> +#include <cstdint> + +#include "qcommon/q3_lauxlib.h" + +#include "lua.hpp" + +namespace luax +{ + inline void setfuncs(lua_State* L, const luaL_Reg* funcs) + { luaL_setfuncs(L, funcs, 0); } + + inline size_t rawlen(lua_State* L, int idx) + { return lua_rawlen(L, idx); } + + inline bool isinteger(lua_State* L, int idx, int64_t* out = nullptr) + { + if (lua_isinteger(L, idx)) // but it maybe not detect all integers. + { + if (out) + *out = lua_tointeger(L, idx); + return true; + } + + double intpart; + if (std::modf(lua_tonumber(L, idx), &intpart) == 0.0) + { + if (std::numeric_limits<lua_Integer>::min() <= intpart + && intpart <= std::numeric_limits<lua_Integer>::max()) + { + if (out) + *out = static_cast<int64_t>(intpart); + return true; + } + } + return false; + } + + inline int typerror(lua_State* L, int narg, const char* tname) + { + const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, luaL_typename(L, narg)); + return luaL_argerror(L, narg, msg); + } + + inline bool optboolfield(lua_State* L, int idx, const char* name, bool def) + { + auto v = def; + auto t = lua_type(L, idx); + + if (t != LUA_TTABLE && t != LUA_TNONE) + luax::typerror(L, idx, "table"); + + if (t != LUA_TNONE) + { + lua_getfield(L, idx, name); + if (!lua_isnoneornil(L, -1)) + v = lua_toboolean(L, -1) != 0; + lua_pop(L, 1); + } + + return v; + } + + inline int optintfield(lua_State* L, int idx, const char* name, int def) + { + auto v = def; + lua_getfield(L, idx, name); + + if (lua_isnumber(L, -1)) + v = static_cast<int>(lua_tointeger(L, -1)); + + lua_pop(L, 1); + return v; + } + +} + +#endif |