2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-28 07:22:31 +00:00

Begin transition away from handle<>

[SVN r14602]
This commit is contained in:
Dave Abrahams
2002-07-25 16:29:30 +00:00
parent 30ef9c6418
commit ddb1236f2f
9 changed files with 77 additions and 74 deletions

View File

@@ -181,22 +181,19 @@ class class_ : public objects::class_base
template <class D>
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 <class D>
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_<T,X1,X2,X3>::class_(char const* name)
template <class T, class X1, class X2, class X3>
inline class_<T,X1,X2,X3>& class_<T,X1,X2,X3>::add_property(char const* name, handle<> const& fget)
inline class_<T,X1,X2,X3>& class_<T,X1,X2,X3>::add_property(char const* name, object const& fget)
{
base::add_property(name, fget);
return *this;
}
template <class T, class X1, class X2, class X3>
inline class_<T,X1,X2,X3>& class_<T,X1,X2,X3>::add_property(char const* name, handle<> const& fget, handle<> const& fset)
inline class_<T,X1,X2,X3>& class_<T,X1,X2,X3>::add_property(char const* name, object const& fget, object const& fset)
{
base::add_property(name, fget, fset);
return *this;

View File

@@ -13,6 +13,7 @@
# include <boost/type_traits/cv_traits.hpp>
# include <boost/python/return_value_policy.hpp>
# include <boost/python/copy_non_const_reference.hpp>
# include <boost/python/object/function_object.hpp>
namespace boost { namespace python {
@@ -61,47 +62,45 @@ namespace detail
}
template <class C, class D>
objects::function* make_getter(D C::*pm)
object make_getter(D C::*pm)
{
typedef return_value_policy<copy_non_const_reference> default_policy;
return new objects::function(
objects::py_function(
::boost::bind(
&detail::member<D,C,default_policy>::get, pm, _1, _2
, default_policy()))
return objects::function_object(
::boost::bind(
&detail::member<D,C,default_policy>::get, pm, _1, _2
, default_policy())
, 1);
}
template <class C, class D, class Policies>
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<D,C,Policies>::get, pm, _1, _2
, policies))
, policies)
, 1);
}
template <class C, class D>
objects::function* make_setter(D C::*pm)
object make_setter(D C::*pm)
{
return new objects::function(
objects::py_function(
::boost::bind(
&detail::member<D,C,default_call_policies>::set, pm, _1, _2
, default_call_policies()))
return objects::function_object(
::boost::bind(
&detail::member<D,C,default_call_policies>::set, pm, _1, _2
, default_call_policies())
, 2);
}
template <class C, class D, class Policies>
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<D,C,Policies>::set, pm, _1, _2
, policies))
return objects::function_object(
::boost::bind(
&detail::member<D,C,Policies>::set, pm, _1, _2
, policies)
, 2);
}

View File

@@ -12,6 +12,8 @@
# include <boost/type_traits/transform_traits.hpp>
# include <boost/python/detail/indirect_traits.hpp>
# include <boost/mpl/select_type.hpp>
# include <boost/python/object_core.hpp>
# include <boost/python/refcount.hpp>
namespace boost { namespace python {
@@ -28,13 +30,16 @@ namespace detail {
// object.
template <class F>
inline PyObject* wrap_function_aux(F f, PyObject*) { return f; }
inline PyObject* wrap_function_aux(F const& f, PyObject*) { return f; }
template <class F, class T>
inline PyObject* wrap_function_aux(F f, boost::python::handle<T> x) { return x.release(); }
inline PyObject* wrap_function_aux(F const&, boost::python::handle<T> x) { return x.release(); }
template <class F>
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 <class F>
inline PyObject* wrap_function_aux(F const& f, ...) { return make_function(f); }
template <class F>
PyObject* wrap_function(F f)

View File

@@ -69,43 +69,28 @@ struct iterators
// accessors. Deduce the Target type from the accessors. The iterator
// returns copies of the inderlying elements.
template <class Accessor1, class Accessor2>
handle<> range(Accessor1 start, Accessor2 finish)
object range(Accessor1 start, Accessor2 finish)
{
return handle<>(
borrowed(allow_null(
detail::make_iterator<objects::default_iterator_call_policies>(
return detail::make_iterator<objects::default_iterator_call_policies>(
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 <class NextPolicies, class Accessor1, class Accessor2>
handle<> range(Accessor1 start, Accessor2 finish, NextPolicies* = 0)
object range(Accessor1 start, Accessor2 finish, NextPolicies* = 0)
{
return handle<>(
borrowed(
allow_null(
detail::make_iterator<NextPolicies>(start, finish, detail::target(start))
.ptr()
)));
return detail::make_iterator<NextPolicies>(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 <class NextPolicies, class Target, class Accessor1, class Accessor2>
handle<> range(Accessor1 start, Accessor2 finish, NextPolicies* = 0, boost::type<Target>* = 0)
object range(Accessor1 start, Accessor2 finish, NextPolicies* = 0, boost::type<Target>* = 0)
{
typedef typename add_reference<Target>::type target;
return handle<>(
borrowed(
allow_null(
detail::make_iterator<NextPolicies, target>(start, finish)
.ptr()
)));
return detail::make_iterator<NextPolicies, target>(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 <class Container
, class NextPolicies = objects::default_iterator_call_policies>
struct iterator : handle<>
struct iterator : object
{
iterator()
: handle<>(
range<NextPolicies>(
: object(
python::range<NextPolicies>(
&iterators<Container>::begin, &iterators<Container>::end
))
{

View File

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

View File

@@ -41,10 +41,6 @@ struct BOOST_PYTHON_DECL function : PyObject
function* m_overloads;
};
//
// implementations
//
}}} // namespace boost::python::objects
#endif // FUNCTION_DWA20011214_HPP

View File

@@ -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 <boost/python/object/function.hpp>
# include <boost/python/object_core.hpp>
namespace boost { namespace python { namespace objects {
template <class F>
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

View File

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

View File

@@ -4,7 +4,7 @@
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
#include <boost/python/object/function.hpp>
#include <boost/python/object/function_object.hpp>
#include <numeric>
#include <boost/python/errors.hpp>
#include <boost/python/str.hpp>
@@ -148,10 +148,8 @@ namespace
function* not_implemented_function()
{
static function* result = new function(py_function(&not_implemented_impl), 2, 3);
static handle<> keeper(result);
return result;
static object keeper(function_object(&not_implemented_impl, 2, 3));
return (function*)keeper.ptr();
}
}