/* ----------------------------------------------------------------------------- * std_complex.i * * Typemaps for handling std::complex and std::complex as a .NET * System.Numerics.Complex type. Requires .NET 4 minimum. * ----------------------------------------------------------------------------- */ %{ #include %} %fragment("SwigSystemNumericsComplex", "header") { extern "C" { // Identical to the layout of System.Numerics.Complex, but does assume that it is // LayoutKind.Sequential on the managed side struct SwigSystemNumericsComplex { double real; double imag; }; } SWIGINTERN SwigSystemNumericsComplex SwigCreateSystemNumericsComplex(double real, double imag) { SwigSystemNumericsComplex cpx; cpx.real = real; cpx.imag = imag; return cpx; } } namespace std { %naturalvar complex; template class complex { public: complex(T re = T(), T im = T()); }; } %define SWIG_COMPLEX_TYPEMAPS(T) %typemap(ctype, fragment="SwigSystemNumericsComplex") std::complex, const std::complex & "SwigSystemNumericsComplex" %typemap(imtype) std::complex, const std::complex & "System.Numerics.Complex" %typemap(cstype) std::complex, const std::complex & "System.Numerics.Complex" %typemap(in) std::complex %{$1 = std::complex< double >($input.real, $input.imag);%} %typemap(in) const std::complex &($*1_ltype temp) %{temp = std::complex< T >((T)$input.real, (T)$input.imag); $1 = &temp;%} %typemap(out, null="SwigCreateSystemNumericsComplex(0.0, 0.0)") std::complex %{$result = SwigCreateSystemNumericsComplex($1.real(), $1.imag());%} %typemap(out, null="SwigCreateSystemNumericsComplex(0.0, 0.0)") const std::complex & %{$result = SwigCreateSystemNumericsComplex($1->real(), $1->imag());%} %typemap(cstype) std::complex, const std::complex & "System.Numerics.Complex" %typemap(csin) std::complex, const std::complex & "$csinput" %typemap(csout, excode=SWIGEXCODE) std::complex, const std::complex & { System.Numerics.Complex ret = $imcall;$excode return ret; } %typemap(csvarin, excode=SWIGEXCODE2) const std::complex & %{ set { $imcall;$excode } %} %typemap(csvarout, excode=SWIGEXCODE2) const std::complex & %{ get { System.Numerics.Complex ret = $imcall;$excode return ret; } %} %template() std::complex; %enddef // By default, typemaps for both std::complex and std::complex // are defined, but one of them can be disabled by predefining the // corresponding symbol before including this file. #ifndef SWIG_NO_STD_COMPLEX_DOUBLE SWIG_COMPLEX_TYPEMAPS(double) #endif #ifndef SWIG_NO_STD_COMPLEX_FLOAT SWIG_COMPLEX_TYPEMAPS(float) #endif