@@ -58,9 +58,10 @@ namespace lua {
5858
5959 template <size_t N>
6060 void push_value (char const (&str)[N]) const {
61- for (size_t len = N - 1 ; len > 0 ; len -= 1 ) {
62- if (str[len] != ' \0 ' ) {
63- lua_pushlstring (L, str, len + 1 );
61+ for (auto i = static_cast <intptr_t >(N - 1 ); i >= 0 ; i -= 1 ) {
62+ if (str[i] != ' \0 ' ) {
63+ lua_pushlstring (L, str, i + 1 );
64+ return ;
6465 }
6566 }
6667 lua_pushlstring (L, str, 0 );
@@ -71,12 +72,24 @@ namespace lua {
7172 if constexpr (std::is_same_v<T, std::nullopt_t >) {
7273 lua_pushnil (L);
7374 }
75+ else if constexpr (std::is_enum_v<T>) {
76+ push_value (static_cast <std::underlying_type_t <T>>(value));
77+ }
7478 else if constexpr (std::is_same_v<T, bool >) {
7579 lua_pushboolean (L, value);
7680 }
81+ else if constexpr (std::is_same_v<T, int8_t >) {
82+ lua_pushinteger (L, value);
83+ }
7784 else if constexpr (std::is_same_v<T, uint8_t >) {
7885 lua_pushinteger (L, value);
7986 }
87+ else if constexpr (std::is_same_v<T, int16_t >) {
88+ lua_pushinteger (L, value);
89+ }
90+ else if constexpr (std::is_same_v<T, uint16_t >) {
91+ lua_pushinteger (L, value);
92+ }
8093 else if constexpr (std::is_same_v<T, int32_t >) {
8194 lua_pushinteger (L, value);
8295 }
@@ -89,6 +102,14 @@ namespace lua {
89102 lua_pushinteger (L, static_cast <int32_t >(value));
90103 }
91104 }
105+ else if constexpr (std::is_same_v<T, int64_t >) {
106+ // WARN: not full supported
107+ lua_pushnumber (L, static_cast <double >(value));
108+ }
109+ else if constexpr (std::is_same_v<T, uint64_t >) {
110+ // WARN: not full supported
111+ lua_pushnumber (L, static_cast <double >(value));
112+ }
92113 else if constexpr (std::is_same_v<T, float >) {
93114 lua_pushnumber (L, value);
94115 }
@@ -148,68 +169,36 @@ namespace lua {
148169 template <>
149170 inline void set_array_value (stack_index_t index, std::string_view value) { lua_pushlstring (L, value.data (), value.size ()); lua_rawseti (L, -2 , index.value ); }
150171
151- void set_array_value (stack_index_t const array_index, int32_t const index, std::nullopt_t ) const { lua_pushnil (L); lua_rawseti (L, array_index.value , index); }
152- void set_array_value (stack_index_t const array_index, int32_t const index, stack_index_t const value_index) const { lua_pushvalue (L, value_index.value ); lua_rawseti (L, array_index.value , index); }
153- void set_array_value (stack_index_t const array_index, int32_t const index, bool const value) const { push_value (value); lua_rawseti (L, array_index.value , index); }
154- void set_array_value (stack_index_t const array_index, int32_t const index, int32_t const & value) const { push_value (value); lua_rawseti (L, array_index.value , index); }
155- void set_array_value (stack_index_t const array_index, int32_t const index, std::string_view const & value) const { push_value (value); lua_rawseti (L, array_index.value , index); }
156- void set_array_value (stack_index_t const array_index, int32_t const index, void * ptr) const { lua_pushlightuserdata (L, ptr); lua_rawseti (L, array_index.value , index); }
157-
158- inline size_t get_array_size (stack_index_t const index) const { return lua_objlen (L, index.value ); }
159-
160- inline void push_array_value_zero_base (stack_index_t array_index, size_t c_index) { lua_rawgeti (L, array_index.value , static_cast <int >(c_index + 1 )); }
161-
162- // C -> lua map
163-
164- inline stack_index_t create_map (size_t reserve = 0u ) { lua_createtable (L, 0 , static_cast <int >(reserve)); return index_of_top (); }
172+ // void set_array_value(stack_index_t const array_index, int32_t const index, std::nullopt_t) const { lua_pushnil(L); lua_rawseti(L, array_index.value, index); }
173+ // void set_array_value(stack_index_t const array_index, int32_t const index, stack_index_t const value_index) const { lua_pushvalue(L, value_index.value); lua_rawseti(L, array_index.value, index); }
174+ // void set_array_value(stack_index_t const array_index, int32_t const index, bool const value) const { push_value(value); lua_rawseti(L, array_index.value, index); }
175+ // void set_array_value(stack_index_t const array_index, int32_t const index, int32_t const value) const { push_value(value); lua_rawseti(L, array_index.value, index); }
176+ // void set_array_value(stack_index_t const array_index, int32_t const index, std::string_view const& value) const { push_value(value); lua_rawseti(L, array_index.value, index); }
177+ // void set_array_value(stack_index_t const array_index, int32_t const index, void* ptr) const { lua_pushlightuserdata(L, ptr); lua_rawseti(L, array_index.value, index); }
178+ // void set_array_value(stack_index_t const array_index, int32_t const index, float const value) const { lua_pushnumber(L, value); lua_rawseti(L, array_index.value, index); }
165179
166180 template <typename T>
167- inline void set_map_value (stack_index_t index, std::string_view key, T value) const { typename T::__invalid_type__ _{}; }
168-
169- template <>
170- inline void set_map_value (stack_index_t index, std::string_view key, int32_t value) const {
171- push_value (key);
181+ void set_array_value (stack_index_t const array_index, stack_index_t const index, T const & value) const {
182+ lua_pushinteger (L, index.value );
172183 push_value (value);
173- lua_settable (L, index .value );
184+ lua_settable (L, array_index .value );
174185 }
175-
176- template <>
177- inline void set_map_value (stack_index_t index, std::string_view key, uint32_t value) const {
178- push_value (key);
179- push_value (value);
180- lua_settable (L, index.value );
186+ void set_array_value (stack_index_t const array_index, stack_index_t const index, void * const ptr) const {
187+ lua_pushinteger (L, index.value );
188+ lua_pushlightuserdata (L, ptr);
189+ lua_settable (L, array_index.value );
181190 }
182191
183- template <>
184- inline void set_map_value (stack_index_t index, std::string_view key, float value) const {
185- push_value (key);
186- push_value (value);
187- lua_settable (L, index.value );
188- }
192+ inline size_t get_array_size (stack_index_t const index) const { return lua_objlen (L, index.value ); }
189193
190- template <>
191- inline void set_map_value (stack_index_t index, std::string_view key, double value) const {
192- push_value (key);
193- push_value (value);
194- lua_settable (L, index.value );
195- }
194+ inline void push_array_value_zero_base (stack_index_t array_index, size_t c_index) { lua_rawgeti (L, array_index.value , static_cast <int >(c_index + 1 )); }
196195
197- template <>
198- inline void set_map_value (stack_index_t index, std::string_view key, std::string_view value) const {
199- push_value (key);
200- push_value (value);
201- lua_settable (L, index.value );
202- }
196+ // C -> lua map
203197
204- template <>
205- inline void set_map_value (stack_index_t index, std::string_view key, stack_index_t value) const {
206- push_value (key);
207- push_value (value);
208- lua_settable (L, index.value );
209- }
198+ inline stack_index_t create_map (size_t reserve = 0u ) const { lua_createtable (L, 0 , static_cast <int >(reserve)); return index_of_top (); }
210199
211- template <>
212- inline void set_map_value (stack_index_t index, std::string_view key, lua_CFunction value) const {
200+ template <typename T >
201+ inline void set_map_value (stack_index_t const index, std::string_view const key, T const value) const {
213202 push_value (key);
214203 push_value (value);
215204 lua_settable (L, index.value );
@@ -244,15 +233,38 @@ namespace lua {
244233
245234 template <typename T>
246235 [[nodiscard]] T get_value (stack_index_t const index) const {
247- if constexpr (std::is_same_v<T, bool >) {
236+ if constexpr (std::is_enum_v<T>) {
237+ return static_cast <T>(get_value<std::underlying_type_t <T>>(index));
238+ }
239+ else if constexpr (std::is_same_v<T, bool >) {
248240 return lua_toboolean (L, index.value );
249241 }
242+ else if constexpr (std::is_same_v<T, int8_t >) {
243+ return static_cast <int8_t >(luaL_checkinteger (L, index.value ));
244+ }
245+ else if constexpr (std::is_same_v<T, uint8_t >) {
246+ return static_cast <uint8_t >(luaL_checkinteger (L, index.value ));
247+ }
248+ else if constexpr (std::is_same_v<T, int16_t >) {
249+ return static_cast <int16_t >(luaL_checkinteger (L, index.value ));
250+ }
251+ else if constexpr (std::is_same_v<T, uint16_t >) {
252+ return static_cast <uint16_t >(luaL_checkinteger (L, index.value ));
253+ }
250254 else if constexpr (std::is_same_v<T, int32_t >) {
251255 return static_cast <int32_t >(luaL_checkinteger (L, index.value ));
252256 }
253257 else if constexpr (std::is_same_v<T, uint32_t >) {
254258 return static_cast <uint32_t >(luaL_checknumber (L, index.value ));
255259 }
260+ else if constexpr (std::is_same_v<T, int64_t >) {
261+ // WARN: not full supported
262+ return static_cast <int64_t >(luaL_checknumber (L, index.value ));
263+ }
264+ else if constexpr (std::is_same_v<T, uint64_t >) {
265+ // WARN: not full supported
266+ return static_cast <uint64_t >(luaL_checknumber (L, index.value ));
267+ }
256268 else if constexpr (std::is_same_v<T, float >) {
257269 return static_cast <float >(luaL_checknumber (L, index.value ));
258270 }
@@ -279,61 +291,26 @@ namespace lua {
279291
280292 template <typename T>
281293 [[nodiscard]] T get_value (stack_index_t const index, T const & default_value) const {
282- if constexpr (std::is_same_v<T, bool >) {
283- if (has_value (index))
284- return lua_toboolean (L, index.value );
285- return default_value;
286- }
287- else if constexpr (std::is_same_v<T, int32_t >) {
288- return static_cast <int32_t >(luaL_optinteger (L, index.value , default_value));
289- }
290- else if constexpr (std::is_same_v<T, uint32_t >) {
291- return static_cast <uint32_t >(luaL_optnumber (L, index.value , static_cast <lua_Number>(default_value)));
292- }
293- else if constexpr (std::is_same_v<T, float >) {
294- if (has_value (index))
295- return static_cast <float >(luaL_checknumber (L, index.value ));
296- return default_value;
297- }
298- else if constexpr (std::is_same_v<T, double >) {
299- if (has_value (index))
300- return luaL_checknumber (L, index.value );
301- return default_value;
302- }
303- else {
304- static_assert (false , " FIXME" );
305- return {};
306- }
294+ if (!is_non_or_nil (index))
295+ return get_value<T>(index);
296+ return default_value;
307297 }
308298
309299 // array & map
310300
311301 template <typename T>
312- inline T get_array_value (stack_index_t array_index, stack_index_t lua_index) const { typename T::__invalid_type__ _{}; }
313-
314- template <>
315- inline stack_index_t get_array_value (stack_index_t array_index, stack_index_t lua_index) const {
316- lua_pushinteger (L, lua_index.value );
302+ inline T get_array_value (stack_index_t const array_index, stack_index_t const index) const {
303+ lua_pushinteger (L, index.value );
317304 lua_gettable (L, array_index.value );
318- return { lua_gettop (L) };
319- }
320-
321- template <>
322- inline int32_t get_array_value (stack_index_t array_index, stack_index_t lua_index) const {
323- lua_pushinteger (L, lua_index.value );
324- lua_gettable (L, array_index.value );
325- auto const result = static_cast <int32_t >(luaL_checkinteger (L, -1 ));
326- lua_pop (L, 1 );
305+ auto const result = get_value<T>(-1 );
306+ pop_value ();
327307 return result;
328308 }
329-
330309 template <>
331- inline uint32_t get_array_value (stack_index_t array_index, stack_index_t lua_index ) const {
332- lua_pushinteger (L, lua_index .value );
310+ inline stack_index_t get_array_value (stack_index_t const array_index, stack_index_t const index ) const {
311+ lua_pushinteger (L, index .value );
333312 lua_gettable (L, array_index.value );
334- auto const result = static_cast <uint32_t >(luaL_checknumber (L, -1 ));
335- lua_pop (L, 1 );
336- return result;
313+ return { lua_gettop (L) };
337314 }
338315
339316 template <typename T>
@@ -382,9 +359,13 @@ namespace lua {
382359 template <typename T>
383360 T* as_userdata (stack_index_t const index) const { return static_cast <T*>(luaL_checkudata (L, index.value , T::class_name.data ())); }
384361
362+ template <typename T>
363+ T* as_userdata (stack_index_t const index, std::string_view const class_name) const { return static_cast <T*>(luaL_checkudata (L, index.value , class_name.data ())); }
364+
385365 // type
386366
387367 [[nodiscard]] bool has_value (stack_index_t const index) const { return lua_type (L, index.value ) != LUA_TNONE ; }
368+ [[nodiscard]] bool is_non_or_nil (stack_index_t const index) const { auto const type = lua_type (L, index.value ); return type == LUA_TNONE || type == LUA_TNIL ; }
388369 [[nodiscard]] bool is_nil (stack_index_t const index) const { return lua_type (L, index.value ) == LUA_TNIL ; }
389370 [[nodiscard]] bool is_boolean (stack_index_t const index) const { return lua_type (L, index.value ) == LUA_TBOOLEAN ; }
390371 [[nodiscard]] bool is_number (stack_index_t const index) const { return lua_type (L, index.value ) == LUA_TNUMBER ; }
0 commit comments