how to pass enum values from TCL script to C++ class using Swig

我的梦境 提交于 2019-12-04 05:36:26

问题


I am using following code

1) File : example.i

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

%include "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) 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;
}

Steps to compile and run

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'

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.


回答1:


According to the official documentation:

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

Some examples of exposing C/C++ enums in Tcl are a available at http://doc.gnu-darwin.org/enum/



来源:https://stackoverflow.com/questions/40058170/how-to-pass-enum-values-from-tcl-script-to-c-class-using-swig

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!