diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-04-29 09:50:57 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-04-29 09:50:57 +0200 |
commit | 07d6074b5deadc7ff6d30d5fb47fb6fa863a14ff (patch) | |
tree | 955fb6216739c3d55aeba47a3ea5d8ffc6a7f3e1 /src/macros_and_utils.h | |
parent | 4a2705af8a9a6b55cf848d53f8330447138a19c4 (diff) | |
download | lanes-07d6074b5deadc7ff6d30d5fb47fb6fa863a14ff.tar.gz lanes-07d6074b5deadc7ff6d30d5fb47fb6fa863a14ff.tar.bz2 lanes-07d6074b5deadc7ff6d30d5fb47fb6fa863a14ff.zip |
All headers reformatted with updated clang-format settings
Diffstat (limited to 'src/macros_and_utils.h')
-rw-r--r-- | src/macros_and_utils.h | 88 |
1 files changed, 51 insertions, 37 deletions
diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h index 9bc71e1..be331a1 100644 --- a/src/macros_and_utils.h +++ b/src/macros_and_utils.h | |||
@@ -1,7 +1,8 @@ | |||
1 | #pragma once | 1 | #pragma once |
2 | 2 | ||
3 | #ifdef __cplusplus | 3 | #ifdef __cplusplus |
4 | extern "C" { | 4 | extern "C" |
5 | { | ||
5 | #endif // __cplusplus | 6 | #endif // __cplusplus |
6 | #include "lua.h" | 7 | #include "lua.h" |
7 | #include "lualib.h" | 8 | #include "lualib.h" |
@@ -65,17 +66,17 @@ template <typename... ARGS> | |||
65 | #define DEBUGSPEW_CODE(_code) _code | 66 | #define DEBUGSPEW_CODE(_code) _code |
66 | #define DEBUGSPEW_OR_NOT(a_, b_) a_ | 67 | #define DEBUGSPEW_OR_NOT(a_, b_) a_ |
67 | #define DEBUGSPEW_PARAM_COMMA(param_) param_, | 68 | #define DEBUGSPEW_PARAM_COMMA(param_) param_, |
68 | #define DEBUGSPEW_COMMA_PARAM( param_) , param_ | 69 | #define DEBUGSPEW_COMMA_PARAM(param_) , param_ |
69 | #else // USE_DEBUG_SPEW() | 70 | #else // USE_DEBUG_SPEW() |
70 | #define DEBUGSPEW_CODE(_code) | 71 | #define DEBUGSPEW_CODE(_code) |
71 | #define DEBUGSPEW_OR_NOT(a_, b_) b_ | 72 | #define DEBUGSPEW_OR_NOT(a_, b_) b_ |
72 | #define DEBUGSPEW_PARAM_COMMA(param_) | 73 | #define DEBUGSPEW_PARAM_COMMA(param_) |
73 | #define DEBUGSPEW_COMMA_PARAM( param_) | 74 | #define DEBUGSPEW_COMMA_PARAM(param_) |
74 | #endif // USE_DEBUG_SPEW() | 75 | #endif // USE_DEBUG_SPEW() |
75 | 76 | ||
76 | #ifdef NDEBUG | 77 | #ifdef NDEBUG |
77 | 78 | ||
78 | #define LUA_ASSERT(L,c) ; //nothing | 79 | #define LUA_ASSERT(L, c) ; // nothing |
79 | 80 | ||
80 | #define STACK_CHECK_START_REL(L, offset_) | 81 | #define STACK_CHECK_START_REL(L, offset_) |
81 | #define STACK_CHECK_START_ABS(L, offset_) | 82 | #define STACK_CHECK_START_ABS(L, offset_) |
@@ -87,8 +88,7 @@ template <typename... ARGS> | |||
87 | 88 | ||
88 | inline void LUA_ASSERT_IMPL(lua_State* L_, bool cond_, char const* file_, size_t const line_, char const* txt_) | 89 | inline void LUA_ASSERT_IMPL(lua_State* L_, bool cond_, char const* file_, size_t const line_, char const* txt_) |
89 | { | 90 | { |
90 | if (!cond_) | 91 | if (!cond_) { |
91 | { | ||
92 | raise_luaL_error(L_, "LUA_ASSERT %s:%llu '%s'", file_, line_, txt_); | 92 | raise_luaL_error(L_, "LUA_ASSERT %s:%llu '%s'", file_, line_, txt_); |
93 | } | 93 | } |
94 | } | 94 | } |
@@ -120,8 +120,7 @@ class StackChecker | |||
120 | : m_L{ L_ } | 120 | : m_L{ L_ } |
121 | , m_oldtop{ lua_gettop(L_) - offset_ } | 121 | , m_oldtop{ lua_gettop(L_) - offset_ } |
122 | { | 122 | { |
123 | if ((offset_ < 0) || (m_oldtop < 0)) | 123 | if ((offset_ < 0) || (m_oldtop < 0)) { |
124 | { | ||
125 | assert(false); | 124 | assert(false); |
126 | raise_luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(m_L), offset_, file_, line_); | 125 | raise_luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(m_L), offset_, file_, line_); |
127 | } | 126 | } |
@@ -131,8 +130,7 @@ class StackChecker | |||
131 | : m_L{ L_ } | 130 | : m_L{ L_ } |
132 | , m_oldtop{ 0 } | 131 | , m_oldtop{ 0 } |
133 | { | 132 | { |
134 | if (lua_gettop(m_L) != pos_) | 133 | if (lua_gettop(m_L) != pos_) { |
135 | { | ||
136 | assert(false); | 134 | assert(false); |
137 | raise_luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(m_L), pos_, file_, line_); | 135 | raise_luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(m_L), pos_, file_, line_); |
138 | } | 136 | } |
@@ -148,11 +146,9 @@ class StackChecker | |||
148 | // verify if the distance between the current top and the initial one is what we expect | 146 | // verify if the distance between the current top and the initial one is what we expect |
149 | void check(int expected_, char const* file_, size_t const line_) | 147 | void check(int expected_, char const* file_, size_t const line_) |
150 | { | 148 | { |
151 | if (expected_ != LUA_MULTRET) | 149 | if (expected_ != LUA_MULTRET) { |
152 | { | ||
153 | int const actual{ lua_gettop(m_L) - m_oldtop }; | 150 | int const actual{ lua_gettop(m_L) - m_oldtop }; |
154 | if (actual != expected_) | 151 | if (actual != expected_) { |
155 | { | ||
156 | assert(false); | 152 | assert(false); |
157 | raise_luaL_error(m_L, "STACK ASSERT failed (%d not %d): %s:%llu", actual, expected_, file_, line_); | 153 | raise_luaL_error(m_L, "STACK ASSERT failed (%d not %d): %s:%llu", actual, expected_, file_, line_); |
158 | } | 154 | } |
@@ -160,10 +156,26 @@ class StackChecker | |||
160 | } | 156 | } |
161 | }; | 157 | }; |
162 | 158 | ||
163 | #define STACK_CHECK_START_REL(L, offset_) StackChecker stackChecker_##L{L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__} | 159 | #define STACK_CHECK_START_REL(L, offset_) \ |
164 | #define STACK_CHECK_START_ABS(L, offset_) StackChecker stackChecker_##L{L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__} | 160 | StackChecker stackChecker_##L \ |
165 | #define STACK_CHECK_RESET_REL(L, offset_) stackChecker_##L = StackChecker{L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__} | 161 | { \ |
166 | #define STACK_CHECK_RESET_ABS(L, offset_) stackChecker_##L = StackChecker{L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__} | 162 | L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \ |
163 | } | ||
164 | #define STACK_CHECK_START_ABS(L, offset_) \ | ||
165 | StackChecker stackChecker_##L \ | ||
166 | { \ | ||
167 | L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \ | ||
168 | } | ||
169 | #define STACK_CHECK_RESET_REL(L, offset_) \ | ||
170 | stackChecker_##L = StackChecker \ | ||
171 | { \ | ||
172 | L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \ | ||
173 | } | ||
174 | #define STACK_CHECK_RESET_ABS(L, offset_) \ | ||
175 | stackChecker_##L = StackChecker \ | ||
176 | { \ | ||
177 | L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \ | ||
178 | } | ||
167 | #define STACK_CHECK(L, offset_) stackChecker_##L.check(offset_, __FILE__, __LINE__) | 179 | #define STACK_CHECK(L, offset_) stackChecker_##L.check(offset_, __FILE__, __LINE__) |
168 | 180 | ||
169 | #endif // NDEBUG | 181 | #endif // NDEBUG |
@@ -172,8 +184,7 @@ class StackChecker | |||
172 | 184 | ||
173 | inline void STACK_GROW(lua_State* L, int n_) | 185 | inline void STACK_GROW(lua_State* L, int n_) |
174 | { | 186 | { |
175 | if (!lua_checkstack(L, n_)) | 187 | if (!lua_checkstack(L, n_)) { |
176 | { | ||
177 | raise_luaL_error(L, "Cannot grow stack!"); | 188 | raise_luaL_error(L, "Cannot grow stack!"); |
178 | } | 189 | } |
179 | } | 190 | } |
@@ -185,23 +196,20 @@ inline void STACK_GROW(lua_State* L, int n_) | |||
185 | // ################################################################################################# | 196 | // ################################################################################################# |
186 | 197 | ||
187 | // a small helper to extract a full userdata pointer from the stack in a safe way | 198 | // a small helper to extract a full userdata pointer from the stack in a safe way |
188 | template<typename T> | 199 | template <typename T> |
189 | [[nodiscard]] T* lua_tofulluserdata(lua_State* L, int index_) | 200 | [[nodiscard]] T* lua_tofulluserdata(lua_State* L, int index_) |
190 | { | 201 | { |
191 | LUA_ASSERT(L, lua_isnil(L, index_) || lua_type(L, index_) == LUA_TUSERDATA); | 202 | LUA_ASSERT(L, lua_isnil(L, index_) || lua_type(L, index_) == LUA_TUSERDATA); |
192 | return static_cast<T*>(lua_touserdata(L, index_)); | 203 | return static_cast<T*>(lua_touserdata(L, index_)); |
193 | } | 204 | } |
194 | 205 | ||
195 | template<typename T> | 206 | template <typename T> |
196 | [[nodiscard]] auto lua_tolightuserdata(lua_State* L, int index_) | 207 | [[nodiscard]] auto lua_tolightuserdata(lua_State* L, int index_) |
197 | { | 208 | { |
198 | LUA_ASSERT(L, lua_isnil(L, index_) || lua_islightuserdata(L, index_)); | 209 | LUA_ASSERT(L, lua_isnil(L, index_) || lua_islightuserdata(L, index_)); |
199 | if constexpr (std::is_pointer_v<T>) | 210 | if constexpr (std::is_pointer_v<T>) { |
200 | { | ||
201 | return static_cast<T>(lua_touserdata(L, index_)); | 211 | return static_cast<T>(lua_touserdata(L, index_)); |
202 | } | 212 | } else { |
203 | else | ||
204 | { | ||
205 | return static_cast<T*>(lua_touserdata(L, index_)); | 213 | return static_cast<T*>(lua_touserdata(L, index_)); |
206 | } | 214 | } |
207 | } | 215 | } |
@@ -223,23 +231,27 @@ template <typename T, auto = [] {}, typename specialization = void> | |||
223 | class Unique | 231 | class Unique |
224 | { | 232 | { |
225 | private: | 233 | private: |
226 | |||
227 | T m_val; | 234 | T m_val; |
228 | 235 | ||
229 | public: | 236 | public: |
230 | |||
231 | Unique() = default; | 237 | Unique() = default; |
232 | operator T() const { return m_val; } | 238 | operator T() const { return m_val; } |
233 | explicit Unique(T b_) : m_val{ b_ } {} | 239 | explicit Unique(T b_) |
240 | : m_val{ b_ } | ||
241 | { | ||
242 | } | ||
234 | }; | 243 | }; |
235 | 244 | ||
236 | template <typename T, auto lambda> | 245 | template <typename T, auto lambda> |
237 | class Unique<T, lambda, std::enable_if_t<!std::is_scalar_v<T>>> : public T | 246 | class Unique<T, lambda, std::enable_if_t<!std::is_scalar_v<T>>> |
247 | : public T | ||
238 | { | 248 | { |
239 | public: | 249 | public: |
240 | |||
241 | using T::T; | 250 | using T::T; |
242 | explicit Unique(T const& b_) : T{ b_ } {} | 251 | explicit Unique(T const& b_) |
252 | : T{ b_ } | ||
253 | { | ||
254 | } | ||
243 | }; | 255 | }; |
244 | 256 | ||
245 | // ################################################################################################# | 257 | // ################################################################################################# |
@@ -252,14 +264,16 @@ using DestState = Unique<lua_State*>; | |||
252 | // A helper to issue an error if the provided optional doesn't contain a value | 264 | // A helper to issue an error if the provided optional doesn't contain a value |
253 | // we can't use std::optional::value_or(luaL_error(...)), because the 'or' value is always evaluated | 265 | // we can't use std::optional::value_or(luaL_error(...)), because the 'or' value is always evaluated |
254 | template <typename T> | 266 | template <typename T> |
255 | concept IsOptional = requires(T x){ x.value_or(T{}); }; | 267 | concept IsOptional = requires(T x) |
268 | { | ||
269 | x.value_or(T{}); | ||
270 | }; | ||
256 | 271 | ||
257 | template<typename T, typename ...Ts> | 272 | template <typename T, typename... Ts> |
258 | requires IsOptional<T> | 273 | requires IsOptional<T> |
259 | typename T::value_type const& OptionalValue(T const& x_, Ts... args_) | 274 | typename T::value_type const& OptionalValue(T const& x_, Ts... args_) |
260 | { | 275 | { |
261 | if (!x_.has_value()) | 276 | if (!x_.has_value()) { |
262 | { | ||
263 | raise_luaL_error(std::forward<Ts>(args_)...); | 277 | raise_luaL_error(std::forward<Ts>(args_)...); |
264 | } | 278 | } |
265 | return x_.value(); | 279 | return x_.value(); |