diff options
Diffstat (limited to 'src/macros_and_utils.h')
-rw-r--r-- | src/macros_and_utils.h | 50 |
1 files changed, 37 insertions, 13 deletions
diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h index 31027d6..e8d5ab5 100644 --- a/src/macros_and_utils.h +++ b/src/macros_and_utils.h | |||
@@ -11,20 +11,25 @@ extern "C" { | |||
11 | #endif // __cplusplus | 11 | #endif // __cplusplus |
12 | 12 | ||
13 | #include <cassert> | 13 | #include <cassert> |
14 | #include <chrono> | ||
14 | #include <tuple> | 15 | #include <tuple> |
15 | #include <type_traits> | 16 | #include <type_traits> |
16 | 17 | ||
18 | using namespace std::chrono_literals; | ||
19 | |||
17 | #define USE_DEBUG_SPEW() 0 | 20 | #define USE_DEBUG_SPEW() 0 |
18 | #if USE_DEBUG_SPEW() | 21 | #if USE_DEBUG_SPEW() |
19 | extern char const* debugspew_indent; | 22 | extern char const* debugspew_indent; |
20 | #define INDENT_BEGIN "%.*s " | 23 | #define INDENT_BEGIN "%.*s " |
21 | #define INDENT_END , (U ? U->debugspew_indent_depth : 0), debugspew_indent | 24 | #define INDENT_END , (U ? U->debugspew_indent_depth.load(std::memory_order_relaxed) : 0), debugspew_indent |
22 | #define DEBUGSPEW_CODE(_code) _code | 25 | #define DEBUGSPEW_CODE(_code) _code |
23 | #define DEBUGSPEW_PARAM_COMMA( param_) param_, | 26 | #define DEBUGSPEW_OR_NOT(a_, b_) a_ |
27 | #define DEBUGSPEW_PARAM_COMMA(param_) param_, | ||
24 | #define DEBUGSPEW_COMMA_PARAM( param_) , param_ | 28 | #define DEBUGSPEW_COMMA_PARAM( param_) , param_ |
25 | #else // USE_DEBUG_SPEW() | 29 | #else // USE_DEBUG_SPEW() |
26 | #define DEBUGSPEW_CODE(_code) | 30 | #define DEBUGSPEW_CODE(_code) |
27 | #define DEBUGSPEW_PARAM_COMMA( param_) | 31 | #define DEBUGSPEW_OR_NOT(a_, b_) b_ |
32 | #define DEBUGSPEW_PARAM_COMMA(param_) | ||
28 | #define DEBUGSPEW_COMMA_PARAM( param_) | 33 | #define DEBUGSPEW_COMMA_PARAM( param_) |
29 | #endif // USE_DEBUG_SPEW() | 34 | #endif // USE_DEBUG_SPEW() |
30 | 35 | ||
@@ -41,8 +46,8 @@ extern char const* debugspew_indent; | |||
41 | 46 | ||
42 | #else // NDEBUG | 47 | #else // NDEBUG |
43 | 48 | ||
44 | #define _ASSERT_L( L, cond_) if( (cond_) == 0) { (void) luaL_error( L, "ASSERT failed: %s:%d '%s'", __FILE__, __LINE__, #cond_);} | 49 | #define _ASSERT_L(L, cond_) if( (cond_) == 0) { (void) luaL_error(L, "ASSERT failed: %s:%d '%s'", __FILE__, __LINE__, #cond_);} |
45 | #define STACK_DUMP( L) luaG_dump( L) | 50 | #define STACK_DUMP(L) luaG_dump(L) |
46 | 51 | ||
47 | class StackChecker | 52 | class StackChecker |
48 | { | 53 | { |
@@ -72,7 +77,7 @@ class StackChecker | |||
72 | if ((offset_ < 0) || (m_oldtop < 0)) | 77 | if ((offset_ < 0) || (m_oldtop < 0)) |
73 | { | 78 | { |
74 | assert(false); | 79 | assert(false); |
75 | std::ignore = luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%d", lua_gettop(m_L), offset_, file_, line_); | 80 | luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%d", lua_gettop(m_L), offset_, file_, line_); // doesn't return |
76 | } | 81 | } |
77 | } | 82 | } |
78 | 83 | ||
@@ -83,7 +88,7 @@ class StackChecker | |||
83 | if (lua_gettop(m_L) != pos_) | 88 | if (lua_gettop(m_L) != pos_) |
84 | { | 89 | { |
85 | assert(false); | 90 | assert(false); |
86 | std::ignore = luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%d", lua_gettop(m_L), pos_, file_, line_); | 91 | luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%d", lua_gettop(m_L), pos_, file_, line_); // doesn't return |
87 | } | 92 | } |
88 | } | 93 | } |
89 | 94 | ||
@@ -103,7 +108,7 @@ class StackChecker | |||
103 | if (actual != expected_) | 108 | if (actual != expected_) |
104 | { | 109 | { |
105 | assert(false); | 110 | assert(false); |
106 | std::ignore = luaL_error(m_L, "STACK ASSERT failed (%d not %d): %s:%d", actual, expected_, file_, line_); | 111 | luaL_error(m_L, "STACK ASSERT failed (%d not %d): %s:%d", actual, expected_, file_, line_); // doesn't return |
107 | } | 112 | } |
108 | } | 113 | } |
109 | } | 114 | } |
@@ -123,24 +128,24 @@ inline void STACK_GROW(lua_State* L, int n_) | |||
123 | { | 128 | { |
124 | if (!lua_checkstack(L, n_)) | 129 | if (!lua_checkstack(L, n_)) |
125 | { | 130 | { |
126 | std::ignore = luaL_error(L, "Cannot grow stack!"); | 131 | luaL_error(L, "Cannot grow stack!"); // doesn't return |
127 | } | 132 | } |
128 | } | 133 | } |
129 | 134 | ||
130 | #define LUAG_FUNC( func_name) int LG_##func_name( lua_State* L) | 135 | #define LUAG_FUNC(func_name) [[nodiscard]] int LG_##func_name(lua_State* L) |
131 | 136 | ||
132 | // ################################################################################################# | 137 | // ################################################################################################# |
133 | 138 | ||
134 | // a small helper to extract a full userdata pointer from the stack in a safe way | 139 | // a small helper to extract a full userdata pointer from the stack in a safe way |
135 | template<typename T> | 140 | template<typename T> |
136 | T* lua_tofulluserdata(lua_State* L, int index_) | 141 | [[nodiscard]] T* lua_tofulluserdata(lua_State* L, int index_) |
137 | { | 142 | { |
138 | ASSERT_L(lua_isnil(L, index_) || lua_type(L, index_) == LUA_TUSERDATA); | 143 | ASSERT_L(lua_isnil(L, index_) || lua_type(L, index_) == LUA_TUSERDATA); |
139 | return static_cast<T*>(lua_touserdata(L, index_)); | 144 | return static_cast<T*>(lua_touserdata(L, index_)); |
140 | } | 145 | } |
141 | 146 | ||
142 | template<typename T> | 147 | template<typename T> |
143 | auto lua_tolightuserdata(lua_State* L, int index_) | 148 | [[nodiscard]] auto lua_tolightuserdata(lua_State* L, int index_) |
144 | { | 149 | { |
145 | ASSERT_L(lua_isnil(L, index_) || lua_islightuserdata(L, index_)); | 150 | ASSERT_L(lua_isnil(L, index_) || lua_islightuserdata(L, index_)); |
146 | if constexpr (std::is_pointer_v<T>) | 151 | if constexpr (std::is_pointer_v<T>) |
@@ -154,7 +159,7 @@ auto lua_tolightuserdata(lua_State* L, int index_) | |||
154 | } | 159 | } |
155 | 160 | ||
156 | template <typename T> | 161 | template <typename T> |
157 | T* lua_newuserdatauv(lua_State* L, int nuvalue_) | 162 | [[nodiscard]] T* lua_newuserdatauv(lua_State* L, int nuvalue_) |
158 | { | 163 | { |
159 | return static_cast<T*>(lua_newuserdatauv(L, sizeof(T), nuvalue_)); | 164 | return static_cast<T*>(lua_newuserdatauv(L, sizeof(T), nuvalue_)); |
160 | } | 165 | } |
@@ -167,3 +172,22 @@ T* lua_newuserdatauv(lua_State* L, int nuvalue_) | |||
167 | std::ignore = lua_error(L); // doesn't return | 172 | std::ignore = lua_error(L); // doesn't return |
168 | assert(false); // we should never get here, but i'm paranoid | 173 | assert(false); // we should never get here, but i'm paranoid |
169 | } | 174 | } |
175 | |||
176 | using lua_Duration = std::chrono::template duration<lua_Number>; | ||
177 | |||
178 | // ################################################################################################# | ||
179 | |||
180 | // A unique type generator | ||
181 | template <typename T, auto = []{}> | ||
182 | struct Unique | ||
183 | { | ||
184 | T m_val; | ||
185 | constexpr Unique() = default; | ||
186 | constexpr operator T() const { return m_val; } | ||
187 | constexpr explicit Unique(T b_) : m_val{ b_ } {} | ||
188 | }; | ||
189 | |||
190 | // ################################################################################################# | ||
191 | |||
192 | using Source = Unique<lua_State*>; | ||
193 | using Dest = Unique<lua_State*>; \ No newline at end of file | ||