Martin.Martinsson
Martin.Martinsson

Reputation: 2154

SWIG change type of field of a specific type for C#

Let's say I have this in C/C++:

struct MyType {
   int foo;
   long other;   
};

I want to change "int" ONLY of field "foo" to bool in SWIG! How can I do this?

I don't want to change the original source. Normally if it were my sources, I would just change to bool!

Upvotes: 0

Views: 255

Answers (1)

You can use %apply to use the bool typemaps on int foo to achieve what you want:

%module test
%apply bool { int foo };
%inline %{
struct MyType {
   int foo;
};
%}

In other more convoluted scenarios you'd have to write your own typemaps to support this, but since bool and int are implicitly convertible in C++ this will compile and work just fine out of the box.

Upvotes: 1

Related Questions