aboutsummaryrefslogtreecommitdiff
path: root/src/macros_and_utils.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/macros_and_utils.h20
1 files changed, 18 insertions, 2 deletions
diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h
index 370cbed..23fa0e9 100644
--- a/src/macros_and_utils.h
+++ b/src/macros_and_utils.h
@@ -11,6 +11,7 @@ extern "C" {
11#endif // __cplusplus 11#endif // __cplusplus
12 12
13#include <cassert> 13#include <cassert>
14#include <type_traits>
14 15
15#define USE_DEBUG_SPEW() 0 16#define USE_DEBUG_SPEW() 0
16#if USE_DEBUG_SPEW() 17#if USE_DEBUG_SPEW()
@@ -125,9 +126,24 @@ inline void STACK_GROW(lua_State* L, int n_)
125 126
126#define LUAG_FUNC( func_name) int LG_##func_name( lua_State* L) 127#define LUAG_FUNC( func_name) int LG_##func_name( lua_State* L)
127 128
128// a small helper to extract a userdata pointer from the stack 129// a small helper to extract a full userdata pointer from the stack in a safe way
129template<typename T> 130template<typename T>
130T* lua_touserdata(lua_State* L, int index_) 131T* lua_tofulluserdata(lua_State* L, int index_)
131{ 132{
133 ASSERT_L(lua_isnil(L, index_) || lua_type(L, index_) == LUA_TUSERDATA);
132 return static_cast<T*>(lua_touserdata(L, index_)); 134 return static_cast<T*>(lua_touserdata(L, index_));
133} 135}
136
137template<typename T>
138auto lua_tolightuserdata(lua_State* L, int index_)
139{
140 ASSERT_L(lua_isnil(L, index_) || lua_islightuserdata(L, index_));
141 if constexpr (std::is_pointer_v<T>)
142 {
143 return static_cast<T>(lua_touserdata(L, index_));
144 }
145 else
146 {
147 return static_cast<T*>(lua_touserdata(L, index_));
148 }
149}