aboutsummaryrefslogtreecommitdiff
path: root/src/compat.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/compat.hpp')
-rw-r--r--src/compat.hpp26
1 files changed, 18 insertions, 8 deletions
diff --git a/src/compat.hpp b/src/compat.hpp
index 59c8001..f66f703 100644
--- a/src/compat.hpp
+++ b/src/compat.hpp
@@ -115,6 +115,7 @@ enum class LuaError
115 ERRFILE = LUA_ERRFILE 115 ERRFILE = LUA_ERRFILE
116}; 116};
117 117
118[[nodiscard]]
118inline constexpr LuaError ToLuaError(int const rc_) 119inline constexpr LuaError ToLuaError(int const rc_)
119{ 120{
120 assert(rc_ == LUA_OK || rc_ == LUA_YIELD || rc_ == LUA_ERRRUN || rc_ == LUA_ERRSYNTAX || rc_ == LUA_ERRMEM || rc_ == LUA_ERRGCMM || rc_ == LUA_ERRERR || rc_ == LUA_ERRFILE); 121 assert(rc_ == LUA_OK || rc_ == LUA_YIELD || rc_ == LUA_ERRRUN || rc_ == LUA_ERRSYNTAX || rc_ == LUA_ERRMEM || rc_ == LUA_ERRGCMM || rc_ == LUA_ERRERR || rc_ == LUA_ERRFILE);
@@ -124,6 +125,7 @@ inline constexpr LuaError ToLuaError(int const rc_)
124// ################################################################################################# 125// #################################################################################################
125 126
126// break lexical order for that one because it's needed below 127// break lexical order for that one because it's needed below
128[[nodiscard]]
127inline LuaType luaG_type(lua_State* const L_, StackIndex const idx_) 129inline LuaType luaG_type(lua_State* const L_, StackIndex const idx_)
128{ 130{
129 return static_cast<LuaType>(lua_type(L_, idx_)); 131 return static_cast<LuaType>(lua_type(L_, idx_));
@@ -139,21 +141,24 @@ inline LuaType luaG_type(lua_State* const L_, StackIndex const idx_)
139#define STRINGVIEW_FMT "%.*s" 141#define STRINGVIEW_FMT "%.*s"
140 142
141// a replacement of lua_tolstring 143// a replacement of lua_tolstring
142[[nodiscard]] inline std::string_view luaG_tostring(lua_State* const L_, StackIndex const idx_) 144[[nodiscard]]
145inline std::string_view luaG_tostring(lua_State* const L_, StackIndex const idx_)
143{ 146{
144 size_t _len{ 0 }; 147 size_t _len{ 0 };
145 char const* _str{ lua_tolstring(L_, idx_, &_len) }; 148 char const* _str{ lua_tolstring(L_, idx_, &_len) };
146 return _str ? std::string_view{ _str, _len } : ""; 149 return _str ? std::string_view{ _str, _len } : "";
147} 150}
148 151
149[[nodiscard]] inline std::string_view luaG_checkstring(lua_State* const L_, StackIndex const idx_) 152[[nodiscard]]
153inline std::string_view luaG_checkstring(lua_State* const L_, StackIndex const idx_)
150{ 154{
151 size_t _len{ 0 }; 155 size_t _len{ 0 };
152 char const* _str{ luaL_checklstring(L_, idx_, &_len) }; 156 char const* _str{ luaL_checklstring(L_, idx_, &_len) };
153 return std::string_view{ _str, _len }; 157 return std::string_view{ _str, _len };
154} 158}
155 159
156[[nodiscard]] inline std::string_view luaG_optstring(lua_State* const L_, StackIndex const idx_, std::string_view const& default_) 160[[nodiscard]]
161inline std::string_view luaG_optstring(lua_State* const L_, StackIndex const idx_, std::string_view const& default_)
157{ 162{
158 if (lua_isnoneornil(L_, idx_)) { 163 if (lua_isnoneornil(L_, idx_)) {
159 return default_; 164 return default_;
@@ -352,7 +357,8 @@ static inline void luaG_newlib(lua_State* const L_, luaL_Reg const (&funcs_)[N])
352// ################################################################################################# 357// #################################################################################################
353 358
354template <typename T> 359template <typename T>
355[[nodiscard]] T* luaG_newuserdatauv(lua_State* L_, UserValueCount nuvalue_) 360[[nodiscard]]
361T* luaG_newuserdatauv(lua_State* const L_, UserValueCount const nuvalue_)
356{ 362{
357 return static_cast<T*>(lua_newuserdatauv(L_, sizeof(T), nuvalue_)); 363 return static_cast<T*>(lua_newuserdatauv(L_, sizeof(T), nuvalue_));
358} 364}
@@ -394,7 +400,8 @@ inline void luaG_setmetatable(lua_State* const L_, std::string_view const& tname
394 400
395// a small helper to extract a full userdata pointer from the stack in a safe way 401// a small helper to extract a full userdata pointer from the stack in a safe way
396template <typename T> 402template <typename T>
397[[nodiscard]] T* luaG_tofulluserdata(lua_State* const L_, StackIndex const index_) 403[[nodiscard]]
404T* luaG_tofulluserdata(lua_State* const L_, StackIndex const index_)
398{ 405{
399 LUA_ASSERT(L_, lua_isnil(L_, index_) || lua_type(L_, index_) == LUA_TUSERDATA); 406 LUA_ASSERT(L_, lua_isnil(L_, index_) || lua_type(L_, index_) == LUA_TUSERDATA);
400 return static_cast<T*>(lua_touserdata(L_, index_)); 407 return static_cast<T*>(lua_touserdata(L_, index_));
@@ -403,7 +410,8 @@ template <typename T>
403// ------------------------------------------------------------------------------------------------- 410// -------------------------------------------------------------------------------------------------
404 411
405template <typename T> 412template <typename T>
406[[nodiscard]] auto luaG_tolightuserdata(lua_State* const L_, StackIndex const index_) 413[[nodiscard]]
414auto luaG_tolightuserdata(lua_State* const L_, StackIndex const index_)
407{ 415{
408 LUA_ASSERT(L_, lua_isnil(L_, index_) || lua_islightuserdata(L_, index_)); 416 LUA_ASSERT(L_, lua_isnil(L_, index_) || lua_islightuserdata(L_, index_));
409 if constexpr (std::is_pointer_v<T>) { 417 if constexpr (std::is_pointer_v<T>) {
@@ -415,14 +423,16 @@ template <typename T>
415 423
416// ------------------------------------------------------------------------------------------------- 424// -------------------------------------------------------------------------------------------------
417 425
418[[nodiscard]] inline std::string_view luaG_typename(lua_State* const L_, LuaType const t_) 426[[nodiscard]]
427inline std::string_view luaG_typename(lua_State* const L_, LuaType const t_)
419{ 428{
420 return lua_typename(L_, static_cast<int>(t_)); 429 return lua_typename(L_, static_cast<int>(t_));
421} 430}
422 431
423// ------------------------------------------------------------------------------------------------- 432// -------------------------------------------------------------------------------------------------
424 433
425[[nodiscard]] inline std::string_view luaG_typename(lua_State* const L_, StackIndex const idx_) 434[[nodiscard]]
435inline std::string_view luaG_typename(lua_State* const L_, StackIndex const idx_)
426{ 436{
427 return luaG_typename(L_, luaG_type(L_, idx_)); 437 return luaG_typename(L_, luaG_type(L_, idx_));
428} 438}