?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. // https://github.com/vinniefalco/LuaBridge
  2. //
  3. // Copyright 2020, Dmitry Tarakanov
  4. // SPDX-License-Identifier: MIT
  5.  
  6. #pragma once
  7.  
  8. #include <LuaBridge/detail/Stack.h>
  9.  
  10. #include <array>
  11.  
  12. namespace luabridge {
  13.  
  14. template<class T, std::size_t s>
  15. struct Stack<std::array<T, s>>
  16. {
  17.     static void push(lua_State* L, std::array<T, s> const& array)
  18.     {
  19.         lua_createtable(L, static_cast<int>(s), 0);
  20.         for (std::size_t i = 0; i < s; ++i)
  21.         {
  22.             lua_pushinteger(L, static_cast<lua_Integer>(i + 1));
  23.             Stack<T>::push(L, array[i]);
  24.             lua_settable(L, -3);
  25.         }
  26.     }
  27.  
  28.     static std::array<T, s> get(lua_State* L, int index)
  29.     {
  30.         if (!lua_istable(L, index))
  31.         {
  32.             luaL_error(L, "#%d argments must be table", index);
  33.         }
  34.  
  35.         std::size_t const tableSize = static_cast<std::size_t>(get_length(L, index));
  36.  
  37.         if (tableSize != s)
  38.         {
  39.             luaL_error(L, "array size should be %d ", s);
  40.         }
  41.  
  42.         std::array<T, s> array;
  43.  
  44.         int const absindex = lua_absindex(L, index);
  45.         lua_pushnil(L);
  46.         int arrayIndex = 0;
  47.         while (lua_next(L, absindex) != 0)
  48.         {
  49.             array[arrayIndex] = Stack<T>::get(L, -1);
  50.             lua_pop(L, 1);
  51.             ++arrayIndex;
  52.         }
  53.         return array;
  54.     }
  55. };
  56.  
  57. } // namespace luabridge
  58.