如何使用Swig将TCL脚本中的枚举值传递给C ++类

技术爱好者

我正在使用以下代码

1)文件:example.i

%module example
%{
      /* Put header files here or function declarations like below */
      #include "example.h"
%}

%include "example.h"

2)文件example.h

enum Type {one,two};
class myClass {
    public:
        myClass() {}
        static bool printVal(int val);
        static bool printEnum(Type val);
 };

3)文件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'

如果我通过枚举,我得到TypeError。我知道有用于类型转换的类型映射,但是我不知道如何使用类型映射将TCL脚本中的枚举值传递给c ++类。我期待有关如何使用SWIG将TCL的枚举值传递给c ++类对象的帮助。

里昂

根据官方文件

C / C ++常量作为包含适当值的全局Tcl变量安装。

因此,必须通过取消引用相应的变量来引用枚举值:

% myClass_printEnum $one

有关在Tcl中公开C / C ++枚举的一些示例,请参见http://doc.gnu-darwin.org/enum/。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章