使用 Swig 忽略特定的重载方法 [英] Ignoring specific overloaded methods with Swig

查看:30
本文介绍了使用 Swig 忽略特定的重载方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 C++ 库的包装器,以便它可以从 Java 中使用,我正在用 Swig 来做这个.

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

我面临的是我有一个 SomeClass 类,它有一些重载的方法 (someMethod).在这些重载方法中,有些接收我不想导出到包装器的复杂数据,还有一些我确实想要然后导出的简单数据.

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.

我正在尝试使用 %rename("$ignore") 指令,但要么导出所有方法,要么不导出.我还有另外两种类型的类 SimpleDataComplexData,其中一种在命名空间 ns1 中,另一种在 ns2 中, SomeClass 在命名空间ns3"中.

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

SimpleData 类:

#ifndef SIMPLEDATA_H_
#define SIMPLEDATA_H_

namespace ns1 {

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

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

类 ComplexData:

The class ComplexData:

#ifndef COMPLEXDATA_H_
#define COMPLEXDATA_H_

namespace ns2 {

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

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

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_ */

i 文件片段如下所示:

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?

完整的 .i 文件:

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

注2:我也不认为相关,但目标语言是Java,源语言是C++.

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

推荐答案

解决方案是不要在方法签名中使用引号.

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";

完整的 .i 文件供参考:

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"

这篇关于使用 Swig 忽略特定的重载方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆