diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-03-27 10:37:05 +0100 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-03-27 10:37:05 +0100 |
commit | 9e3ca50cfafa0d7dc3e15f3b6a635aef6a938b80 (patch) | |
tree | 0b8dcb8cf1d90fedca7234e8a0179a09514a12bc /src/macros_and_utils.h | |
parent | f214d35df5f09e7026dafd8553e83cc6ea21cbde (diff) | |
download | lanes-9e3ca50cfafa0d7dc3e15f3b6a635aef6a938b80.tar.gz lanes-9e3ca50cfafa0d7dc3e15f3b6a635aef6a938b80.tar.bz2 lanes-9e3ca50cfafa0d7dc3e15f3b6a635aef6a938b80.zip |
C++ migration: new helper templates lua_tolightuserdata and lua_tofulluserdata
Diffstat (limited to 'src/macros_and_utils.h')
-rw-r--r-- | src/macros_and_utils.h | 20 |
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 |
129 | template<typename T> | 130 | template<typename T> |
130 | T* lua_touserdata(lua_State* L, int index_) | 131 | T* 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 | |||
137 | template<typename T> | ||
138 | auto 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 | } | ||