2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-24 06:02:14 +00:00

added: converters for [plain] char and std::complex

[SVN r9397]
This commit is contained in:
Ralf W. Grosse-Kunstleve
2001-03-03 11:48:52 +00:00
parent f5fa4a460a
commit 51d60a6035
4 changed files with 127 additions and 0 deletions

View File

@@ -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>());

View File

@@ -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 (Ralf W. Grosse-Kunstleve)
#include <boost/python/conversions.hpp>
#include <typeinfo>
@@ -160,6 +163,24 @@ unsigned short from_python(PyObject* p, boost::python::type<unsigned short> type
return integer_from_python(p, type);
}
PyObject* to_python(char c)
{
if (c == '\0') return PyString_FromString("");
return PyString_FromStringAndSize(&c, 1);
}
char from_python(PyObject* p, boost::python::type<char>)
{
int l = -1;
if (PyString_Check(p)) l = PyString_Size(p);
if (l < 0 || l > 1) {
PyErr_SetString(PyExc_TypeError, "expected string of length 0 or 1");
throw boost::python::argument_error();
}
if (l == 0) return '\0';
return PyString_AsString(p)[0];
}
PyObject* to_python(unsigned char i)
{
return integer_to_python(i);

View File

@@ -815,6 +815,25 @@ namespace bpl_test {
w.set_secret_number(number);
}
// Test plain char converters.
char get_plain_char() { return 'x'; }
std::string use_plain_char(char c) { return std::string(3, c); }
std::string use_const_plain_char(const char c) { return std::string(5, c); }
// Test std::complex<double> converters.
std::complex<double> dpolar(double rho, double theta) {
return std::polar(rho, theta);
}
double dreal(const std::complex<double>& c) { return c.real(); }
double dimag(std::complex<double> c) { return c.imag(); }
// Test std::complex<float> converters.
std::complex<float> fpolar(float rho, float theta) {
return std::polar(rho, theta);
}
double freal(const std::complex<float>& c) { return c.real(); }
double fimag(std::complex<float> c) { return c.imag(); }
/************************************************************/
/* */
/* init the module */
@@ -1036,6 +1055,21 @@ void init_module(boost::python::module_builder& m)
world_class.def(world_getinitargs, "__getinitargs__");
world_class.def(world_getstate, "__getstate__");
world_class.def(world_setstate, "__setstate__");
// Test plain char converters.
m.def(get_plain_char, "get_plain_char");
m.def(use_plain_char, "use_plain_char");
m.def(use_const_plain_char, "use_const_plain_char");
// Test std::complex<double> converters.
m.def(dpolar, "dpolar");
m.def(dreal, "dreal");
m.def(dimag, "dimag");
// Test std::complex<float> converters.
m.def(fpolar, "fpolar");
m.def(freal, "freal");
m.def(fimag, "fimag");
}
PyObject* raw(const boost::python::tuple& args, const boost::python::dictionary& keywords)

View File

@@ -1070,6 +1070,39 @@ test methodologies for wrapping functions that return a pointer
3
>>> eo.second
1
======== test [plain] char converters ==============
>>> get_plain_char()
'x'
>>> use_plain_char('a')
'aaa'
>>> use_const_plain_char('b')
'bbbbb'
======== test std::complex converters ==============
>>> c = dpolar(3, 5)
>>> type(c)
<type 'complex'>
>>> '%.3g' % (dreal(c))
'0.851'
>>> '%.3g' % (dimag(c))
'-2.88'
>>> '%.3g' % (freal(c))
'0.851'
>>> '%.3g' % (fimag(c))
'-2.88'
>>> c = fpolar(7, 13)
>>> type(c)
<type 'complex'>
>>> '%.3g' % (fimag(c))
'2.94'
>>> '%.3g' % (freal(c))
'6.35'
>>> '%.3g' % (dimag(c))
'2.94'
>>> '%.3g' % (dreal(c))
'6.35'
'''
from test import *