From bc552d326cb7b81dd6b75a847d23302ddf0fc7f6 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Sat, 30 Mar 2002 01:29:31 +0000 Subject: [PATCH] initial checkin [SVN r13310] --- include/boost/python/data_members.hpp | 111 ++++++++++++++++++++++++++ test/data_members.cpp | 36 +++++++++ 2 files changed, 147 insertions(+) create mode 100644 include/boost/python/data_members.hpp create mode 100644 test/data_members.cpp diff --git a/include/boost/python/data_members.hpp b/include/boost/python/data_members.hpp new file mode 100644 index 00000000..000560e3 --- /dev/null +++ b/include/boost/python/data_members.hpp @@ -0,0 +1,111 @@ +// 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 DATA_MEMBERS_DWA2002328_HPP +# define DATA_MEMBERS_DWA2002328_HPP + +# include +# include +# include +# include +# include +# include +# include + +namespace boost { namespace python { + +namespace detail +{ + template + struct member + { + static PyObject* get(Data Class::*pm, PyObject* args_, PyObject*, Policies const& policies) + { + from_python c0(PyTuple_GET_ITEM(args_, 0)); + if (!c0.convertible()) return 0; + + // find the result converter + typedef typename Policies::result_converter result_converter; + typedef typename boost::add_reference::type source; + typename mpl::apply1::type cr; + if (!cr.convertible()) return 0; + + if (!policies.precall(args_)) return 0; + + PyObject* result = cr( (c0(PyTuple_GET_ITEM(args_, 0)))->*pm ); + + return policies.postcall(args_, result); + } + + static PyObject* set(Data Class::*pm, PyObject* args_, PyObject*, Policies const& policies) + { + // check that each of the arguments is convertible + from_python c0(PyTuple_GET_ITEM(args_, 0)); + if (!c0.convertible()) return 0; + + typedef typename add_const::type target1; + typedef typename add_reference::type target; + from_python c1(PyTuple_GET_ITEM(args_, 1)); + + if (!c1.convertible()) return 0; + + if (!policies.precall(args_)) return 0; + + (c0(PyTuple_GET_ITEM(args_, 0)))->*pm = c1(PyTuple_GET_ITEM(args_, 1)); + + return policies.postcall(args_, detail::none()); + } + }; +} + +template +objects::function* make_getter(D C::*pm) +{ + typedef return_value_policy default_policy; + return new objects::function( + objects::py_function( + ::boost::bind( + &detail::member::get, pm, _1, _2 + , default_policy())) + , 1); +} + +template +objects::function* make_getter(D C::*pm, Policies const& policies) +{ + return new objects::function( + objects::py_function( + ::boost::bind( + &detail::member::get, pm, _1, _2 + , policies)) + , 1); +} + +template +objects::function* make_setter(D C::*pm) +{ + return new objects::function( + objects::py_function( + ::boost::bind( + &detail::member::set, pm, _1, _2 + , default_call_policies())) + , 1); +} + +template +objects::function* make_setter(D C::*pm, Policies const& policies) +{ + return new objects::function( + objects::py_function( + ::boost::bind( + &detail::member::set, pm, _1, _2 + , policies)) + , 1); +} + + +}} // namespace boost::python + +#endif // DATA_MEMBERS_DWA2002328_HPP diff --git a/test/data_members.cpp b/test/data_members.cpp new file mode 100644 index 00000000..4657fe6d --- /dev/null +++ b/test/data_members.cpp @@ -0,0 +1,36 @@ +// 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 "test_class.hpp" + +using namespace boost::python; + +typedef test_class<> X; + +typedef test_class<1> Y; + +BOOST_PYTHON_MODULE_INIT(data_members_ext) +{ + module("data_members_ext") + .add( + class_("X") + .def_init(args()) + .def("value", &X::value) + .def("set", &X::set) + .def_readonly("x", &X::x) + ) + .add( + class_("Y") + .def_init(args()) + .def("value", &Y::value) + .def("set", &Y::set) + .def_readwrite("x", &Y::x) + ) + ; +} + +#include "module_tail.cpp"