From a25021d215d3801f5c671ccdaa299d4f18bac161 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Fri, 8 Mar 2002 15:32:32 +0000 Subject: [PATCH] Initial checkin [SVN r13137] --- .../converter/rvalue_from_python_chain.hpp | 39 ++++++++++ test/callbacks.cpp | 71 +++++++++++++++++++ test/callbacks.py | 43 +++++++++++ 3 files changed, 153 insertions(+) create mode 100644 include/boost/python/converter/rvalue_from_python_chain.hpp create mode 100644 test/callbacks.cpp create mode 100644 test/callbacks.py diff --git a/include/boost/python/converter/rvalue_from_python_chain.hpp b/include/boost/python/converter/rvalue_from_python_chain.hpp new file mode 100644 index 00000000..c57ef564 --- /dev/null +++ b/include/boost/python/converter/rvalue_from_python_chain.hpp @@ -0,0 +1,39 @@ +// Copyright David Abrahams 2002. 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. +#ifndef RVALUE_FROM_PYTHON_CHAIN_DWA200237_HPP +# define RVALUE_FROM_PYTHON_CHAIN_DWA200237_HPP +# include +# include +# include + +namespace boost { namespace python { namespace converter { + +namespace detail +{ + template + struct rvalue_from_python_chain_impl + { + static rvalue_from_python_registration*const& value; + }; + + template + rvalue_from_python_registration*const& rvalue_from_python_chain_impl::value + = registry::rvalue_converters(undecorated_type_id()); +} + +template +struct rvalue_from_python_chain + : detail::rvalue_from_python_chain_impl< + typename add_reference< + typename add_cv::type + >::type + > +{ +}; + +}}} // namespace boost::python::converter + +#endif // RVALUE_FROM_PYTHON_CHAIN_DWA200237_HPP diff --git a/test/callbacks.cpp b/test/callbacks.cpp new file mode 100644 index 00000000..9991e41f --- /dev/null +++ b/test/callbacks.cpp @@ -0,0 +1,71 @@ +// Copyright David Abrahams 2002. 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. +#include +#include +#include +#include + +using namespace boost::python; + +int apply_int_int(PyObject* f, int x) +{ + return returning::call(f, x); +} + +void apply_void_int(PyObject* f, int x) +{ + returning::call(f, x); +} + +struct X +{ + explicit X(int x) : x(x), magic(7654321) { ++counter; } + X(X const& rhs) : x(rhs.x), magic(7654321) { ++counter; } + ~X() { assert(magic == 7654321); magic = 6666666; x = 9999; --counter; } + + void set(int x) { assert(magic == 7654321); this->x = x; } + int value() const { assert(magic == 7654321); return x; } + static int count() { return counter; } + private: + void operator=(X const&); + private: + int x; + long magic; + static int counter; +}; + +X apply_X_X(PyObject* f, X x) +{ + return returning::call(f, x); +} + +void apply_void_X_ref(PyObject* f, X x) +{ + returning::call(f, boost::ref(x)); +} + +int X::counter; + +BOOST_PYTHON_MODULE_INIT(callbacks_ext) +{ + boost::python::module("callbacks_ext") + .def("apply_int_int", apply_int_int) + .def("apply_void_int", apply_void_int) + .def("apply_X_X", apply_X_X) + .def("apply_void_X_ref", apply_void_X_ref) + .add( + class_("X") + .def_init(args()) + .def_init(args()) + .def("value", &X::value) + .def("set", &X::set) + ) + .def("x_count", &X::count) + ; +} + + + diff --git a/test/callbacks.py b/test/callbacks.py new file mode 100644 index 00000000..16c5be94 --- /dev/null +++ b/test/callbacks.py @@ -0,0 +1,43 @@ +''' +>>> from callbacks_ext import * + +>>> def double(x): +... return x + x +... +>>> apply_int_int(double, 42) +84 +>>> apply_void_int(double, 42) + +>>> def identity(x): +... return x + +>>> x = apply_X_X(identity, X(42)) +>>> x.value() +42 +>>> x_count() +1 +>>> del x +>>> x_count() +0 + +>>> def increment(x): +... x.set(x.value() + 1) +... +>>> x = X(42) +>>> apply_void_X_ref(increment, x) +>>> x.value() +43 +''' + +def run(args = None): + import sys + import doctest + + if args is not None: + sys.argv = args + return doctest.testmod(sys.modules.get(__name__)) + +if __name__ == '__main__': + print "running..." + import sys + sys.exit(run()[0])