Menu

#1320 Java: typedef struct b b;

None
closed-fixed
nobody
None
5
2022-07-26
2013-04-22
xypron
No

Dear maintainer,

I use Swig 2.0.9 for creating a Java wrapper for the following C include:

typedef struct _a a;

struct _a {
  a* foo;
};

typedef struct b b;

struct b {
  b* foo;
};

My expecation is that both generated classes are structurally equivalent. This is not the case.

Class _a uses
  public void setFoo(_a value) {
    testJNI._a_foo_set(swigCPtr, this, _a.getCPtr(value), value);
  }

which allows to easily access foo.

Unfortunately class b uses

  public void setFoo(SWIGTYPE_p_b value) {
    testJNI.b_foo_set(swigCPtr, SWIGTYPE_p_b.getCPtr(value));
  }

I would have expected

  public void setFoo(b value) {
    testJNI.b_foo_set(swigCPtr, this, b.getCPtr(value), value);
  }

Best regards

Xypron

Discussion

  • xypron

    xypron - 2013-05-01

    Essentially

    %define %glp_structure(TYPE)
    %typemap(jni) TYPE * "jlong"
    %typemap(jtype) TYPE * "long"
    %typemap(jstype) TYPE * "TYPE"
    %typemap(in) TYPE %{ $1 = ($&1_ltype)&$input; %}
    %typemap(out) TYPE

    %{ *($&1_ltype)&$result = $1; %}
    %typemap(javain) TYPE * "TYPE.getCPtr($javainput)"
    %typemap(javaout) TYPE * {
    long cPtr = $jnicall;
    return (cPtr == 0) ? null : new TYPE(cPtr, $owner);
    }
    %enddef
    %glp_structure(b)
    %include "my_include.h"

    will produce the result wanted for this single case. But I do not see why this should be necessary.

    I suggest a logic like the one above should be merged into the standard behavior of Swig.

    Best regards

    Xypron

     
  • Olly Betts

    Olly Betts - 2022-07-26
    • status: open --> closed-fixed
    • Group: -->
     
  • Olly Betts

    Olly Betts - 2022-07-26

    With git master I get:

      public void setFoo(b value) {   
        xJNI.b_foo_set(swigCPtr, this, b.getCPtr(value), value);
      }
    

    That appears to be the code you wanted so it seems this has been fixed in the meantime, so closing. If you're still seeing the problem, please open a new ticket on https://github.com/swig/swig with a reproducer.

     

Log in to post a comment.