?login_element?

Subversion Repositories NedoOS

Rev

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

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