Returning tables of objects from C++, adopt policy

1.1k views Asked by At

Using luabind, I create a table of objects from C++

luabind::object create_table(lua_State *L)
{
  luabind::object result = luabind::newtable(L);
  int index = 1;
  for ( ... ) {
    lua_Object *o = new lua_Object( ... );
    result[ index ++ ] = o;
  }
  return result;
}

I register the function as

module(L)
[
  def("create_table", &create_table)
]

and lua_Object as

class_<lua_Object> reg("Object");
reg
  .def(constructor<float,float>())
  ;
module(L) [ reg ];

How can I tell luabind to take ownership of the objects stored in the table ( new lua_Object( ... ) )? What would be a work around?

Thanks -

1

There are 1 answers

1
sbk On

Replace

result[ index ++ ] = o

with

result[ index ++ ] = luabind::object(L, o, luabind::adopt(luabind::result));

On a side note, don't you have to register create_table with raw(_1) policy?