你試圖實現的東西似乎是強大的打字。 SWIG默認情況下會嘗試使用與您在C++中看到的行爲相同的行爲來公開接口,因此在這種情況下,行爲是預期的 - weak typedefs都是C++提供的。你可以解決這個問題。
鑑於頭文件:
#include <string>
class SingleValue
{
public:
SingleValue()
{}
~SingleValue()
{}
std::string getValue() const
{
return std::to_string(m_nSingleValue);
}
private:
int m_nSingleValue;
};
typedef SingleValue RoadType;
typedef SingleValue RoadSubType;
typedef SingleValue FunctionalClass;
inline RoadType make_road() { return RoadType(); }
FunctionalClass make_func() { return FunctionalClass(); }
它不同於你只能在更正getValue()
並增加了兩個內聯函數的測試,我們可以把它包起來,並通過做親近強類型定義語義:
%module test
%{
#include "test.h"
%}
%include <std_string.i>
class SingleValue
{
public:
SingleValue();
~SingleValue();
std::string getValue() const;
};
struct RoadType : public SingleValue {
};
struct RoadSubType : public SingleValue {
};
struct FunctionalClass : public SingleValue {
};
RoadType make_road();
FunctionalClass make_func();
請注意,我還沒有在所有顯示SWIG typedef的和我有關的類型和RoadType
等所有腦幹完全撒謊,但這是OK一樣,因爲所有得到由SWIG生成的代碼是樂gal並且仍然正確。
這會導致接口在make_X
函數返回不同類型時生成。
如果你想避免頭文件,你可以引入一個宏來幫助接口文件之間的重複,頭文件就變成了:
#include <string>
class SingleValue
{
public:
SingleValue()
{}
~SingleValue()
{}
std::string getValue() const
{
return std::to_string(m_nSingleValue);
}
private:
int m_nSingleValue;
};
#ifndef STRONG_TYPEDEF
#define STRONG_TYPEDEF(o,n) typedef o n
#endif
STRONG_TYPEDEF(SingleValue, RoadType);
STRONG_TYPEDEF(SingleValue, RoadSubType);
STRONG_TYPEDEF(SingleValue, FunctionalClass);
inline RoadType make_road() { return RoadType(); }
FunctionalClass make_func() { return FunctionalClass(); }
這意味着該接口文件可以簡單地成爲:
%module test
%{
#include "test.h"
%}
%include <std_string.i>
#define STRONG_TYPEDEF(o, n) struct n : o {};
%include "test.h"
這個工作一方面是因爲SingleValue
是一類,因此強烈的typedef可以成爲Java類型系統爲它實施CH一個子類吃東西。如果類型不是類,那麼仍然可以在不使用繼承的情況下執行相同的操作,例如the first part of an answer I gave on a similar problem使用D將會起作用 - 儘管如果沒有任何繼承存在,您仍然需要詳細說明空結構。