?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. #include <LuaBridge/detail/dump.h>
  9.  
  10. #include <map>
  11.  
  12. namespace luabridge {
  13.  
  14. template<class K, class V>
  15. struct Stack<std::map<K, V>>
  16. {
  17.     typedef std::map<K, V> Map;
  18.  
  19.     static void push(lua_State* L, const Map& map)
  20.     {
  21.         lua_createtable(L, 0, static_cast<int>(map.size()));
  22.         typedef typename Map::const_iterator ConstIter;
  23.         for (ConstIter i = map.begin(); i != map.end(); ++i)
  24.         {
  25.             Stack<K>::push(L, i->first);
  26.             Stack<V>::push(L, i->second);
  27.             lua_settable(L, -3);
  28.         }
  29.     }
  30.  
  31.     static Map get(lua_State* L, int index)
  32.     {
  33.         if (!lua_istable(L, index))
  34.         {
  35.             luaL_error(L, "#%d argument must be a table", index);
  36.         }
  37.  
  38.         Map map;
  39.         int const absindex = lua_absindex(L, index);
  40.         lua_pushnil(L);
  41.         while (lua_next(L, absindex) != 0)
  42.         {
  43.             map.emplace(Stack<K>::get(L, -2), Stack<V>::get(L, -1));
  44.             lua_pop(L, 1);
  45.         }
  46.         return map;
  47.     }
  48.  
  49.     static bool isInstance(lua_State* L, int index) { return lua_istable(L, index); }
  50. };
  51.  
  52. } // namespace luabridge
  53.