2017-09-27 32 views
1

私はこの質問回答フォーラムで非常に新しく、swigでも同じです。 swigインターフェイスファイルを使用してJavaおよびC用のラッパーを生成する正しい手順に従っているかどうかはわかりません。SWIGの構造体の関数ポインタを処理します

私のヘッダexample.hファイルが

#ifndef INCLUDE_ARTIK_WEBSOCKET_H_ 
#define INCLUDE_ARTIK_WEBSOCKET_H_ 

#ifdef __cplusplus 
extern "C" { 
#endif 
typedef enum { 
    SAMPLE_WEBSOCKET_CLOSED = 1, 
    SAMPLE_WEBSOCKET_CONNECTED, 
    SAMPLE_WEBSOCKET_HANDSHAKE_ERROR 
} sample_websocket_connection_state; 

typedef void *sample_websocket_handle; 

typedef struct { 
    char *uri; 
void *private_data; 
} sample_websocket_config; 


typedef struct { 
    void(*websocket_request) (
       sample_websocket_handle * handle, 
       sample_websocket_config * config 
       ); 

} sample_websocket_module; 

extern const sample_websocket_module websocket_module; 

#ifdef __cplusplus 
} 
#endif 
#endif 

以下のようになります。そして、私のインターフェイスファイルtest_app.iは、以下のようになります。

%module test_app 
%{ 
#include "example.h" 
%} 

%include "example.h" 

そして、私は、ファイルに

    を次生成するコマンド swig -java test_app.iを使用してラッパーを生成
  1. sample_websocket_config.jav
  2. sample_websocket_module.java
  3. SWIGTYPE_p_void.java
  4. test_app.java
  5. test_app_wrap.c
  6. sample_websocket_connection_state.java
  7. SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void.java
  8. test_appJNI.java

    typedef struct { 
    void(*websocket_request) (
          sample_websocket_handle * handle, 
          sample_websocket_config * config 
          ); 
    

    } sample_websocket_module;上記のコード

public class sample_websocket_module { 
    private transient long swigCPtr; 
    protected transient boolean swigCMemOwn; 

    protected sample_websocket_module(long cPtr, boolean cMemoryOwn) { 
    swigCMemOwn = cMemoryOwn; 
    swigCPtr = cPtr; 
    } 

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

    protected void finalize() { 
    delete(); 
    } 

    public synchronized void delete() { 
    if (swigCPtr != 0) { 
     if (swigCMemOwn) { 
     swigCMemOwn = false; 
     test_appJNI.delete_sample_websocket_module(swigCPtr); 
     } 
     swigCPtr = 0; 
    } 
    } 

    public void setWebsocket_request(SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void value) { 
    test_appJNI.sample_websocket_module_websocket_request_set(swigCPtr, this, SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void.getCPtr(value)); 
    } 

    public SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void getWebsocket_request() { 
    long cPtr = test_appJNI.sample_websocket_module_websocket_request_get(swigCPtr, this); 
    return (cPtr == 0) ? null : new SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void(cPtr, false); 
    } 

    public sample_websocket_module() { 
    this(test_appJNI.new_sample_websocket_module(), true); 
    } 

} 

下回っだから私はそれが両方の引数を合併し、別のクラスSWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void.javaを作成しているように、この関数に引数をどのように提供するかを確認していないように見えるsample_websocket_module.javaクラスを生成します。

public class SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void { 
    private transient long swigCPtr; 

    protected SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void(long cPtr, @SuppressWarnings("unused") boolean futureUse) { 
    swigCPtr = cPtr; 
    } 

    protected SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void() { 
    swigCPtr = 0; 
    } 

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

これは上記のクラスを使用している関数です。では、この関数にどのように値を与えることができますか?

public void setWebsocket_request(SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void value) { 
    test_appJNI.sample_websocket_module_websocket_request_set(swigCPtr, this, SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void.getCPtr(value)); 
    } 

ありがとうございます。

答えて

1

私はあなたが呼び出すことのできる逆参照を行うCコードを追加する必要があると思います。 test_app_wrap.cに貼り付けられる関数をインタフェースファイルに追加することができます。

のでtest_app.iは次のようになります。

%module test_app 
%{ 
#include "example.h" 

    void callwebsocketreq(sample_websocket_module *mod, 
       sample_websocket_handle * handle, 
       sample_websocket_config * config) { 
     (*mod->websocket_request)(handle,config); 
    } 

%} 

%include "example.h" 
void callwebsocketreq(sample_websocket_module *mod, 
       sample_websocket_handle * handle, 
       sample_websocket_config * config); 

今、あなたはそれらの引数を渡すために呼び出すことができますtest_app.javaでcallwebsocketreq方法があるでしょう。

関連する問題