Why do you want the class pointer in the lua code? As a C++ class, it's going to be opaque... or better be. ~smile~
Perhaps set up a std::map in the C++ code and store the pointer in the map with a hash value and pass the hash value to the lua? The lua can then use that to pass back to the C++ code elsewhere.
EDIT: You could dereference P
a bit and pass around a hash as a substitute for the this
in P
.
Keep in mind that thing:Method()
is just a shorthand for thing.Method( thing )
-- so, using a hash for thing
is still largely the same construct, but a little less OO in appearance to the eye.
Something similar to this would work...
std::map<unsigned,P*> p_Map;
void p_AddValue( unsigned hash, int aValue )
{
if( p_Map.find( hash ) != p_Map.end() )
p_Map[hash]->AddValue( aValue );
}
unsigned p_New()
{
P* p = new P();
unsigned hash;
do hash = GenerateHash( p );
while( p_Map.find( hash ) != p_Map.end() );
p_Map[hash] = p;
return hash;
}
module(L)
[
def("p_AddValue", &p_AddValue)
def("p_New", &p_New)
]
Then in Lua you should be able to do things like this...
local p1 = p_New();
local p2 = p_New();
p_AddValue( p1, 5 );
p_AddValue( p2, 10 );
etc.
It's not a perfect solution, but it should get you around the issue you're having. Hopefully someone else maybe can come along with a better answer?
RE-EDIT: come to think of it, though a tad cumbersome, there might be another way that would allow you to use the P class (indirectly) via a proxy class in Lua...
class PProxy
{
protected:
P p;
public:
PProxy() : P() {};
~PProxy() {};
void AddValue( int aValue ) { p.AddValue( aValue ); }
}
module(L)
[
class_<PProxy>("P")
.def(constructor<>())
.def("AddValue", &PProxy::AddValue)
]