summaryrefslogtreecommitdiff
path: root/linux-x86/share/swig/csharp/std_complex.i
blob: 6a0cc54585fb35671764730db5807d221577c537 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* -----------------------------------------------------------------------------
 * std_complex.i
 *
 * Typemaps for handling std::complex<float> and std::complex<double> as a .NET
 * System.Numerics.Complex type. Requires .NET 4 minimum.
 * ----------------------------------------------------------------------------- */

%{
#include <complex>
%}

%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<typename T>
class complex
{
public:
    complex(T re = T(), T im = T());
};

}

%define SWIG_COMPLEX_TYPEMAPS(T)
%typemap(ctype, fragment="SwigSystemNumericsComplex") std::complex<T>, const std::complex<T> & "SwigSystemNumericsComplex"
%typemap(imtype) std::complex<T>, const std::complex<T> & "System.Numerics.Complex"
%typemap(cstype) std::complex<T>, const std::complex<T> & "System.Numerics.Complex"

%typemap(in) std::complex<T>
%{$1 = std::complex< double >($input.real, $input.imag);%}

%typemap(in) const std::complex<T> &($*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<T>
%{$result = SwigCreateSystemNumericsComplex($1.real(), $1.imag());%}

%typemap(out, null="SwigCreateSystemNumericsComplex(0.0, 0.0)") const std::complex<T> &
%{$result = SwigCreateSystemNumericsComplex($1->real(), $1->imag());%}

%typemap(cstype) std::complex<T>, const std::complex<T> & "System.Numerics.Complex"

%typemap(csin) std::complex<T>, const std::complex<T> & "$csinput"

%typemap(csout, excode=SWIGEXCODE) std::complex<T>, const std::complex<T> & {
    System.Numerics.Complex ret = $imcall;$excode
    return ret;
  }

%typemap(csvarin, excode=SWIGEXCODE2) const std::complex<T> & %{
    set {
      $imcall;$excode
    }
  %}

%typemap(csvarout, excode=SWIGEXCODE2) const std::complex<T> & %{
    get {
      System.Numerics.Complex ret = $imcall;$excode
      return ret;
    }
  %}

%template() std::complex<T>;
%enddef

// By default, typemaps for both std::complex<double> and std::complex<float>
// 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