13
0

Make Lua std::set bindings more generic, (prepare for multiset etc)

This commit is contained in:
Robin Gareus 2017-03-18 14:34:02 +01:00
parent a1116ebd6f
commit bc300ddab7
2 changed files with 7 additions and 12 deletions

View File

@ -1331,10 +1331,9 @@ struct CFunc
// generate std::set from table keys ( table[member] = true ) // generate std::set from table keys ( table[member] = true )
// http://www.lua.org/pil/11.5.html // http://www.lua.org/pil/11.5.html
template <class T> template <class T, class C>
static int tableToSet (lua_State *L) static int tableToSet (lua_State *L)
{ {
typedef std::set<T> C;
C * const t = Userdata::get <C> (L, 1, true); C * const t = Userdata::get <C> (L, 1, true);
if (!t) { return luaL_error (L, "invalid pointer to std::set"); } if (!t) { return luaL_error (L, "invalid pointer to std::set"); }
if (!lua_istable (L, -1)) { return luaL_error (L, "argument is not a table"); } if (!lua_istable (L, -1)) { return luaL_error (L, "argument is not a table"); }
@ -1358,10 +1357,9 @@ struct CFunc
// iterate over a std::set, explicit "true" value. // iterate over a std::set, explicit "true" value.
// compare to http://www.lua.org/pil/11.5.html // compare to http://www.lua.org/pil/11.5.html
template <class T> template <class T, class C>
static int setIterIter (lua_State *L) static int setIterIter (lua_State *L)
{ {
typedef std::set<T> C;
typedef typename C::const_iterator IterType; typedef typename C::const_iterator IterType;
IterType * const end = static_cast <IterType * const> (lua_touserdata (L, lua_upvalueindex (2))); IterType * const end = static_cast <IterType * const> (lua_touserdata (L, lua_upvalueindex (2)));
IterType * const iter = static_cast <IterType * const> (lua_touserdata (L, lua_upvalueindex (1))); IterType * const iter = static_cast <IterType * const> (lua_touserdata (L, lua_upvalueindex (1)));
@ -1377,24 +1375,22 @@ struct CFunc
} }
// generate iterator // generate iterator
template <class T> template <class T, class C>
static int setIter (lua_State *L) static int setIter (lua_State *L)
{ {
typedef std::set<T> C;
C * const t = Userdata::get <C> (L, 1, false); C * const t = Userdata::get <C> (L, 1, false);
if (!t) { return luaL_error (L, "invalid pointer to std::set"); } if (!t) { return luaL_error (L, "invalid pointer to std::set"); }
typedef typename C::const_iterator IterType; typedef typename C::const_iterator IterType;
new (lua_newuserdata (L, sizeof (IterType*))) IterType (t->begin()); new (lua_newuserdata (L, sizeof (IterType*))) IterType (t->begin());
new (lua_newuserdata (L, sizeof (IterType*))) IterType (t->end()); new (lua_newuserdata (L, sizeof (IterType*))) IterType (t->end());
lua_pushcclosure (L, setIterIter<T>, 2); lua_pushcclosure (L, setIterIter<T, C>, 2);
return 1; return 1;
} }
// generate table from std::set // generate table from std::set
template <class T> template <class T, class C>
static int setToTable (lua_State *L) static int setToTable (lua_State *L)
{ {
typedef std::set<T> C;
C const* const t = Userdata::get <C> (L, 1, true); C const* const t = Userdata::get <C> (L, 1, true);
if (!t) { return luaL_error (L, "invalid pointer to std::set"); } if (!t) { return luaL_error (L, "invalid pointer to std::set"); }

View File

@ -1824,9 +1824,8 @@ public:
.addFunction ("clear", (void (LT::*)())&LT::clear) .addFunction ("clear", (void (LT::*)())&LT::clear)
.addFunction ("empty", &LT::empty) .addFunction ("empty", &LT::empty)
.addFunction ("size", &LT::size) .addFunction ("size", &LT::size)
.addExtCFunction ("add", &CFunc::tableToSet<T>) .addExtCFunction ("iter", &CFunc::setIter<T, LT>)
.addExtCFunction ("iter", &CFunc::setIter<T>) .addExtCFunction ("table", &CFunc::setToTable<T, LT>);
.addExtCFunction ("table", &CFunc::setToTable<T>);
} }
template <unsigned int T> template <unsigned int T>