2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-22 05:22:45 +00:00

Join ralf_grosse_kunstleve with HEAD

[SVN r9444]
This commit is contained in:
Ralf W. Grosse-Kunstleve
2001-03-05 20:01:01 +00:00
parent b438293c9c
commit 7732b390b4
2 changed files with 67 additions and 22 deletions

View File

@@ -5,6 +5,11 @@
//
// The author gratefully acknowleges the support of Dragon Systems, Inc., in
// producing this work.
//
// Revision History:
// 04 Mar 01 Fixed std::complex<> stuff to work with MSVC (David Abrahams)
// 03 Mar 01 added: converters for [plain] char and std::complex
// (Ralf W. Grosse-Kunstleve)
#ifndef METHOD_DWA122899_H_
# define METHOD_DWA122899_H_
@@ -16,8 +21,18 @@
# include <boost/smart_ptr.hpp>
# include <boost/python/errors.hpp>
# include <string>
# ifdef BOOST_MSVC6_OR_EARLIER
# pragma warning(push)
# pragma warning(disable:4275) // disable a bogus warning caused by <complex>
# endif
# include <complex>
# ifdef BOOST_MSVC6_OR_EARLIER
# pragma warning(pop)
# endif
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE // this is a gcc 2.95.2 bug workaround
// This can be instantiated on an enum to provide the to_python/from_python
@@ -71,6 +86,30 @@ inline void xdecref(T* p)
xdecref_impl(reinterpret_cast<PyObject*>(p_base));
}
namespace detail {
void expect_complex(PyObject*);
template <class T>
std::complex<T> complex_from_python(PyObject* p, boost::python::type<T>)
{
expect_complex(p);
return std::complex<T>(
static_cast<T>(PyComplex_RealAsDouble(p)),
static_cast<T>(PyComplex_ImagAsDouble(p)));
}
template <class T>
PyObject* complex_to_python(const std::complex<T>& sc) {
Py_complex pcc;
pcc.real = sc.real();
pcc.imag = sc.imag();
return PyComplex_FromCComplex(pcc);
}
}
}} // namespace boost::python
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
@@ -135,30 +174,34 @@ 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);
inline PyObject* to_python(const std::complex<float>& x)
{
return boost::python::detail::complex_to_python<float>(x);
}
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)));
inline PyObject* to_python(const std::complex<double>& x)
{
return boost::python::detail::complex_to_python<double>(x);
}
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>&>());
inline std::complex<double> from_python(PyObject* p,
boost::python::type<std::complex<double> >) {
return boost::python::detail::complex_from_python(p, boost::python::type<double>());
}
inline std::complex<double> from_python(PyObject* p,
boost::python::type<const std::complex<double>&>) {
return boost::python::detail::complex_from_python(p, boost::python::type<double>());
}
inline std::complex<float> from_python(PyObject* p,
boost::python::type<std::complex<float> >) {
return boost::python::detail::complex_from_python(p, boost::python::type<float>());
}
inline std::complex<float> from_python(PyObject* p,
boost::python::type<const std::complex<float>&>) {
return boost::python::detail::complex_from_python(p, boost::python::type<float>());
}
// For when your C++ function really wants to pass/return a PyObject*

View File

@@ -5,6 +5,9 @@
//
// The author gratefully acknowleges the support of Dragon Systems, Inc., in
// producing this work.
//
// Revision History:
// Mar 01 01 Use PyObject_INIT() instead of trying to hand-initialize (David Abrahams)
#ifndef BASE_OBJECT_DWA051600_H_
# define BASE_OBJECT_DWA051600_H_
@@ -46,9 +49,8 @@ base_object<python_type>::base_object(PyTypeObject* type_obj)
std::
#endif
memset(bp, 0, sizeof(base_python_type));
ob_refcnt = 1;
ob_type = type_obj;
Py_INCREF(type_obj);
PyObject_INIT(bp, type_obj);
}
template <class python_type>