Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring specific overloaded methods with Swig

Tags:

swig

I'm making a wrapper of a C++ library so it can be used from Java, I'm doing this with Swig.

What I'm facing is that i have a Class SomeClass, which has some overloaded methods (someMethod). Of this overloaded methods some receive complex data that i don't want to export to the wrapper, and some simple ones which i do want then exported.

I'm trying to use the %rename("$ignore") directive but either i get all methods exported or none. I have another two types of Classes SimpleData and ComplexData, one of them in namespace ns1 and the other in ns2, SomeClass is in namespace ''ns3`.

The class SimpleData:

#ifndef SIMPLEDATA_H_
#define SIMPLEDATA_H_

namespace ns1 {

class SimpleData {
public:
    SimpleData(){}
    virtual ~SimpleData(){}
};

} /* namespace ns1 */
#endif /* SIMPLEDATA_H_ */

The class ComplexData:

#ifndef COMPLEXDATA_H_
#define COMPLEXDATA_H_

namespace ns2 {

class ComplexData {
public:
    ComplexData(){}
    virtual ~ComplexData(){}
};

} /* namespace ns2 */
#endif /* COMPLEXDATA_H_ */

The class SomeClass:

#ifndef SOMECLASS_H_
#define SOMECLASS_H_

#include "SimpleData.h"
#include "ComplexData.h"

namespace ns3 {

class SomeClass {
public:
    SomeClass(){}
    bool someMethod(const ns1::SimpleData & data){return true;}
    bool someMethod(const ns2::ComplexData & data){return true;}
    bool someMethod(const int & data){return true;}
    bool anotherMethod();
    virtual ~SomeClass(){}
};

} /* namespace ns3 */
#endif /* SOMECLASS_H_ */

The i file snippet looks like this:

%rename ("$ignore", fullname=1) "ns3::SomeClass::someMethod(const ns2::ComplexData&)";

But that isn't working. Which one is the correct way of ignoring some specific overload of a method?

The full .i file:

%module libSomeClass

%{
    #include "../src/SomeClass.h"
%}

%pragma(java) jniclasscode=%{
  static {
    try {
        System.loadLibrary("SWIG_C++");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load. \n" + e);
      System.exit(1);
    }
  }
%}

// SimpleData
%include "../src/SimpleData.h"

//removes too much
//%rename ("$ignore", fullname=1) "ns3::SomeClass::someMethod";

//does not work
%rename ("$ignore", fullname=1) "ns3::SomeClass::someMethod(const ns2::ComplexData &)";

%rename ("renamedMethod", fullname=1) "ns3::SomeClass::anotherMethod";

%include "../src/SomeClass.h"

Note: I don't think it actually has nothing to do but just in case, those methods actually throw a exception.

Note2: I also don't think is relevant, but the target language is Java and the source language is C++.

like image 757
Javier Mr Avatar asked Jul 03 '13 15:07

Javier Mr


2 Answers

Have you tried using just the %ignore directive, http://www.swig.org/Doc1.3/SWIG.html#SWIG_rename_ignore? Check out http://www.swig.org/Doc1.3/SWIGPlus.html#ambiguity_resolution_renaming to see how to best match the function you want to ignore.

Also note that "The placement of the %rename directive is arbitrary as long as it appears before the declarations to be renamed", is your %rename before the function?

(In your example you are missing the class name, I assume that is just a typo right?)

like image 68
Dan Macumber Avatar answered Oct 15 '22 11:10

Dan Macumber


The solution is to not use quotes with the method signature.

%rename ("$ignore", fullname=1) ns3::SomeClass::someMethod(const ns2::ComplexData &);

I did put quotes in the first place because I always have and always worked for me, for example:

%rename ("renamedMethod", fullname=1) "ns3::SomeClass::anotherMethod";

The full .i file for reference:

%module libSomeClass

%{
    #include "../src/SomeClass.h"
%}

%pragma(java) jniclasscode=%{
  static {
    try {
        System.loadLibrary("SWIG_C++");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load. \n" + e);
      System.exit(1);
    }
  }
%}

// SimpleData
%include "../src/SimpleData.h"

%rename ("$ignore", fullname=1) ns3::SomeClass::someMethod(const ns2::ComplexData &);

%ignore ns3::SomeClass::someMethod(const int &);

%rename ("renamedMethod", fullname=1) "ns3::SomeClass::anotherMethod";

%include "../src/SomeClass.h"
like image 3
Javier Mr Avatar answered Oct 15 '22 12:10

Javier Mr