Ignoring specific overloaded methods with Swig

落爺英雄遲暮 提交于 2020-01-03 16:59:48

问题


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++.


回答1:


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?)




回答2:


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"


来源:https://stackoverflow.com/questions/17451877/ignoring-specific-overloaded-methods-with-swig

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!