2016-05-10 4 views
2

C++からTclへのポインタのリストを返したいと思います。そして、ポインターによっていくつかのメンバー関数を呼び出します。スィグはそれを手伝うことができます。Swig:C++からTclへのポインタのリストを返す方法

しかし、私はのポインタへのポインタのリストを私に与えます。

% getLongTime 10 
_30a8620000000000_p_p_MyData _30bc620000000000_p_p_MyData 

SWIGで逆参照する構文が見つかりません。だから私は各クラスの参照解除関数を書く必要があります。非常に不便です。

ポインタのリストを返すために "std_vector.i"が含まれています。誰もベクトルを使用しているが2番目のポインタを使用していないという例がありますか?または、別の解決策がOKです!

私も同じように、一般的な間接参照関数を記述しようとしています

void * dereference(void ** p){ return *p; } 

しかし、それは動作しません。 Tclは私にタイプエラーを与えます:

TypeError in method 'dereference', argument 1 of type 'void ** 

私はあなたのプログラムを試してみたいと思ったら私のコードを単純化しました。ここに私のmain.cppには、次のとおりです。

#include <cstdlib> 
#include <vector> 
using namespace std; 
#include "main.hh" 
#include <tcl.h> 

std::vector<MyData> MyData::AllTime;   // It seems the place of this statement matters 

std::vector<MyData*> getLongTime(int limit) { 
    vector<MyData*> longTime; 
    for(int i = 0; i <= 3; ++i) 
     if (MyData::AllTime[i].getMinute() >= limit) 
      longTime.push_back(&MyData::AllTime[i]); 
    return longTime; 
} 

MyData* derefMyData(MyData** p) {return *p;} // I have to write a dereference function for every class 

/// print command, exectue it and print resulte 
void print(Tcl_Interp *interp, const char *command) { 
    printf("%% %s\n", command); 
    Tcl_Eval(interp, command); 
    const char *msg = Tcl_GetStringResult(interp); 
    if (*msg != '\0') 
     printf("%s\n", msg); 
} 

int main(int argc, char** argv) { 
    Tcl_Interp *interp = Tcl_CreateInterp(); 
    Tcl_Eval(interp, "load ./swig.so swig"); 
    MyData::AllTime.push_back(MyData(10)); 
    MyData::AllTime.push_back(MyData(15)); 
    MyData::AllTime.push_back(MyData(9)); 

    // set the time exceeding 10 to 10 by tcl function. 
    print(interp, "set times [getLongTime 10]"); 
    print(interp, "foreach t $times { [derefMyData $t] setMinute 10}"); 

    printf("All times:"); 
    for(std::vector<MyData>::iterator i = MyData::AllTime.begin(); i != MyData::AllTime.end(); ++i) 
     printf("%d ", i->getMinute()); 
    printf("\n"); 

    Tcl_DeleteInterp(interp); 
    return 0; 
} 

ここに私のmain.hhが

#ifndef MAIN_HH 
#define MAIN_HH 

#include <cstdlib> 
#include <stdio.h> 
#include <vector> 
using namespace std; 

class MyData{ 
    int minute; 
public: 
    MyData(int minute):minute(minute){}; 
    int getMinute(){return minute;}; 
    void setMinute(int m){ minute = m; }; 
    static std::vector<MyData> AllTime; 
}; 

std::vector<MyData*> getLongTime(int limit); 
MyData* derefMyData(MyData** p); 
#endif /* MAIN_HH */ 

であるここに私のswig.iここ

/* swig.i */ 
%module swig 
%{ 
#include "main.hh" 
%} 
%include "std_vector.i" 
namespace std { 
    %template(myDataVector) std::vector<MyData *>; 
} 

class MyData{ 
public: 
    MyData(int minute); 
    void setMinute(int m); 
}; 

extern std::vector<MyData*> getLongTime(int limit); 
extern MyData* derefMyData(MyData** p); 

は私のコンパイルが

swig -c++ -tcl swig.i 
g++ -fpic -c main.cpp swig_wrap.cxx 
g++ -shared main.o swig_wrap.o -o swig.so ;# I don't need to compile with main.o in my original program. 
g++ main.o -o swig.out -g -I/usr/local/include -L/usr/local/lib -ltcl8.5 
setenv LD_LIBRARY_PATH /usr/local/lib:/usr/local/lib 
./swig.out 
コマンドです

出力は次のとおりです。

0ところで
% set times [getLongTime 10] 
_30a8620000000000_p_p_MyData _80bb620000000000_p_p_MyData 
% foreach t $times { [derefMyData $t] setMinute 10} 
All times:10 10 9 

、簡易番組レポート* glibcの検出*
私はそれを修正する方法がわかりません。
幸いにも、私の元のプログラムは問題ありません。
誰かがそれを修正する方法を知っている場合。私にお知らせください。

マイSWIGのバージョン:1.3.29
私のgccのバージョン:4.5.2

+0

Tclライブラリを初期化するときに、 'Tcl_CreateInterp'を実行する前に' main'から 'Tcl_FindExecutable(argv [0]);'を呼び出すべきです。 。 –

答えて

0

私は問題はあなたがMyData *ためのSWIGのタイプマップを得たのではなく、単にMyData(あるいは&MyDataためていませんでしたということだと思います。私のC++は少し不安定です)。それはSWIGがあなたが期待していることをしていないコードを生成させるようにしています。 _30a8620000000000_p_p_MyDataは、ローカル変数longTimeからコピーされた値のセルへのハンドルであるため、ハンドルのマップされたタイプはセル内容へのポインタ、つまりMyData **です。

あなたがconst vector<MyData *>を返すと、より良い結果が得られます。それは確かに値自体が実際に一定であるTcl側でもっと意味をなさないでしょう。私はSWIGがそれを利用するのに十分スマートであるかどうかはわかりません。あなた自身でタイプマップコードを書く必要があるかもしれません(あなたの実際のコードがこれよりもずっと長い場合は賢明です)。

関連する問題