使用 SWIG 处理对指针/双指针的引用 [C++至爪哇]

发布于 2024-08-27 09:32:32 字数 859 浏览 1 评论 0原文

我的代码有一个类似

class IExample { ~IExample(); 的接口//纯虚方法 ...};

继承接口的类,例如

class CExample : public IExample { protected: CExample(); // 纯虚方法的实现 ... };

以及创建此类对象的全局函数 -

createExample( IExample *& obj ) { obj = new CExample();

现在,我尝试使用 SWIG 获取 Java API 包装器,SWIG 生成的接口具有类似 - IExample(long cPtr, boolean cMemoryOwn) 的构造函数,并且全局函数变为 createExample(IExample obj )

问题是当我这样做时,

IExample exObject = new IExample(ExampleLibraryJNI.new_plong(), true /*or false*/ ); ExampleLibrary.createExample( exObject );

成功调用了 C++ 层的 createExample(...) API,但是当调用返回到 Java 层时,cPtr (long) 变量未更新。理想情况下,此变量应包含 CExample 对象的地址。

我在文档中读到,类型映射也可用于处理输出参数和指针引用;但是,我无法找出使用类型映射来解决此问题的合适方法或任何其他解决方法。

请建议我是否做错了什么,或者在这种情况下如何使用类型映射?

My code has an interface like

class IExample { ~IExample(); //pure virtual methods ...};

a class inheriting the interface like

class CExample : public IExample { protected: CExample(); //implementation of pure virtual methods ... };

and a global function to create object of this class -

createExample( IExample *& obj ) { obj = new CExample(); } ;

Now, I am trying to get Java API wrapper using SWIG, the SWIG generated interface has a construcotr like - IExample(long cPtr, boolean cMemoryOwn) and global function becomes createExample(IExample obj )

The problem is when i do,

IExample exObject = new IExample(ExampleLibraryJNI.new_plong(), true /*or false*/ );
ExampleLibrary.createExample( exObject );

The createExample(...) API at C++ layer succesfully gets called, however, when call returns to Java layer, the cPtr (long) variable does not get updated. Ideally, this variable should contain address of CExample object.

I read in documentation that typemaps can be used to handle output parameters and pointer references as well; however, I am not able to figure out the suitable way to use typemaps to resolve this problem, or any other workaround.

Please suggest if i am doing something wrong, or how to use typemap in such situation?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫瑟鸿黎 2024-09-03 09:32:32

我不知道你如何用类型映射来解决这个问题; Java不支持引用参数,因此转换会非常复杂。

为什么不直接让 createExample() 返回一个 IExample* 呢?如果您需要其他内容的返回值,我建议返回 std::pair 或一些类似的结构化类型。

I don't know how you'd solve this problem with typemaps; Java doesn't support reference parameters so the conversion would be very complicated.

Why not just have createExample() return an IExample*? If you need the return value for something else, I recommend returning std::pair<IExample*,OtherThing>, or some similar structured type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文