如何使用Swig将TCL脚本中的枚举值传递给C ++类 [英] how to pass enum values from TCL script to C++ class using Swig
问题描述
我正在使用以下代码
1)文件:example.i
1) File : example.i
%module example
%{
/* Put header files here or function declarations like below */
#include "example.h"
%}
%include "example.h"
2)文件example.h
2) File example.h
enum Type {one,two};
class myClass {
public:
myClass() {}
static bool printVal(int val);
static bool printEnum(Type val);
};
3)文件example.cpp
3) File example.cpp
#include "example.h"
#include <iostream>
using namespace std;
bool myClass::printVal(int val) {
cout << " Int Val = " << val << endl;
return 0;
}
bool myClass::printEnum(type val) {
cout << " Enum Val = " << val << endl;
return 0;
}
编译和运行的步骤
swig -c++ -tcl example.i
g++ -c -fpic example_wrap.cxx example.cpp -I/usr/local/include
g++ -shared example.o example_wrap.o -o example.so
setenv LD_LIBRARY_PATH /pathtoexample.so:$LD_LIBRARY_PATH
tclsh
% load example.so
%myClass_printVal 1
Int Val = 1
%myClass_printEnum one
TypeError in method 'myClass_printEnum', argument 1 of type 'type'
如果我通过enum,我将收到TypeError.我知道有用于类型转换的类型映射,但是我不知道如何使用类型映射将TCL脚本中的枚举值传递给c ++类.我期待有关如何使用SWIG将TCL中的枚举值传递给c ++类对象的帮助.
I am getting TypeError if I pass enum . I know there is typemap for type conversion , but I do not know how to use typemaps to pass enum values from TCL script to c++ class . I am looking forward for help for how to pass enum values from TCL to c++ class objects using SWIG.
推荐答案
根据官方文档:
C/C ++常量作为包含以下内容的全局Tcl变量安装:适当的值.
C/C++ constants are installed as global Tcl variables containing the appropriate value.
因此,您必须通过取消引用相应的变量来引用枚举值:
So you must refer to the enum value by dereferencing the corresponding variable:
% myClass_printEnum $one
在 http://doc.gnu中可以找到一些在Tcl中公开C/C ++枚举的示例.-darwin.org/enum/
这篇关于如何使用Swig将TCL脚本中的枚举值传递给C ++类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!