Fix long long and other warnings.

Add the macros

%typemap_asfromcheck()
%typemap_asfrom()

that can be used to defined all the different typemaps
for types where the As/From/Check methods are provided.

All the basic type (int, char,...) typemaps are implemented
using them.

The std::string and std::complex<T> are reimplemented using
the new %typemap_asfrom/check macros too. This helps to complete
all the previously missing typemaps (consttab, varin, varout,..)
and also ilustrates how to define the As/From/Check methods
to use with the %typemap_asfrom/check macros.

As a byproduct, the C complex typemap was added, and the file

complex.i

can be used to load the complex support for either C or C++. The
original C++ std_complex.i is still there, and the corresponding C
ccomplex.i too, if they need to be included explicitly.

Also, the As/From methods are declared via %fragment, so, they
can be reused as needed, and only appear in the wrapped code if
they corresponding typemap is invoked, making the typemaps
and the entire code shorter and simpler.


Marcelo.


git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@5691 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Marcelo Matus 2004-01-28 01:30:13 +00:00
commit 3f1f31d1e8
11 changed files with 647 additions and 506 deletions

View file

@ -1,108 +1,28 @@
#ifndef SWIG_STD_COMPLEX_I_
#define SWIG_STD_COMPLEX_I_
#ifdef SWIG
%{
#include <complex>
static inline int
SwigComplex_Check(PyObject *o)
{
return PyComplex_Check(o) || PyFloat_Check(o) || PyInt_Check(o) || PyLong_Check(o);
}
template <class __Complex>
__Complex
SwigComplex_As(PyObject *o)
{
if (PyComplex_Check(o)) {
return __Complex(PyComplex_RealAsDouble(o), PyComplex_ImagAsDouble(o));
} else if (PyFloat_Check(o)) {
return __Complex(PyFloat_AsDouble(o), 0);
} else if (PyInt_Check(o)) {
return __Complex(PyInt_AsLong(o), 0);
} else if (PyLong_Check(o)) {
return __Complex(PyLong_AsLongLong(o), 0);
} else {
PyErr_SetString(PyExc_TypeError, "Expecting a complex or compatible type");
return __Complex(0,0);
}
}
static inline std::complex<double>
SwigComplex_AsComplexDouble(PyObject *o)
{
return SwigComplex_As<std::complex<double> >(o);
}
#include <complex>
%}
%include "complex_common.i"
%define swig_specialize_complex(Complex)
%typecheck(SWIG_TYPECHECK_COMPLEX)
Complex, const Complex&
{
$1 = SwigComplex_Check($input);
}
%typemap(in) Complex {
$1 = SwigComplex_As< Complex >($input);
if (PyErr_Occurred()) SWIG_fail;
}
%typemap(in) const Complex& (Complex temp) {
temp = SwigComplex_As< Complex >($input);
if (PyErr_Occurred()) SWIG_fail;
$1 = &temp;
}
%typemap(out) Complex {
$result = PyComplex_FromDoubles($1.real(), $1.imag());
}
%typemap(out) const Complex & {
$result = PyComplex_FromDoubles($1->real(), $1->imag());
}
// C++ proxy class typemaps
%typemap(directorin) Complex {
$input = PyComplex_FromDoubles($1_name.real(), $1_name.imag());
}
%typemap(directorin) const Complex & {
$input = PyComplex_FromDoubles($1_name.real(), $1_name.imag());
}
%typemap(directorout) Complex {
$result = SwigComplex_As< Complex >($input);
if (PyErr_Occurred()) {
throw Swig::DirectorTypeMismatchException("Expecting a complex or compatible type");
}
}
%typemap(directorout) const Complex & (Complex temp) {
temp = SwigComplex_As< Complex >($input);
if (PyErr_Occurred()) {
throw Swig::DirectorTypeMismatchException("Expecting a complex or compatible type");
}
$result = &temp;
}
/* defining the complex as/from converters */
%enddef
namespace std
{
template <class T> class complex;
}
%swig_cplxdbl_conv(std::complex<double>, StdCplxDbl,
std::complex<double>, std::real, std::imag)
%swig_cplxflt_conv(std::complex<float>, StdCplxFlt,
std::complex<float>, std::real, std::imag)
/* declaring the typemaps */
%typemap_asfrom(std::complex<double>, CPLXDBL,
SWIG_PyObj_AsStdCplxDbl, SWIG_PyObj_FromStdCplxDbl);
%typemap_asfrom(std::complex<float>, CPLXFLT,
SWIG_PyObj_AsStdCplxFlt, SWIG_PyObj_FromStdCplxFlt);
swig_specialize_complex(std::complex<float>);
swig_specialize_complex(std::complex<double>);
#endif // SWIG
#endif //SWIG_STD_COMPLEX_I_