0

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.

TechEnthusiast
  • 273
  • 4
  • 18

1 Answers1

0

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/

Leon
  • 31,443
  • 4
  • 72
  • 97
  • I tried the same and it is not working . I tried set one "one" ; myClass_printEnum $one . I am getting the same error TypeError in method 'myClass_printEnum', argument 1 of type 'type' – TechEnthusiast Oct 15 '16 at 16:49
  • global variable `one` is already set by `example.so`. Don't set it yourself. – Leon Oct 15 '16 at 16:51
  • is it possible to define enum Type inside class myClass and pass it from TCL for example class myClass { public: – TechEnthusiast Oct 15 '16 at 17:19
  • is it possible to define enum Type inside class myClass and pass it from TCL for example class myClass { public: enum Type { one,two} .. } an use it TCL – TechEnthusiast Oct 15 '16 at 17:20
  • it working , if I use load ./example.so . but In my actual tool code , the swig code in linked statically and I am getting following error message unknown variable myClass_one . Could you guide me how to solve this ? – TechEnthusiast Oct 16 '16 at 07:56
  • @TechEnthusiast And could you explain to me why anyone should help you on stackoverflow given that you don't appreciate their help (since your activity history shows that you haven't accepted any answer to any of your almost 40 questions asked here)? – Leon Oct 16 '16 at 08:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125834/discussion-between-techenthusiast-and-leon). – TechEnthusiast Oct 16 '16 at 12:30