c++tclswigtypemaps

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


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.


Solution

  • 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/