围绕 SWIG 包装器的自定义包装器

Custom wrappers around SWIG wrappers

我试图强制 SWIG 在 "default" 生成的包装器周围使用我自己的包装器,这是一个示例...

我有以下 "interface" 代码:

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) {...}
}

在其他 类 中,我使用预期的 return 值,例如 "Expected setHandle(IViewHandle * handle)" 并且 SWIG 生成此代码:

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"

你没有 post 它,但我猜你的 C++ 方法 setHandle 看起来像:

Expected<void> setHandle(IViewHandle);

所以,如果你只想为这个方法修改return类型,你可以设置一个%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;
%}