2017-05-31 73 views
0

我試圖迫使痛飲使用自己的包裝圍繞「默認」生成的包裝,這裏有一個例子...各地SWIG包裝定製包裝

我有以下的「接口」代碼:

template<typename T> 
class Expected 
{ 
public: 
    T Value(); 
}; 

%template(Expected_Int) Expected<int>; 
%template(Expected_Bool) Expected<bool>; 
%template(Expected_Void) Expected<void>; 

和我自己的C#實現(我自己的包裝)

public class Expected 
{ 
    public Expected(Expected_Void private) {...} 
} 

在其他班,我用的是預期收益值,像這樣的「預期setHandle(IViewHandle *手柄)」,並痛飲生成驗證碼:

public override Expected_Void setHandle(IViewHandle handle) { 
    Expected_Void ret = new Expected_Void(luciadsdkPINVOKE.ViewContext_setHandle(swigCPtr, IViewHandle.getCPtr(handle)), true); 
    if (luciadsdkPINVOKE.SWIGPendingException.Pending) throw luciadsdkPINVOKE.SWIGPendingException.Retrieve(); 
    return ret; 
    } 

現在,我希望生成下面的C#代碼(大約有SWIG包裝我自己的包裝)

public override Expected setHandle(IViewHandle handle) { 
    Expected_Void ret = new Expected_Void(luciadsdkPINVOKE.ViewContext_setHandle(swigCPtr, IViewHandle.getCPtr(handle)), true); 
    if (luciadsdkPINVOKE.SWIGPendingException.Pending) throw luciadsdkPINVOKE.SWIGPendingException.Retrieve(); 
    return Expected(ret); 
} 

這可能嗎?

感謝

的解

%typemap(csout,excode=SWIGEXCODE) Expected<void> { 
    IExpected ret = new IExpected($imcall, true);$excode 
    return ret; 
} 
%typemap(cstype) Expected<void> "IExpected" 

回答

1

你沒有張貼,但我猜你的C++方法setHandle看起來像:

Expected<void> setHandle(IViewHandle); 

所以,如果你只是只想爲這種方法修改返回類型,你可以設置一個%typemap(csout),有點像這樣:

%typemap(csout) Expected<void> MyClass::setHandle %{ 
    Expected_void ret = $imcall;$excode 
    return Expected(ret); 
%} 

我認爲這應該有效。也許我忘了一些東西,但看看圍繞this,也許你會發現更多的信息。

希望它有幫助。

編輯:

實際上不會工作,這就是我的意思:

%typemap(csout) Expected<void> MyClass::setHandle %{ 
    Expected ret = new Expected($imcall);$excode 
    return ret; 
%} 
+0

謝謝, 我將我的解決方案添加到帖子中。你離我的解決方案還不太遠;-) – CDZ

+0

好的,我看到你的解決方案。如果您沒有創建IExpected來解決這個問題,那顯然是最好的。 無論如何,我注意到我在回答中犯了一個錯誤,我編輯這個帖子以防萬一。 –

+0

另一個提示,只是要知道,這個typemap將適用於所有返回預期的方法。要專注於一種方法,您需要指定方法名稱,就像我一樣。 –