?login_element?

Subversion Repositories NedoOS

Rev

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

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