Wrapping boost::shared_ptr in PHP using SWIG
If I use SWIG to wrap this C++ function:
boost::shared_ptr<Client> Client::create() {
return boost::shared_ptr<Client>(new Client());
}
And then call it in PHP:
$client = Client::create();
echo gettype($client);
The type of $client
is resource
, not object
, and so I cannot call Client
methods.
What are my options for wrapping this function? I'm creating a PHP wrapper for someone else's C++ library, so reworking the code to not use boost::shared_ptr
isn't really an op开发者_Python百科tion.
This is the only solution I've come up with so far:
MyClient Client::createObject() {
return *Client::create();
}
And calling it in PHP:
$client = Client::createObject();
echo gettype($client);
Now the type of $client
is object
and I can call Client
methods on it. Is this a reasonable workaround? If not, what should I be doing?
You should take a look to http://www.swig.org/Doc2.0/SWIGPlus.html#SWIGPlus_smart_pointers ;)
精彩评论