-
I've managed to hack together, compile and test POC for opencascade::handle and it seems to function in trivial cases: creating and passing classes and derived classes to OCC native API. How should I go about going the other way? When the API returns an opencascade object (via opencascade::handle) I would like to downcast the object to the appropriate actual Java class so that instanceof etc work as expected. The ancient jCAE SWIG interface uses something like this but I have not figured out how to inject something like this to the JavaCPP generated code. I'm not even sure if this it is feasible to do this on the Java side ... should this use some kind of adapter code on the C++ side...
PS It seems I somehow miss use this Discussions mechanism in github as I cannot mark any of my threads as answered... |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 11 replies
-
We can easily do a reinterpret cast from anything to anything else with the constructors that accept a Pointer argument, like this one:
Don't mind, just do it when you figure it out :) |
Beta Was this translation helpful? Give feedback.
-
Hi, I took a stab at providing a new feature for JavaCPP that allows adding automatic downcasting if so desired. This is very much WIP and a first stab and I could envision a lot of different ways to do that. You can find the code here: https://github.com/nyholku/javacpp/tree/feature/autodowncast It is very simple. I added a parameter Then I modified the code generation to generate a call to The user then needs to implement this method in the adapterd and do what ever it takes to dynamically downcast the pointer that the adapter hosts. I'm in no way sure memory is managed correctly, but a trivial test case runs. For OCC I've create an adapter template class, here: https://github.com/nyholku/javacpp-occ/blob/master specifically here: https://github.com/nyholku/javacpp-occ/blob/master/src/occ-handle-adapter.hxx There is a fake C++ OCC API included that uses the OCC class and calling convention here: https://github.com/nyholku/javacpp-occ/blob/master/src/FakeOccDemoApi.hxx and a Java demo that demonstrates calling an API function that returns calls with OCC handle to the native API and returns a downcast correct Java class from that call more or less automatically. https://github.com/nyholku/javacpp-occ/blob/master/src/OccDowncastDemo.java The This is in no way representative how the OCC Java bindings should or perhaps will be done and structured, but the https://github.com/nyholku/javacpp-occ/blob/master/src/occ/TKernelConfig.java demonstrates how some of the fundamental OCC headers can be used as-is with my modified JavaCPP. All comments are welcome. (Also fixed an array subscript error in Parser IIRC.) |
Beta Was this translation helpful? Give feedback.
We can easily do a reinterpret cast from anything to anything else with the constructors that accept a Pointer argument, like this one:
http://bytedeco.org/javacpp/apidocs/org/bytedeco/javacpp/BytePointer.html#BytePointer-org.bytedeco.javacpp.Pointer-
Or do you mean how to have it do that automatically? That's not something JavaCPP can do automatically right now, but you can provide your own helper methods for that, that's very straightforward. I'm not sure what your question is... Could you elaborate?
Don't mind, just do it when you figure it out :)