使用 SWIG 在 PHP 中包装 boost::shared_ptr

发布于 2024-09-11 07:45:55 字数 820 浏览 5 评论 0原文

如果我使用 SWIG 来包装这个 C++ 函数:

boost::shared_ptr<Client> Client::create() {
    return boost::shared_ptr<Client>(new Client());
}

然后在 PHP 中调用它:

$client = Client::create();
echo gettype($client);

$client 的类型是 resource,而不是 object,并且所以我无法调用 Client 方法。

包装这个函数有哪些选择?我正在为其他人的 C++ 库创建 PHP 包装器,因此重新编写代码以不使用 boost::shared_ptr 并不是真正的选择。

这是迄今为止我提出的唯一解决方案:

MyClient Client::createObject() {
    return *Client::create();
}

并在 PHP 中调用它:

$client = Client::createObject();
echo gettype($client);

现在 $client 的类型是 object,我可以调用 Client 方法就可以了。这是一个合理的解决方法吗?如果没有,我应该做什么?

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 option.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文