views:

203

answers:

1

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?

A: 

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.

David Seiler