2013-07-03 86 views
4

我正在做一个C++库的包装,所以它可以从Java使用,我正在用Swig做到这一点。忽略特定的重载方法与Swig

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

我试图使用%rename("$ignore")指令,但要么我得到的所有方法导出或没有。我有另外两种类型的SimpleDataComplexData,其中一个在命名空间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++。

回答

2

的解决方案是使用引号与方法签名。

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

我没有把报价摆在首位,因为我总是有,总是为我工作,例如:

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

参考完整.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" 

%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" 
6

您是否试过仅使用%ignore指令,http://www.swig.org/Doc1.3/SWIG.html#SWIG_rename_ignore?查看http://www.swig.org/Doc1.3/SWIGPlus.html#ambiguity_resolution_renaming以查看如何最佳匹配您要忽略的功能。

另请注意,“%rename指令的位置是任意的,只要它出现在要重命名的声明之前”,是否在函数之前重命名%?

(在你的榜样,你缺少的类名,我认为这只是一个笔误吧?)

+0

的文档声明'%ignore'和'%rename(“$ ignore”)'[完全一样](http://www.swig.org/Doc2.0/SWIGDocumentation。html#SWIG_advanced_renaming),只不过'%rename'有点灵活。是的,我的'%rename'在函数声明之前(更准确地说是在'%include'之前)。是的,缺乏类名是一个错字。我已经能够找到一个解决方案,它似乎是在方法签名中使用**引号**的问题,奇数。无论如何感谢您的回答,这让我再次阅读文档,并感谢我意识到这些示例没有引号。 –