开发者

handling refrence to pointers/double pointers using SWIG [C++ to Java]

开发者 https://www.devze.com 2022-12-25 10:13 出处:网络
My code has an interface like class IExample { ~IExample(); //pure virtual methods ...}; a class inheriting the interface like

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消