我正在做一个C++库的包装,所以它可以从Java使用,我正在用Swig做到这一点。忽略特定的重载方法与Swig
我面临的是我有一个类SomeClass
,它有一些重载的方法(someMethod
)。在这些重载的方法中,有些接收到复杂的数据,我不想将其导出到包装器,还有一些简单的数据,然后我想要导出。
我试图使用%rename("$ignore")
指令,但要么我得到的所有方法导出或没有。我有另外两种类型的SimpleData
和ComplexData
,其中一个在命名空间ns1
中,另一个在ns2
,SomeClass
位于命名空间“ns3”中。
类SimpleData
:
#ifndef SIMPLEDATA_H_
#define SIMPLEDATA_H_
namespace ns1 {
class SimpleData {
public:
SimpleData(){}
virtual ~SimpleData(){}
};
} /* namespace ns1 */
#endif /* SIMPLEDATA_H_ */
类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_ */
在我的文件片段看起来是这样的:
%rename ("$ignore", fullname=1) "ns3::SomeClass::someMethod(const ns2::ComplexData&)";
但这不起作用。哪一个是忽略方法的某些特定超载的正确方法?
完整.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"
注:我不认为它实际上没有任何关系,但为了以防万一,这些方法实际上抛出一个异常。注2:我也不认为是相关的,但目标语言是Java,源语言是C++。
的文档声明'%ignore'和'%rename(“$ ignore”)'[完全一样](http://www.swig.org/Doc2.0/SWIGDocumentation。html#SWIG_advanced_renaming),只不过'%rename'有点灵活。是的,我的'%rename'在函数声明之前(更准确地说是在'%include'之前)。是的,缺乏类名是一个错字。我已经能够找到一个解决方案,它似乎是在方法签名中使用**引号**的问题,奇数。无论如何感谢您的回答,这让我再次阅读文档,并感谢我意识到这些示例没有引号。 –