Quantcast

Question about %interface for java

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

Question about %interface for java

Bernardo Frankenfeld
Hello,

I've posted a stack overflow question on the topic, can anyone help me?


copying the content of the question below:


I'm having a problem while wrapping a huge C++ code to JAVA using SWIG.

What I'm trying to do is wrap a Derived class that extends 2 other classes in C++, but one of the classes (Base1 on the example bellow), should be wrapped as an interface. (To account for Java not having multiple inheritance).

I've found the problem and created a simple c++ code that demonstrate the problem:

namespace Space {
  class Base1 {
      public:
    virtual void Method1() = 0;
   };
  class Base2 {
    public:
    virtual void Method2();
  };
  class Derived : public Base1, public Base2 {
    public:
    virtual void Method1();
  };
}

The code above represents what I want to wrap in JAVA.

When I run Swig on the above example, it creates Base1 as an Interface (as it should)

public interface Base1 {
  long Base1_GetInterfaceCPtr();
  void Method1();
}

And declares (in JAVA) the derived class extending Base2 and implementing Base1 (as it also should), BUT , it DOES NOT create a java method inside the Derived class that implements the interface Base1 ( Method1 inside Derived) the full Derived Java class generated is as follows:

public class Derived extends Base2 implements Base1 {
  private transient long swigCPtr;

  protected Derived(long cPtr, boolean cMemoryOwn) {
    super(SOURCEIJNI.Derived_SWIGUpcast(cPtr), cMemoryOwn);
    swigCPtr = cPtr;
  }

  protected static long getCPtr(Derived obj) {
    return (obj == null) ? 0 : obj.swigCPtr;
  }

  protected void finalize() {
    delete();
  }

  public synchronized void delete() {
    if (swigCPtr != 0) {
      if (swigCMemOwn) {
        swigCMemOwn = false;
        SOURCEIJNI.delete_Derived(swigCPtr);
      }
      swigCPtr = 0;
    }
    super.delete();
  }

  public long Base1_GetInterfaceCPtr() {
    return SOURCEIJNI.Derived_Base1_GetInterfaceCPtr(swigCPtr);
  }

  public Derived() {
    this(SOURCEIJNI.new_Derived(), true);
  }
}

And so, Im stuck with a Java compilation error of a SWIG generated class (Derived) that does not implement all methods of its interface (Base1)

What am I missing? I've read the documentation and should work...

My ".i" file: (using swig 3.0.10)

%module SOURCEI

%include <swiginterface.i>
%interface_impl(Space::Base1);

%{
#include "Source.h"
%}
%include "Source.h"


Thank you
Bernardo

------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports.http://sdm.link/zohodev2dev
_______________________________________________
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: Question about %interface for java

William S Fulton
Works for me using swig-3.0.10. Maybe you have another file called Source.h that is being picked up. Do some sanity checks by looking at the preprocessed output running swig -E.

William

On 13 July 2016 at 19:32, Bernardo Frankenfeld <[hidden email]> wrote:
Hello,

I've posted a stack overflow question on the topic, can anyone help me?


copying the content of the question below:


I'm having a problem while wrapping a huge C++ code to JAVA using SWIG.

What I'm trying to do is wrap a Derived class that extends 2 other classes in C++, but one of the classes (Base1 on the example bellow), should be wrapped as an interface. (To account for Java not having multiple inheritance).

I've found the problem and created a simple c++ code that demonstrate the problem:

namespace Space {
  class Base1 {
      public:
    virtual void Method1() = 0;
   };
  class Base2 {
    public:
    virtual void Method2();
  };
  class Derived : public Base1, public Base2 {
    public:
    virtual void Method1();
  };
}

The code above represents what I want to wrap in JAVA.

When I run Swig on the above example, it creates Base1 as an Interface (as it should)

public interface Base1 {
  long Base1_GetInterfaceCPtr();
  void Method1();
}

And declares (in JAVA) the derived class extending Base2 and implementing Base1 (as it also should), BUT , it DOES NOT create a java method inside the Derived class that implements the interface Base1 ( Method1 inside Derived) the full Derived Java class generated is as follows:

public class Derived extends Base2 implements Base1 {
  private transient long swigCPtr;

  protected Derived(long cPtr, boolean cMemoryOwn) {
    super(SOURCEIJNI.Derived_SWIGUpcast(cPtr), cMemoryOwn);
    swigCPtr = cPtr;
  }

  protected static long getCPtr(Derived obj) {
    return (obj == null) ? 0 : obj.swigCPtr;
  }

  protected void finalize() {
    delete();
  }

  public synchronized void delete() {
    if (swigCPtr != 0) {
      if (swigCMemOwn) {
        swigCMemOwn = false;
        SOURCEIJNI.delete_Derived(swigCPtr);
      }
      swigCPtr = 0;
    }
    super.delete();
  }

  public long Base1_GetInterfaceCPtr() {
    return SOURCEIJNI.Derived_Base1_GetInterfaceCPtr(swigCPtr);
  }

  public Derived() {
    this(SOURCEIJNI.new_Derived(), true);
  }
}

And so, Im stuck with a Java compilation error of a SWIG generated class (Derived) that does not implement all methods of its interface (Base1)

What am I missing? I've read the documentation and should work...

My ".i" file: (using swig 3.0.10)

%module SOURCEI

%include <swiginterface.i>
%interface_impl(Space::Base1);

%{
#include "Source.h"
%}
%include "Source.h"


Thank you
Bernardo

------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports.http://sdm.link/zohodev2dev
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user



------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports.http://sdm.link/zohodev2dev
_______________________________________________
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: Question about %interface for java

Bernardo Frankenfeld
Found the error. Was calling swig with "-small" command.

I have a follow up question... 

I have a Base1 class ( that should be wrapped as a interface file in java). But in my C++ code, it is used as a shared_ptr<Base1> . 

For example:

namespace Space {
class Base1;
typedef boost::shared_ptr<Base1> Base1Ptr;
typedef boost::shared_ptr<Base2> Base2Ptr;
typedef boost::shared_ptr<Derived> DerivedPtr;
  class Base1 {
 public:
    virtual void Method1() = 0;
virtual ~Base1() = default;
  };
  class Base2 {
 public:
    virtual void Method2();
virtual ~Base2() = default;
  };
  class Derived : public Base1, public Base2 {
  public:
virtual void Method1();
Base1Ptr testMethod(); //using sharedPtr
Base1* testMethod2(); 
  };
}

if I run swig with the ".i" file:

%module SOURCEI
%include <boost_shared_ptr.i>
%include <swiginterface.i>

%shared_ptr(Space::Base1);
%shared_ptr(Space::Base2);
%shared_ptr(Space::Derived);
%interface_impl(Space::Base1);

%{
#define SWIG_FILE_WITH_INIT
#include "Source.h"
%}
%include "Source.h"

I get the following .java Derived class:

public class Derived extends Base2 implements Base1 {
  private transient long swigCPtr;
  private boolean swigCMemOwnDerived;

  protected Derived(long cPtr, boolean cMemoryOwn) {
    super(SOURCEIJNI.Derived_SWIGSmartPtrUpcast(cPtr), true);
    swigCMemOwnDerived = cMemoryOwn;
    swigCPtr = cPtr;
  }

  protected static long getCPtr(Derived obj) {
    return (obj == null) ? 0 : obj.swigCPtr;
  }

  protected void finalize() {
    delete();
  }

  public synchronized void delete() {
    if (swigCPtr != 0) {
      if (swigCMemOwnDerived) {
        swigCMemOwnDerived = false;
        SOURCEIJNI.delete_Derived(swigCPtr);
      }
      swigCPtr = 0;
    }
    super.delete();
  }

  public long Base1_GetInterfaceCPtr() {
    return SOURCEIJNI.Derived_Base1_GetInterfaceCPtr(swigCPtr);
  }

  public void Method1() {
    SOURCEIJNI.Derived_Method1(swigCPtr, this);
  }

  public Base1 testMethod() {
    long cPtr = SOURCEIJNI.Derived_testMethod(swigCPtr, this);
    return (cPtr == 0) ? null : new Base1(cPtr, true);
  }

  public Base1 testMethod2() {
    long cPtr = SOURCEIJNI.Derived_testMethod2(swigCPtr, this);
    return (cPtr == 0) ? null : (Base1)new Base1SwigImpl(cPtr, false);
  }

  public Derived() {
    this(SOURCEIJNI.new_Derived(), true);
  }

}

testMethod2 is corrected mapped to the interface implementation. While testMethod gives a Java compile error (trying to create (new) an Interface!)

I kinda of understand why the error occurs, as i'm saying with the %shared_ptr() call that it should treat Base1Ptr as a shared_ptr and not as an interface... but i kinda need it to be both...

what is the best aproach to solve the problem? I would like for testMethod() to be implemented like testMethod2()

thank you,
Bernardo 

2016-07-15 0:23 GMT-03:00 William S Fulton <[hidden email]>:
Works for me using swig-3.0.10. Maybe you have another file called Source.h that is being picked up. Do some sanity checks by looking at the preprocessed output running swig -E.

William

On 13 July 2016 at 19:32, Bernardo Frankenfeld <[hidden email]> wrote:
Hello,

I've posted a stack overflow question on the topic, can anyone help me?


copying the content of the question below:


I'm having a problem while wrapping a huge C++ code to JAVA using SWIG.

What I'm trying to do is wrap a Derived class that extends 2 other classes in C++, but one of the classes (Base1 on the example bellow), should be wrapped as an interface. (To account for Java not having multiple inheritance).

I've found the problem and created a simple c++ code that demonstrate the problem:

namespace Space {
  class Base1 {
      public:
    virtual void Method1() = 0;
   };
  class Base2 {
    public:
    virtual void Method2();
  };
  class Derived : public Base1, public Base2 {
    public:
    virtual void Method1();
  };
}

The code above represents what I want to wrap in JAVA.

When I run Swig on the above example, it creates Base1 as an Interface (as it should)

public interface Base1 {
  long Base1_GetInterfaceCPtr();
  void Method1();
}

And declares (in JAVA) the derived class extending Base2 and implementing Base1 (as it also should), BUT , it DOES NOT create a java method inside the Derived class that implements the interface Base1 ( Method1 inside Derived) the full Derived Java class generated is as follows:

public class Derived extends Base2 implements Base1 {
  private transient long swigCPtr;

  protected Derived(long cPtr, boolean cMemoryOwn) {
    super(SOURCEIJNI.Derived_SWIGUpcast(cPtr), cMemoryOwn);
    swigCPtr = cPtr;
  }

  protected static long getCPtr(Derived obj) {
    return (obj == null) ? 0 : obj.swigCPtr;
  }

  protected void finalize() {
    delete();
  }

  public synchronized void delete() {
    if (swigCPtr != 0) {
      if (swigCMemOwn) {
        swigCMemOwn = false;
        SOURCEIJNI.delete_Derived(swigCPtr);
      }
      swigCPtr = 0;
    }
    super.delete();
  }

  public long Base1_GetInterfaceCPtr() {
    return SOURCEIJNI.Derived_Base1_GetInterfaceCPtr(swigCPtr);
  }

  public Derived() {
    this(SOURCEIJNI.new_Derived(), true);
  }
}

And so, Im stuck with a Java compilation error of a SWIG generated class (Derived) that does not implement all methods of its interface (Base1)

What am I missing? I've read the documentation and should work...

My ".i" file: (using swig 3.0.10)

%module SOURCEI

%include <swiginterface.i>
%interface_impl(Space::Base1);

%{
#include "Source.h"
%}
%include "Source.h"


Thank you
Bernardo

------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports.http://sdm.link/zohodev2dev
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user




------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity planning
reports.http://sdm.link/zohodev2dev
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...