Quantcast

Renaming a generated SWIGTYPE

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Renaming a generated SWIGTYPE

Kustaa Nyholm
Hi,


Background: I'm extending jCAE/occjava which is used to access a huge
C++ CAD/solid modeling library called OpenCasCade from Java.


Some C++ side objects need to compared for equality so that I can test that it actually
is the very same instance.

To do that I've added following:


%typemap(javacode) Handle_Standard_Type& %{
  public boolean equals(Object obj) {
    boolean equal = false;
    if (obj instanceof $javaclassname)
      equal = ((($javaclassname)obj).swigCPtr == this.swigCPtr);
    return equal;
  }
%}

And this works but generates and ugly SWIGTYPE_p_Hanlde_Standard_Type Java name.

I've been up and down the information highway looking for a solution but
none of the suggestions I've seem to work.

For example if in my .i file I add

class Handle_Standard_Type {}

the I get the pretty name but the equals() method does not get injected into the
generated code.

A further complication is that, most of the classes/objects in this library
(OCC) are accessed via handles, so that for each class, say for example
'Geom_Curve' there is a class 'Handle_Geom_Curve'.

To get rid of that 'Handle_' ugliness on the Java side occjava defines adds a
bunch of SWIG definitions like this:

%rename(Geom_Curve) Handle_Geom_Curve;

which the expose the actual methods like this:

%extend Handle_Geom_Curve
{
.....
Standard_Boolean isClosed() const
  {
    return (*self)->IsClosed();
  }
......
}

And so I'm looking for a solution that would be compatible with this renaming
convention.

wbr Kusti


 
 
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Extending the Java proxy class

Kustaa Nyholm
This is related to my yesterdays question of adding an equals() method, but
I approached this slightly differently and ask again little differently to
see if I get a better response.


I have this SWIG definition:

class Handle_Standard_Type {};

%typemap(javacode) Handle_Standard_Type %{
          public boolean equals(Object obj) {
            boolean equal = false;
            if (obj instanceof $javaclassname)
              equal = ((($javaclassname)obj).swigCPtr == this.swigCPtr);
            return equal;
          }
%}


The file Handle_Standard_Type.java gets generated but the equals() method
I was trying to inject there does not.

Why is this typemap not matching or what is the matter here?

wbr Kusti


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Extending the Java proxy class

Martin Siggel
Hi Kusti,

you should probably use extend instead of typemap as done here:

https://github.com/tpaviot/pythonocc-core/blob/master/src/SWIG_files/wrapper/Standard.i#L1291

Just replace pythoncode with javacode and put your code in instead.

Cheers,
Martin

2016-12-04 8:43 GMT+01:00 Kustaa Nyholm <[hidden email]>:

> This is related to my yesterdays question of adding an equals() method, but
> I approached this slightly differently and ask again little differently to
> see if I get a better response.
>
>
> I have this SWIG definition:
>
> class Handle_Standard_Type {};
>
> %typemap(javacode) Handle_Standard_Type %{
>           public boolean equals(Object obj) {
>             boolean equal = false;
>             if (obj instanceof $javaclassname)
>               equal = ((($javaclassname)obj).swigCPtr == this.swigCPtr);
>             return equal;
>           }
> %}
>
>
> The file Handle_Standard_Type.java gets generated but the equals() method
> I was trying to inject there does not.
>
> Why is this typemap not matching or what is the matter here?
>
> wbr Kusti
>
>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
> _______________________________________________
> Swig-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/swig-user

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Extending the Java proxy class

Kustaa Nyholm

> On 4 Dec 2016, at 11:22, Martin Siggel <[hidden email]> wrote:
>
> Hi Kusti,
>
> you should probably use extend instead of typemap as done here:
>
> https://github.com/tpaviot/pythonocc-core/blob/master/src/SWIG_files/wrapper/Standard.i#L1291
>
> Just replace pythoncode with javacode and put your code in instead.
>
> Cheers,
> Martin

Hi Martin,

thanks for the tip. Looks like a great idea but there is no %javacode possibility %extend, too bad :(

wbr Kusti



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Extending the Java proxy class

Vadim Zeitlin-13
In reply to this post by Kustaa Nyholm
On Sun, 4 Dec 2016 09:43:32 +0200 Kustaa Nyholm <[hidden email]> wrote:

KN> I have this SWIG definition:
KN>
KN> class Handle_Standard_Type {};
KN>
KN> %typemap(javacode) Handle_Standard_Type %{
KN>  public boolean equals(Object obj) {
KN>    boolean equal = false;
KN>    if (obj instanceof $javaclassname)
KN>      equal = ((($javaclassname)obj).swigCPtr == this.swigCPtr);
KN>    return equal;
KN>  }
KN> %}
KN>
KN>
KN> The file Handle_Standard_Type.java gets generated but the equals() method
KN> I was trying to inject there does not.
KN>
KN> Why is this typemap not matching or what is the matter here?

 You must define the typemap before the class it applies to is parsed by
SWIG, so if you really do it in the order shown above, it's never going to
work.

 FWIW I can confirm that using javacode typemaps to define equals() for
the generated Java wrappers does work, so you're on the right track (BTW,
I'm not a Java expert, but I believe you should override hashCode() as well
if you override equals()).

 Regards,
VZ

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user

attachment0 (203 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Extending the Java proxy class

Kustaa Nyholm

> On 4 Dec 2016, at 16:05, Vadim Zeitlin <[hidden email]> wrote:
>
> You must define the typemap before the class it applies to is parsed by
> SWIG, so if you really do it in the order shown above, it's never going to
> work.

Brilliant!

That solved the issue. I sort knew this but it did not connect and in
my confusion I figured that the class def needs to be before the typemap
cause typically in programming language you introduce types before you  
use them, but of course here it works the other way round because it is
the scope of the typemap that matters, as explained in SWIG manual 10.2.2

For the posterity this is what made it work for me. Of course the rename
is not functionally necessary, it is just to conform to occjava naming
convention.


%rename(Standard_Type) Handle_Standard_Type;
%typemap(javacode) Handle_Standard_Type %{  
  public boolean equals(Object obj) {
    boolean equal = false;
    if (obj instanceof $javaclassname)
      equal = ((($javaclassname)obj).swigCPtr == this.swigCPtr);
    return equal;
  }
%}

class Handle_Standard_Type {};

> I'm not a Java expert, but I believe you should override hashCode() as well
> if you override equals()).

That is of course correct and a good reminder. Not 100% sure what is the best
approach here. I guess I could use the "swigCPtr" then at least it would
work correctly, not sure if that would an optimal hash code.

Thanks a billion for solving this puzzle for me.

wbr Kusti
>


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re-using SWIG Python binding to create Java bingings?

Kustaa Nyholm
Hi,

I'm interested in using Opencascade C++ library from Java.

Currently there is a nice but not complete interface occjava which is based on SWIG.

There is also a complete Python binging for OCC which is based on SWIG.

Is there any (automatic) way to leverage the Python binding to create a corresponding Java binging?

Or (not a SWIG question really) to directly call the Python bindings from Java with reflection or
JNA?

wbr Kusti
 
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Re-using SWIG Python binding to create Java bingings?

William S Fulton


On 7 January 2017 at 15:37, Kustaa Nyholm <[hidden email]> wrote:
Hi,

I'm interested in using Opencascade C++ library from Java.

Currently there is a nice but not complete interface occjava which is based on SWIG.

There is also a complete Python binging for OCC which is based on SWIG.

Is there any (automatic) way to leverage the Python binding to create a corresponding Java binging?

SWIG is only a semi-automated tool for wrapping C++ code. How much additional code you need to write to better support Java in addition to existing Python bindings really depends on how much the Python bindings have been customized. If there are lots of customizations, such as Python typemaps and python specific SWIG features, then an equivalent may need to be supplied for Java. Sounds like someone needs to roll up their sleeves and add in the missing Java SWIG code.

William


------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Re-using SWIG Python binding to create Java bingings?

Kustaa Nyholm

> SWIG is only a semi-automated tool for wrapping C++ code. How much additional code you need to write to better support Java in addition to existing Python bindings really depends on how much the Python bindings have been customized. If there are lots of customizations, such as Python typemaps and python specific SWIG features, then an equivalent may need to be supplied for Java. Sounds like someone needs to roll up their sleeves and add in the missing Java SWIG code.
>
> William

Thanks, I just wanted to know before I roll up my leaves if there was
some way to automatically leverage those. Of course they can provide
guidance ... oh well time get back in the saddle.

wbr Kusti
------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...