[英]Swig: How to return a list of pointers from C++ to Tcl
我想返回一个从C ++到Tcl的指针列表。 然后通过指针调用一些成员函数。 SWIG可以帮助我做到这一点。
但是,SWIG给我一个指针指针列表。
% getLongTime 10
_30a8620000000000_p_p_MyData _30bc620000000000_p_p_MyData
我在SWIG中找不到取消引用的语法。 所以我必须为每个类编写解除引用函数。 这很不方便。
我包含“std_vector.i”以返回指针列表。 任何人都有一个使用矢量而没有第二个指针的例子? 或者任何替代解决方案都可以!
我也尝试编写一般的解引用函数,如:
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
这是输出:
% 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
我认为问题在于你没有MyData *
的SWIG类型图,而只是MyData
(或者也许是&MyData
;我的C ++有点不稳定)。 这使得SWIG生成的代码没有达到预期的效果; _30a8620000000000_p_p_MyData
是从longTime
局部变量复制的值中单元格的句柄,因此句柄的映射类型是指向单元格内容的指针,即MyData **
。
如果返回const vector<MyData *>
可能会得到更好的结果。 这肯定在Tcl方面更有意义,其中值本身实际上是不变的。 我不知道SWIG是否足够智能以利用它。 您可能必须自己编写类型映射代码(只有在您的实际代码比这长得多的时候才有意义)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.