Lua binding for std::map<>::at()

This commit is contained in:
Robin Gareus 2016-10-07 03:39:22 +02:00
parent bf6b8d76f4
commit 86bfe4eff6
2 changed files with 19 additions and 1 deletions

View File

@ -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

View File

@ -1749,7 +1749,8 @@ public:
.addFunction ("count", (T_SIZE (LT::*)(const K&) const)&LT::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>