Lua binding for std::map<>::at()
authorRobin Gareus <robin@gareus.org>
Fri, 7 Oct 2016 01:39:22 +0000 (03:39 +0200)
committerRobin Gareus <robin@gareus.org>
Fri, 7 Oct 2016 01:39:22 +0000 (03:39 +0200)
libs/lua/LuaBridge/detail/CFunctions.h
libs/lua/LuaBridge/detail/Namespace.h

index 824bdd7e963c8a52dd76cce5c75039dbda8b647b..2f1c3f635b4e2fd972d5a716dbc76df89f8f4316 100644 (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
index bb05751455cd2f87a1e8d058b1d86de1fd348e74..167066036767bd55b4f80e10a2efeb9766fa9d56 100644 (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>