문제

Using SWIG 3.0.0 on Windows, exporting to python, I have the following interface file:

%module example

%immutable;

class MyClass {
 public:
%mutable;
  char *str1;
%immutable;
  char *str2;
};

My intent is to make the all class members in the whole file immutable (read-only) by default, but allow a few to be writable -- in this case str1.

But SWIG seems to ignore the %mutable directives inside the class; it never generates any MyClass_str1_set method for str1 or str2. I've been through the doc and it looks like this ought to work. I've also tried putting %mutable MyClass::str1; at top level, but that doesn't help either. What am I missing?

도움이 되었습니까?

해결책

It is not that clear from docs and can't test this right now but this might work:

// make everything immutable except str2 data member:
%feature("immutable","1");
%feature("immutable","0") MyClass::str2;

class MyClass {
 public:
  char *str1;
  char *str2;
  char *str3;
};

If that doesn't work you could also try moving the statement inside the class:

class MyClass {
 public:

  // make everything immutable except str2 data member:
  %immutable;   
  char *str0;
  char *str1;

  %mutable;   
  char *str2;

  %immutable;   
  char *str3;
  char *str4;
};
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top