diff options
author | Robin Gareus <robin@gareus.org> | 2016-10-07 03:39:22 +0200 |
---|---|---|
committer | Robin Gareus <robin@gareus.org> | 2016-10-07 03:39:22 +0200 |
commit | 86bfe4eff63abd24073d7c2b291eb3667b3eab91 (patch) | |
tree | 52b61f73852cfc805b82e9130c2adba6b44ecd99 /libs/lua | |
parent | bf6b8d76f454af73f952655a6f2998198b8cabf3 (diff) |
Lua binding for std::map<>::at()
Diffstat (limited to 'libs/lua')
-rw-r--r-- | libs/lua/LuaBridge/detail/CFunctions.h | 17 | ||||
-rw-r--r-- | libs/lua/LuaBridge/detail/Namespace.h | 3 |
2 files changed, 19 insertions, 1 deletions
diff --git a/libs/lua/LuaBridge/detail/CFunctions.h b/libs/lua/LuaBridge/detail/CFunctions.h index 824bdd7e96..2f1c3f635b 100644 --- a/libs/lua/LuaBridge/detail/CFunctions.h +++ b/libs/lua/LuaBridge/detail/CFunctions.h @@ -1293,6 +1293,23 @@ struct CFunc return 1; } + // generate table from std::map + template <class K, class V> + static int mapAt (lua_State *L) + { + typedef std::map<K, V> C; + C const* const t = Userdata::get <C> (L, 1, true); + if (!t) { return luaL_error (L, "invalid pointer to std::map"); } + K const key = Stack<K>::get (L, 2); + typename C::const_iterator iter = t->find(key); + if (iter == t->end()) { + return 0; + } + Stack <V>::push (L, (*iter).second); + return 1; + } + + //-------------------------------------------------------------------------- // generate std::set from table keys ( table[member] = true ) // http://www.lua.org/pil/11.5.html diff --git a/libs/lua/LuaBridge/detail/Namespace.h b/libs/lua/LuaBridge/detail/Namespace.h index bb05751455..1670660367 100644 --- a/libs/lua/LuaBridge/detail/Namespace.h +++ b/libs/lua/LuaBridge/detail/Namespace.h @@ -1749,7 +1749,8 @@ public: .addFunction ("count", (T_SIZE (LT::*)(const K&) const)<::count) .addExtCFunction ("add", &CFunc::tableToMap<K, V>) .addExtCFunction ("iter", &CFunc::mapIter<K, V>) - .addExtCFunction ("table", &CFunc::mapToTable<K, V>); + .addExtCFunction ("table", &CFunc::mapToTable<K, V>) + .addExtCFunction ("at", &CFunc::mapAt<K, V>); } template <class T> |