C++->Java: SWIGTYPE_p_ objects pointing to DEFINE'd or TYPEDEF ST RUCT'd objects

View: New views
2 Messages — Rating Filter:   Alert me  

C++->Java: SWIGTYPE_p_ objects pointing to DEFINE'd or TYPEDEF ST RUCT'd objects

by Alexis Beuraud :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

Hi all

 

I am a SWIG newbie, and I struggle with type/struct definitions in the C++ header files

I would love to search the SWIG archive but when I perform a search, I often get a 'timeout' instead of responses.

Furthermore, http://sourceforge.net/mailarchive/forum.php?forum_name=swig-user today "yields 500 - Internal Server Error"

 

 

I have a third-party DLL file with C++ header files, and I would like to call the DLL function through Java.

 

However there are a lot of objects which are DEFINE'd, typedef'd and I do not know how to handle with that.

 

For example, there is a TK_BOOL type and a STKSupLanguage structure defined as following:

 

#define TK_BOOL     int

 

typedef struct

{

  TK_BOOL English;         /* @field English language supported. */

  TK_BOOL Dutch;           /* @field Dutch language supported. */

  TK_BOOL French;          /* @field French language supported. */

  TK_BOOL German;          /* @field German language supported. */

  TK_BOOL Spanish;         /* @field Spanish language supported. */

  TK_BOOL Italian;         /* @field Italian language supported. */

  TK_BOOL Swedish;         /* @field Swedish language supported. */

  TK_BOOL Polish;          /* @field Polish language supported. */

  TK_BOOL Japanese;        /* @field Japanese language supported. */

} STKSupLanguages;

 

 

I ran SWIG.exe on the header files to generate C++ wrapper files and Java code.  It generated Java code corresponding to the DLL C++ functions, like this one :

 

  public static SWIGTYPE_p_TK_BOOL Init___ToolkitEx(SWIGTYPE_p_SUserFunc f_UserFunc, SWIGTYPE_p_ELanguageType f_language, SWIGTYPE_p_STKFunctions f_pTKFunctions, SWIGTYPE_p_TKCommandSet_t f_CommandSet, SWIGTYPE_p_TKError_t f_pErrorInfo) {

    return new SWIGTYPE_p_TK_BOOL(mypackageJNI.Init___ToolkitEx(SWIGTYPE_p_SUserFunc.getCPtr(f_UserFunc), SWIGTYPE_p_ELanguageType.getCPtr(f_language), SWIGTYPE_p_STKFunctions.getCPtr(f_pTKFunctions), SWIGTYPE_p_TKCommandSet_t.getCPtr(f_CommandSet), SWIGTYPE_p_TKError_t.getCPtr(f_pErrorInfo)), true);

  }

 

The java code for the functions seems to be ok. But these SWIGTYPE_p_* classes are all similar to this one :

 

public class SWIGTYPE_p_ELanguageType {

     

  private long swigCPtr;

 

  protected SWIGTYPE_p_ELanguageType(long cPtr, boolean futureUse) {

    swigCPtr = cPtr;

  }

 

  protected SWIGTYPE_p_ELanguageType() {

    swigCPtr = 0;

  }

 

  protected static long getCPtr(SWIGTYPE_p_ELanguageType obj) {

    return (obj == null) ? 0 : obj.swigCPtr;

  }

}

 

So, as far as I understand, SWIG generated correct java code for the exported functions, but is not generating the code corresponding to the passed parameters. I cannot instanciate objects in order to use SWIGTYPE_p_* pseudo-pointers in order to call the native function.

I had a look on the documentation about simple pointers handling (which uses the C++ type int) and I tried launching SWIG with such a .i file :

 

 

##############TK_BOOL.i

%module mymodule

%{

#include"tktypes.h"

%}

 

%include "enums.swg"

%javaconst(1);

enum { FALSE=0, TRUE=1};

 

%include cpointer.i

%pointer_functions(TK_BOOL, TK_BOOLp);

#######end - TK_BOOL.i

 

This resulted into the following java file being generated : TK_BOOLConstants.java, TK_BOOL.java and TK_BOOLJNI.java (and SWIGTYPE_p_TK_BOOL.java, the same as before)

 

public interface TK_BOOLConstants {

  public final static int FALSE = 0;

  public final static int TRUE = 1;

 

}

public class TK_BOOL implements TK_BOOLConstants {

  public static SWIGTYPE_p_TK_BOOL new_TK_BOOLp() {

    long cPtr = TK_BOOLJNI.new_TK_BOOLp();

    return (cPtr == 0) ? null : new SWIGTYPE_p_TK_BOOL(cPtr, false);

  }

 

  public static SWIGTYPE_p_TK_BOOL copy_TK_BOOLp(SWIGTYPE_p_TK_BOOL value) {

    long cPtr = TK_BOOLJNI.copy_TK_BOOLp(SWIGTYPE_p_TK_BOOL.getCPtr(value));

    return (cPtr == 0) ? null : new SWIGTYPE_p_TK_BOOL(cPtr, false);

  }

 

  public static void delete_TK_BOOLp(SWIGTYPE_p_TK_BOOL self) {

    TK_BOOLJNI.delete_TK_BOOLp(SWIGTYPE_p_TK_BOOL.getCPtr(self));

  }

 

  public static void TK_BOOLp_assign(SWIGTYPE_p_TK_BOOL self, SWIGTYPE_p_TK_BOOL value) {

    TK_BOOLJNI.TK_BOOLp_assign(SWIGTYPE_p_TK_BOOL.getCPtr(self), SWIGTYPE_p_TK_BOOL.getCPtr(value));

  }

 

  public static SWIGTYPE_p_TK_BOOL TK_BOOLp_value(SWIGTYPE_p_TK_BOOL self) {

    return new SWIGTYPE_p_TK_BOOL(TK_BOOLJNI.TK_BOOLp_value(SWIGTYPE_p_TK_BOOL.getCPtr(self)), true);

  }

 

This is what I needed, but I still lack a function: I cannot affect to a TK_BOOL object a specified value (TRUE or FALSE), I can only copy an existing TK_BOOL object .

For example I would like to do (pseudo-code):

SWIGTYPE_p_TK_BOOL pBool = TK_BOOL.new_TK_BOOLp(BOOLConstants.TRUE);

SWIGTYPE_p_ELanguageType = STKSupLanguages.new_ELanguagep(STKSupLanguages.English)

 

My questions are

1)       How do I get SWIG to generate some java code allowing me to set a variable which I will pass from Java through a SWIGTYPE_p_ object?

2)       There are many DEFINE and typedef struct in my C++ header files. Shall I create a .i file for each of them? Isn't there a way to generate them automatically? Or Is there a way to generate all needed Java files from the C++ header files, without using .i files?

3)       Am I going the right way? Or did I make big mistakes so far?

 

 

Thank you in advance for your help.

Alexis


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: C++->Java: SWIGTYPE_p_ objects pointing to DEFINE'd orTYPEDEF ST RUCT'd objects

by David Piepgrass :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> For example, there is a TK_BOOL type and a STKSupLanguage structure
> defined as following:
...
> I ran SWIG.exe on the header files to generate C++ wrapper files and
Java
> code.  It generated Java code corresponding to the DLL C++ functions,
like
> this one :
>
>   public static SWIGTYPE_p_TK_BOOL
Init___ToolkitEx(SWIGTYPE_p_SUserFunc
> f_UserFunc, SWIGTYPE_p_ELanguageType f_language,
SWIGTYPE_p_STKFunctions
> f_pTKFunctions, SWIGTYPE_p_TKCommandSet_t f_CommandSet,
> SWIGTYPE_p_TKError_t f_pErrorInfo) {
>
>     return new
>
SWIGTYPE_p_TK_BOOL(mypackageJNI.Init___ToolkitEx(SWIGTYPE_p_SUserFunc.ge
tC
> Ptr(f_UserFunc), SWIGTYPE_p_ELanguageType.getCPtr(f_language),
> SWIGTYPE_p_STKFunctions.getCPtr(f_pTKFunctions),
> SWIGTYPE_p_TKCommandSet_t.getCPtr(f_CommandSet),
> SWIGTYPE_p_TKError_t.getCPtr(f_pErrorInfo)), true);
>
>   }

Everything named SWIGTYPE_p_* is a data type that SWIG does not
recognize. You need to %include the header file that defines TK_BOOL and
STKSupLanguage, then SWIG will generate better wrappers.

For example, if those types are defined in "tktypes.h", then use

%include"tktypes.h"

in addition to what you had already,

%{
#include"tktypes.h"
%}


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user
LightInTheBox - Buy quality products at wholesale price