![](/img/trans.png)
[英]How to Pass an int array and a List<string> as argument to C++ from C# when using SWIG
[英]How to create passable from C# into C++ delegate that takes a IEnumerable as argument with SWIG?
所以我有下一個C ++代碼:
#ifdef WIN32
# undef CALLBACK
# define CALLBACK __stdcall
#else
# define CALLBACK
#endif
#include <iostream>
#include <vector>
namespace OdeProxy {
typedef std::vector< double > state_type;
typedef void (CALLBACK *System)( const state_type &, state_type &, const double);
typedef void (CALLBACK *Observer)( const state_type &, double);
class Ode {
public:
state_type initialConditions;
System system;
Observer observer;
double from;
double to;
double step;
};
}
和.i文件:
/* File : MyProject.i */
%module MyProject
%{
#include "C++/OdeProxy.h"
%}
%include "std_vector.i"
%include "C++/OdeProxy.h"
%template(state_type) std::vector<double>;
//// Delegate realated stuff ////
%typemap(cstype) void (*)( const state_type &, state_type &, const double) "SystemDelegate";
%typemap(imtype) void (*)( const state_type &, state_type &, const double) "SystemDelegate";
%typemap(cstype) void (*)( const state_type &, double) "ObserverDelegate";
%typemap(imtype) void (*)( const state_type &, double) "ObserverDelegate";
我創造了這個線程的靈感。 代碼生成。
但我無法理解如何獲得代碼
using OdeLibrary;
namespace OdeTest
{
class Program
{
static void Main(string[] args)
{
//var lam = new OdeLibrary.SWIGTYPE_p_f_r_q_const__std__vector__double___double__void()
var ode = new Ode{
from = 0,
to = 10,
initialConditions = new state_type(new[]{1,2,3}),
step = 0.01,
observer = (x, dxdt, t) => { return; }
};
}
}
}
編譯。 錯誤:
Error Cannot convert lambda expression to type 'OdeLibrary.SWIGTYPE_p_f_r_q_const__std__vector__double___double__void' because it is not a delegate type
SWIGTYPE_p_f_r_q_const__std__vector__double___double__void
如下所示:
/* ----------------------------------------------------------------------------
* This file was automatically generated by SWIG (http://www.swig.org).
* Version 2.0.9
*
* Do not make changes to this file unless you know what you are doing--modify
* the SWIG interface file instead.
* ----------------------------------------------------------------------------- */
namespace OdeLibrary {
using System;
using System.Runtime.InteropServices;
public class SWIGTYPE_p_f_r_q_const__std__vector__double___double__void {
private HandleRef swigCPtr;
internal SWIGTYPE_p_f_r_q_const__std__vector__double___double__void(IntPtr cPtr, bool futureUse) {
swigCPtr = new HandleRef(this, cPtr);
}
protected SWIGTYPE_p_f_r_q_const__std__vector__double___double__void() {
swigCPtr = new HandleRef(null, IntPtr.Zero);
}
internal static HandleRef getCPtr(SWIGTYPE_p_f_r_q_const__std__vector__double___double__void obj) {
return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
}
}
}
所以我想知道什么應該在.i
文件中更改或添加到c#generated wrapper中以獲得將C#lambda作為委托傳遞給C ++類的能力?
這適用於以下配置:
public class StateTypeCustomMarshaller : ICustomMarshaler
{
public static ICustomMarshaler GetInstance(string s)
{
return new StateTypeCustomMarshaller();
}
public object MarshalNativeToManaged(IntPtr pNativeData)
{
return new state_type(pNativeData, false);
}
public IntPtr MarshalManagedToNative(object ManagedObj)
{
throw new NotImplementedException();
}
public void CleanUpNativeData(IntPtr pNativeData)
{
throw new NotImplementedException();
}
public void CleanUpManagedData(object ManagedObj)
{
}
public int GetNativeDataSize()
{
throw new NotImplementedException();
}
}
public delegate void ObserverDelegate(
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(StateTypeCustomMarshaller))]state_type state,
double d);
對應的.i文件是:
/* File : MyProject.i */
%module MyProject
%include "std_vector.i"
%template(state_type) std::vector<double>;
//// Delegate realated stuff ////
%typemap(csin) void (*)(OdeProxy::state_type&, double) "$csinput";
%typemap(cstype) void (*)(OdeProxy::state_type&,double) "ConsoleApplication2.Helpers.ObserverDelegate";
%typemap(imtype) void (*)(OdeProxy::state_type&, double) "ConsoleApplication2.Helpers.ObserverDelegate";
%typemap(csvarout) void (*)(OdeProxy::state_type&, double) %{
get {
return $imcall;
} %}
%{
#include "OdeProxy.h"
%}
%include "OdeProxy.h"
注意:我嘗試過對state-type的非常量引用,但是使用常量引用也可以。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.