2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-21 05:02:17 +00:00
Files
python/test/m2.cpp
Dave Abrahams 98c9e67625 Fixed mistaken "C" linkage
[SVN r12268]
2002-01-10 13:59:14 +00:00

87 lines
2.4 KiB
C++

// Copyright David Abrahams 2001. Permission to copy, use,
// modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
// This module exercises the converters exposed in m1 at a low level
// by exposing raw Python extension functions that use wrap<> and
// unwrap<> objects.
#include <boost/python/convert.hpp>
#include <boost/python/module.hpp>
#include "simple_type.hpp"
using boost::python::wrap;
using boost::python::unwrap;
// Get a simple (by value) from the argument, and return the
// string it holds.
PyObject* unwrap_simple(simple x)
{
return PyString_FromString(x.s);
}
// Likewise, but demands that its possible to get a non-const
// reference to the simple.
PyObject* unwrap_simple_ref(simple& x)
{
return PyString_FromString(x.s);
}
// Likewise, with a const reference to the simple object.
PyObject* unwrap_simple_const_ref(simple const& x)
{
return PyString_FromString(x.s);
}
// Get an int (by value) from the argument, and convert it to a
// Python Int.
PyObject* unwrap_int(int x)
{
return PyInt_FromLong(x);
}
// Get a non-const reference to an int from the argument
PyObject* unwrap_int_ref(int& x)
{
return PyInt_FromLong(x);
}
// Get a const reference to an int from the argument.
PyObject* unwrap_int_const_ref(int const& x)
{
return PyInt_FromLong(x);
}
// MSVC6 bug workaround
template <class T> struct xxxx;
// rewrap<T> extracts a T from the argument, then converts the T back
// to a PyObject* and returns it.
template <class T>
struct rewrap
{
static T f(T x) { return x; }
};
BOOST_PYTHON_MODULE_INIT(m2)
{
boost::python::module m2("m2");
m2.def(unwrap_int, "unwrap_int");
m2.def(unwrap_int_ref, "unwrap_int_ref");
m2.def(unwrap_int_const_ref, "unwrap_int_const_ref");
m2.def(unwrap_simple, "unwrap_simple");
m2.def(unwrap_simple_ref, "unwrap_simple_ref");
m2.def(unwrap_simple_const_ref, "unwrap_simple_const_ref");
m2.def(&rewrap<int>::f, "wrap_int");
m2.def(&rewrap<int&>::f, "wrap_int_ref");
m2.def(&rewrap<int const&>::f, "wrap_int_const_ref");
m2.def(&rewrap<simple>::f, "wrap_simple");
m2.def(&rewrap<simple&>::f, "wrap_simple_ref");
m2.def(&rewrap<simple const&>::f, "wrap_simple_const_ref");
}
#include "module_tail.cpp"