mirror of
https://github.com/boostorg/python.git
synced 2026-01-27 19:12:16 +00:00
added: converters for [plain] char and std::complex
[SVN r9397]
This commit is contained in:
@@ -5,6 +5,9 @@
|
||||
//
|
||||
// The author gratefully acknowleges the support of Dragon Systems, Inc., in
|
||||
// producing this work.
|
||||
//
|
||||
// Revision History:
|
||||
// Mar 03 01 added: converters for [plain] char and std::complex (Ralf W. Grosse-Kunstleve)
|
||||
|
||||
#ifndef METHOD_DWA122899_H_
|
||||
# define METHOD_DWA122899_H_
|
||||
@@ -16,6 +19,7 @@
|
||||
# include <boost/smart_ptr.hpp>
|
||||
# include <boost/python/errors.hpp>
|
||||
# include <string>
|
||||
# include <complex>
|
||||
|
||||
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE // this is a gcc 2.95.2 bug workaround
|
||||
|
||||
@@ -100,6 +104,10 @@ PyObject* to_python(unsigned short);
|
||||
unsigned short from_python(PyObject*, boost::python::type<unsigned short>);
|
||||
unsigned short from_python(PyObject*, boost::python::type<const unsigned short&>);
|
||||
|
||||
PyObject* to_python(char);
|
||||
char from_python(PyObject*, boost::python::type<char>);
|
||||
char from_python(PyObject*, boost::python::type<const char&>);
|
||||
|
||||
PyObject* to_python(signed char);
|
||||
signed char from_python(PyObject*, boost::python::type<signed char>);
|
||||
signed char from_python(PyObject*, boost::python::type<const signed char&>);
|
||||
@@ -130,6 +138,32 @@ PyObject* to_python(const std::string& s);
|
||||
std::string from_python(PyObject*, boost::python::type<std::string>);
|
||||
std::string from_python(PyObject*, boost::python::type<const std::string&>);
|
||||
|
||||
template <class T>
|
||||
PyObject* to_python(const std::complex<T>& sc) {
|
||||
Py_complex pcc;
|
||||
pcc.real = sc.real();
|
||||
pcc.imag = sc.imag();
|
||||
return PyComplex_FromCComplex(pcc);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
std::complex<T> from_python(PyObject* p,
|
||||
boost::python::type<const std::complex<T>&>) {
|
||||
if (! PyComplex_Check(p)) {
|
||||
PyErr_SetString(PyExc_TypeError, "expected a complex number");
|
||||
throw boost::python::argument_error();
|
||||
}
|
||||
return std::complex<T>(
|
||||
static_cast<T>(PyComplex_RealAsDouble(p)),
|
||||
static_cast<T>(PyComplex_ImagAsDouble(p)));
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline std::complex<T> from_python(PyObject* p,
|
||||
boost::python::type<std::complex<T> >) {
|
||||
return from_python(p, boost::python::type<const std::complex<T>&>());
|
||||
}
|
||||
|
||||
// For when your C++ function really wants to pass/return a PyObject*
|
||||
PyObject* to_python(PyObject*);
|
||||
PyObject* from_python(PyObject*, boost::python::type<PyObject*>);
|
||||
@@ -304,6 +338,11 @@ inline unsigned short from_python(PyObject* p, boost::python::type<const unsigne
|
||||
return from_python(p, boost::python::type<unsigned short>());
|
||||
}
|
||||
|
||||
inline char from_python(PyObject* p, boost::python::type<const char&>)
|
||||
{
|
||||
return from_python(p, boost::python::type<char>());
|
||||
}
|
||||
|
||||
inline signed char from_python(PyObject* p, boost::python::type<const signed char&>)
|
||||
{
|
||||
return from_python(p, boost::python::type<signed char>());
|
||||
|
||||
Reference in New Issue
Block a user