From ddb1236f2f5fc9db8456ed696c1a70210744252e Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Thu, 25 Jul 2002 16:29:30 +0000 Subject: [PATCH] Begin transition away from handle<> [SVN r14602] --- include/boost/python/class.hpp | 15 +++---- include/boost/python/data_members.hpp | 43 +++++++++---------- include/boost/python/detail/wrap_function.hpp | 11 +++-- include/boost/python/iterator.hpp | 35 +++++---------- include/boost/python/object/class.hpp | 4 +- include/boost/python/object/function.hpp | 4 -- .../boost/python/object/function_object.hpp | 23 ++++++++++ src/object/class.cpp | 8 ++-- src/object/function.cpp | 8 ++-- 9 files changed, 77 insertions(+), 74 deletions(-) create mode 100644 include/boost/python/object/function_object.hpp diff --git a/include/boost/python/class.hpp b/include/boost/python/class.hpp index 2e59189e..b2d960b0 100644 --- a/include/boost/python/class.hpp +++ b/include/boost/python/class.hpp @@ -181,22 +181,19 @@ class class_ : public objects::class_base template self& def_readonly(char const* name, D T::*pm) { - handle<> fget(make_getter(pm)); - this->add_property(name, fget); + this->add_property(name, make_getter(pm)); return *this; } template self& def_readwrite(char const* name, D T::*pm) { - handle<> fget(make_getter(pm)); - handle<> fset(make_setter(pm)); - return this->add_property(name, fget, fset); + return this->add_property(name, make_getter(pm), make_setter(pm)); } // Property creation - self& add_property(char const* name, handle<> const& fget); - self& add_property(char const* name, handle<> const& fget, handle<> const& fset); + self& add_property(char const* name, object const& fget); + self& add_property(char const* name, object const& fget, object const& fset); self& setattr(char const* name, handle<> const&); @@ -277,14 +274,14 @@ inline class_::class_(char const* name) template -inline class_& class_::add_property(char const* name, handle<> const& fget) +inline class_& class_::add_property(char const* name, object const& fget) { base::add_property(name, fget); return *this; } template -inline class_& class_::add_property(char const* name, handle<> const& fget, handle<> const& fset) +inline class_& class_::add_property(char const* name, object const& fget, object const& fset) { base::add_property(name, fget, fset); return *this; diff --git a/include/boost/python/data_members.hpp b/include/boost/python/data_members.hpp index b9114403..f186c1d0 100644 --- a/include/boost/python/data_members.hpp +++ b/include/boost/python/data_members.hpp @@ -13,6 +13,7 @@ # include # include # include +# include namespace boost { namespace python { @@ -61,47 +62,45 @@ namespace detail } template -objects::function* make_getter(D C::*pm) +object 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())) + + return objects::function_object( + ::boost::bind( + &detail::member::get, pm, _1, _2 + , default_policy()) , 1); + } template -objects::function* make_getter(D C::*pm, Policies const& policies) +object make_getter(D C::*pm, Policies const& policies) { - return new objects::function( - objects::py_function( + return objects::function_object( ::boost::bind( &detail::member::get, pm, _1, _2 - , policies)) + , policies) , 1); } template -objects::function* make_setter(D C::*pm) +object make_setter(D C::*pm) { - return new objects::function( - objects::py_function( - ::boost::bind( - &detail::member::set, pm, _1, _2 - , default_call_policies())) + return objects::function_object( + ::boost::bind( + &detail::member::set, pm, _1, _2 + , default_call_policies()) , 2); } template -objects::function* make_setter(D C::*pm, Policies const& policies) +object make_setter(D C::*pm, Policies const& policies) { - return new objects::function( - objects::py_function( - ::boost::bind( - &detail::member::set, pm, _1, _2 - , policies)) + return objects::function_object( + ::boost::bind( + &detail::member::set, pm, _1, _2 + , policies) , 2); } diff --git a/include/boost/python/detail/wrap_function.hpp b/include/boost/python/detail/wrap_function.hpp index 71ee967e..dcc3daca 100644 --- a/include/boost/python/detail/wrap_function.hpp +++ b/include/boost/python/detail/wrap_function.hpp @@ -12,6 +12,8 @@ # include # include # include +# include +# include namespace boost { namespace python { @@ -28,13 +30,16 @@ namespace detail { // object. template -inline PyObject* wrap_function_aux(F f, PyObject*) { return f; } +inline PyObject* wrap_function_aux(F const& f, PyObject*) { return f; } template -inline PyObject* wrap_function_aux(F f, boost::python::handle x) { return x.release(); } +inline PyObject* wrap_function_aux(F const&, boost::python::handle x) { return x.release(); } template -inline PyObject* wrap_function_aux(F f, ...) { return make_function(f); } +inline PyObject* wrap_function_aux(F const&, object const& x) { return python::incref(x.ptr()); } + +template +inline PyObject* wrap_function_aux(F const& f, ...) { return make_function(f); } template PyObject* wrap_function(F f) diff --git a/include/boost/python/iterator.hpp b/include/boost/python/iterator.hpp index 673d26e6..3f477034 100644 --- a/include/boost/python/iterator.hpp +++ b/include/boost/python/iterator.hpp @@ -69,43 +69,28 @@ struct iterators // accessors. Deduce the Target type from the accessors. The iterator // returns copies of the inderlying elements. template -handle<> range(Accessor1 start, Accessor2 finish) +object range(Accessor1 start, Accessor2 finish) { - return handle<>( - borrowed(allow_null( - detail::make_iterator( + return detail::make_iterator( start, finish - , detail::target(start)) - .ptr() - )) - ); + , detail::target(start)); } // Create an iterator-building function which uses the given accessors // and next() policies. Deduce the Target type. template -handle<> range(Accessor1 start, Accessor2 finish, NextPolicies* = 0) +object range(Accessor1 start, Accessor2 finish, NextPolicies* = 0) { - return handle<>( - borrowed( - allow_null( - detail::make_iterator(start, finish, detail::target(start)) - .ptr() - ))); + return detail::make_iterator(start, finish, detail::target(start)); } // Create an iterator-building function which uses the given accessors // and next() policies, operating on the given Target type template -handle<> range(Accessor1 start, Accessor2 finish, NextPolicies* = 0, boost::type* = 0) +object range(Accessor1 start, Accessor2 finish, NextPolicies* = 0, boost::type* = 0) { typedef typename add_reference::type target; - return handle<>( - borrowed( - allow_null( - detail::make_iterator(start, finish) - .ptr() - ))); + return detail::make_iterator(start, finish); } // A Python callable object which produces an iterator traversing @@ -114,11 +99,11 @@ handle<> range(Accessor1 start, Accessor2 finish, NextPolicies* = 0, boost::type // next() function. template -struct iterator : handle<> +struct iterator : object { iterator() - : handle<>( - range( + : object( + python::range( &iterators::begin, &iterators::end )) { diff --git a/include/boost/python/object/class.hpp b/include/boost/python/object/class.hpp index babd409a..1367337b 100644 --- a/include/boost/python/object/class.hpp +++ b/include/boost/python/object/class.hpp @@ -36,8 +36,8 @@ struct BOOST_PYTHON_DECL class_base : python::api::object ); // Retrieve the underlying object - void add_property(char const* name, handle<> const& fget); - void add_property(char const* name, handle<> const& fget, handle<> const& fset); + void add_property(char const* name, object const& fget); + void add_property(char const* name, object const& fget, object const& fset); void setattr(char const* name, handle<> const&); void enable_pickling(bool getstate_manages_dict); }; diff --git a/include/boost/python/object/function.hpp b/include/boost/python/object/function.hpp index ebd2a8c3..80a50dd0 100644 --- a/include/boost/python/object/function.hpp +++ b/include/boost/python/object/function.hpp @@ -41,10 +41,6 @@ struct BOOST_PYTHON_DECL function : PyObject function* m_overloads; }; -// -// implementations -// - }}} // namespace boost::python::objects #endif // FUNCTION_DWA20011214_HPP diff --git a/include/boost/python/object/function_object.hpp b/include/boost/python/object/function_object.hpp new file mode 100644 index 00000000..b638ac01 --- /dev/null +++ b/include/boost/python/object/function_object.hpp @@ -0,0 +1,23 @@ +// 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 FUNCTION_OBJECT_DWA2002725_HPP +# define FUNCTION_OBJECT_DWA2002725_HPP +# include +# include + +namespace boost { namespace python { namespace objects { + +template +inline object function_object(F const& f, unsigned min_args, unsigned max_args = 0) +{ + return python::object( + python::detail::new_non_null_reference( + new function(objects::py_function(f), min_args, max_args))); +} + +}}} // namespace boost::python::object + +#endif // FUNCTION_OBJECT_DWA2002725_HPP diff --git a/src/object/class.cpp b/src/object/class.cpp index def44c33..b3c6c5e0 100644 --- a/src/object/class.cpp +++ b/src/object/class.cpp @@ -296,15 +296,15 @@ namespace objects extern DL_IMPORT(PyTypeObject) PyProperty_Type; } - void class_base::add_property(char const* name, handle<> const& fget) + void class_base::add_property(char const* name, object const& fget) { - handle<> property(PyObject_CallFunction((PyObject*)&PyProperty_Type, "O", fget.get())); + handle<> property(PyObject_CallFunction((PyObject*)&PyProperty_Type, "O", fget.ptr())); setattr(name, property); } - void class_base::add_property(char const* name, handle<> const& fget, handle<> const& fset) + void class_base::add_property(char const* name, object const& fget, object const& fset) { - handle<> property(PyObject_CallFunction((PyObject*)&PyProperty_Type, "OO", fget.get(), fset.get())); + handle<> property(PyObject_CallFunction((PyObject*)&PyProperty_Type, "OO", fget.ptr(), fset.ptr())); setattr(name, property); } diff --git a/src/object/function.cpp b/src/object/function.cpp index dad36dd5..53cad982 100644 --- a/src/object/function.cpp +++ b/src/object/function.cpp @@ -4,7 +4,7 @@ // "as is" without express or implied warranty, and with no claim as // to its suitability for any purpose. -#include +#include #include #include #include @@ -148,10 +148,8 @@ namespace function* not_implemented_function() { - static function* result = new function(py_function(¬_implemented_impl), 2, 3); - static handle<> keeper(result); - - return result; + static object keeper(function_object(¬_implemented_impl, 2, 3)); + return (function*)keeper.ptr(); } }