Home | History | Annotate | Download | only in ocaml
      1 // -*- C++ -*-
      2 #ifndef SWIG_STD_COMPLEX_I_
      3 #define SWIG_STD_COMPLEX_I_
      4 
      5 #ifdef SWIG
      6 
      7 %{
      8 #include <complex>
      9 %}
     10 
     11 namespace std
     12 {
     13   template <class T> class complex;
     14 
     15   %define specialize_std_complex(T)
     16 
     17   %typemap(in) complex<T> {
     18     if (PyComplex_Check($input)) {
     19       $1 = std::complex<T>(PyComplex_RealAsDouble($input),
     20 			   PyComplex_ImagAsDouble($input));
     21     } else if (PyFloat_Check($input)) {
     22       $1 = std::complex<T>(PyFloat_AsDouble($input), 0);
     23     } else if (PyInt_Check($input)) {
     24       $1 = std::complex<T>(PyInt_AsLong($input), 0);
     25     }
     26     else {
     27       PyErr_SetString(PyExc_TypeError,"Expected a complex");
     28       SWIG_fail;
     29     }
     30   }
     31 
     32   %typemap(in) const complex<T>& (std::complex<T> temp) {
     33     if (PyComplex_Check($input)) {
     34       temp = std::complex<T>(PyComplex_RealAsDouble($input),
     35 			     PyComplex_ImagAsDouble($input));
     36       $1 = &temp;
     37     } else if (PyFloat_Check($input)) {
     38       temp = std::complex<T>(PyFloat_AsDouble($input), 0);
     39       $1 = &temp;
     40     } else if (PyInt_Check($input)) {
     41       temp = std::complex<T>(PyInt_AsLong($input), 0);
     42       $1 = &temp;
     43     } else {
     44       PyErr_SetString(PyExc_TypeError,"Expected a complex");
     45       SWIG_fail;
     46     }
     47   }
     48 
     49   %typemap(out) complex<T> {
     50     $result = PyComplex_FromDoubles($1.real(), $1.imag());
     51   }
     52 
     53   %typemap(out) const complex<T> & {
     54     $result = PyComplex_FromDoubles($1->real(), $1->imag());
     55   }
     56 
     57   %enddef
     58 
     59   specialize_std_complex(double);
     60   specialize_std_complex(float);
     61 }
     62 
     63 #endif // SWIG
     64 
     65 #endif //SWIG_STD_COMPLEX_I_
     66