7

I have a C++ header file which defines nested macros. It can be compiled with Clang or GCC, but SIWG cannot parse them properly.

I can compile MyClass.cc that includes the header file.

$ g++ MyClass.cc -c -o MyClass.o

But SWIG 3.0.2 generates the following error.

$ swig -c++ -python MyClass.i   
MyClass.h:15: Error: Syntax error in input(3).

How do I remove this error? The macros defined in Util.h use the trick explained in this question Overloading Macro on Number of Arguments

Util.h

#ifndef UTIL_H_
#define UTIL_H_

#define GET3(Name, Title, Type) \
  Type Get##Title() const { \
    return f##Name; \
  }
#define ARG_LEN_SWITCH(_1, _2, _3, NAME, ...) NAME
#define GET2(Name, Type) GET3(Name, Name, Type)
#define GET(...) ARG_LEN_SWITCH(__VA_ARGS__, GET3, GET2)(__VA_ARGS__)

#endif // UTIL_H_

MyClass.h

#ifndef MY_CLASS_H_
#define MY_CLASS_H_

#include <iostream>
#include <string>
#include "Util.h"

 class MyClass
{
 private:
  std::string fString;
 public:
  MyClass(std::string s);
  GET(String, std::string)
};

#endif // MY_CLASS_H_

MyClass.cc

#include "MyClass.h"

 MyClass::MyClass(std::string s) : fString(s)
{
}

MyClass.i

%module MyClass
%{
#include "Util.h"
#include "MyClass.h"
%}

%include "Util.h"
%include "MyClass.h"
Community
  • 1
  • 1
Akira Okumura
  • 1,816
  • 2
  • 20
  • 41

1 Answers1

0

To make this work in SWIG you'll have to use %define instead of #define. I've no idea why, I never managed to track this one down properly, e.g.

#define GET3(Name, Title, Type) \
  Type Get##Title() const { \
    return f##Name; \
  }
#define ARG_LEN_SWITCH(_1, _2, _3, NAME, ...) NAME
#define GET2(Name, Type) GET3(Name, Name, Type)
#define GET(...) ARG_LEN_SWITCH(__VA_ARGS__, GET3, GET2)(__VA_ARGS__)

Becomes:

#define GET3(Name, Title, Type) \
  Type Get##Title() const { \
    return f##Name; \
  }
#define ARG_LEN_SWITCH(_1, _2, _3, NAME, ...) NAME
#define GET2(Name, Type) GET3(Name, Name, Type)
%define GET(...)
    ARG_LEN_SWITCH(__VA_ARGS__, GET3, GET2)(__VA_ARGS__)
%enddef

Which I think should be sufficient for your example.

Flexo
  • 87,323
  • 22
  • 191
  • 272