2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-24 06:02:14 +00:00

reference<> => handle<>

[SVN r14136]
This commit is contained in:
Dave Abrahams
2002-06-12 21:59:17 +00:00
parent 0d58869d6e
commit 366ee6d24b
36 changed files with 1264 additions and 293 deletions

View File

@@ -6,7 +6,7 @@
#ifndef BACK_REFERENCE_DWA2002510_HPP
# define BACK_REFERENCE_DWA2002510_HPP
# include <boost/python/reference.hpp>
# include <boost/python/handle.hpp>
namespace boost { namespace python {
@@ -17,10 +17,10 @@ struct back_reference
typedef T type;
back_reference(PyObject*, T);
ref reference() const;
handle<> reference() const;
T get() const;
private:
ref m_reference;
handle<> m_reference;
T m_value;
};
@@ -75,13 +75,13 @@ class is_back_reference
//
template <class T>
back_reference<T>::back_reference(PyObject* p, T x)
: m_reference(p, ref::increment_count)
: m_reference(python::borrow(p))
, m_value(x)
{
}
template <class T>
ref back_reference<T>::reference() const
handle<> back_reference<T>::reference() const
{
return m_reference;
}

96
include/boost/python/cast.hpp Executable file
View File

@@ -0,0 +1,96 @@
// 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 CAST_DWA200269_HPP
# define CAST_DWA200269_HPP
# include <boost/python/detail/wrap_python.hpp>
# include <boost/type_traits/same_traits.hpp>
# include <boost/type.hpp>
namespace boost { namespace python {
template <class T> struct base_type_traits;
template <>
struct base_type_traits<PyObject>
{
typedef PyObject type;
};
template <>
struct base_type_traits<PyTypeObject>
{
typedef PyObject type;
};
namespace detail
{
typedef char* yes_convertible;
typedef int* no_convertible;
typedef char* yes_python_object;
typedef int* no_python_object;
template <class Target>
struct convertible
{
static inline yes_convertible check(Target) { return 0; }
static inline no_convertible check(...) { return 0; }
};
template <class Target>
inline Target* upcast(Target* p, yes_convertible)
{
return p;
}
template <class Target, class Source>
inline Target* upcast(Source* p, no_convertible, boost::type<Target>* = 0)
{
typedef typename base_type_traits<Source>::type base;
return detail::upcast<Target>((base*)p, convertible<Target*>::check((base*)0));
}
template <class Target, class Source>
inline Target* downcast(Source* p, yes_convertible)
{
return static_cast<Target*>(p);
}
template <class Target, class Source>
inline Target* downcast(Source* p, no_convertible, boost::type<Target>* = 0)
{
typedef typename base_type_traits<Source>::type base;
return (Target*)detail::downcast<base>(p, convertible<Source*>::check((base*)0));
}
template <class T>
inline void assert_castable(boost::type<T>* = 0)
{
typedef char must_be_a_complete_type[sizeof(T)];
}
}
template <class Target, class Source>
inline Target* upcast(Source* x, Target* = 0)
{
detail::assert_castable<Source>();
detail::assert_castable<Target>();
return detail::upcast<Target>(x, detail::convertible<Target*>::check(x));
}
template <class Target, class Source>
inline Target* downcast(Source* x, Target* = 0)
{
detail::assert_castable<Source>();
detail::assert_castable<Target>();
return detail::downcast<Target>(x, detail::convertible<Source*>::check((Target*)0));
}
}} // namespace boost::python
#endif // CAST_DWA200269_HPP

View File

@@ -9,7 +9,7 @@
# include <boost/python/class_fwd.hpp>
# include <boost/python/bases.hpp>
# include <boost/python/args.hpp>
# include <boost/python/reference.hpp>
# include <boost/python/handle.hpp>
# include <boost/python/object/class.hpp>
# include <boost/python/type_id.hpp>
# include <boost/python/detail/wrap_function.hpp>
@@ -46,14 +46,14 @@ namespace detail
// to the type of holder that must be created. The 3rd argument is a
// reference to the Python type object to be created.
template <class T, class Holder>
static inline void register_copy_constructor(mpl::bool_t<true> const&, Holder*, ref const& obj, T* = 0)
static inline void register_copy_constructor(mpl::bool_t<true> const&, Holder*, handle<> const& obj, T* = 0)
{
objects::class_wrapper<T,Holder> x(obj);
}
// Tag dispatched to have no effect.
template <class T, class Holder>
static inline void register_copy_constructor(mpl::bool_t<false> const&, Holder*, ref const&, T* = 0)
static inline void register_copy_constructor(mpl::bool_t<false> const&, Holder*, handle<> const&, T* = 0)
{
}
}
@@ -104,7 +104,7 @@ class class_ : public objects::class_base
// appropriate.
objects::function::add_to_namespace(
this->object(), name,
ref(detail::wrap_function(
handle<>(detail::wrap_function(
// This bit of nastiness casts F to a member function of T if possible.
detail::member_function_cast<T,F>::stage1(f).stage2((T*)0).stage3(f)
)));
@@ -132,7 +132,7 @@ class class_ : public objects::class_base
// appropriate.
objects::function::add_to_namespace(
this->object(), op.name(),
ref(detail::wrap_function(&op_t::template apply<T>::execute)));
handle<>(detail::wrap_function(&op_t::template apply<T>::execute)));
return *this;
}
@@ -176,7 +176,7 @@ class class_ : public objects::class_base
template <class D>
self& def_readonly(char const* name, D T::*pm)
{
ref fget(make_getter(pm));
handle<> fget(make_getter(pm));
this->add_property(name, fget);
return *this;
}
@@ -184,16 +184,16 @@ class class_ : public objects::class_base
template <class D>
self& def_readwrite(char const* name, D T::*pm)
{
ref fget(make_getter(pm));
ref fset(make_setter(pm));
handle<> fget(make_getter(pm));
handle<> fset(make_setter(pm));
return this->add_property(name, fget, fset);
}
// Property creation
self& add_property(char const* name, ref const& fget);
self& add_property(char const* name, ref const& fget, ref const& fset);
self& add_property(char const* name, handle<> const& fget);
self& add_property(char const* name, handle<> const& fget, handle<> const& fset);
self& setattr(char const* name, ref const&);
self& setattr(char const* name, handle<> const&);
private: // types
typedef objects::class_id class_id;
@@ -258,21 +258,21 @@ 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, ref const& fget)
inline class_<T,X1,X2,X3>& class_<T,X1,X2,X3>::add_property(char const* name, handle<> 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, ref const& fget, ref const& fset)
inline class_<T,X1,X2,X3>& class_<T,X1,X2,X3>::add_property(char const* name, handle<> const& fget, handle<> const& fset)
{
base::add_property(name, fget, fset);
return *this;
}
template <class T, class X1, class X2, class X3>
inline class_<T,X1,X2,X3>& class_<T,X1,X2,X3>::setattr(char const* name, ref const& x)
inline class_<T,X1,X2,X3>& class_<T,X1,X2,X3>::setattr(char const* name, handle<> const& x)
{
base::setattr(name, x);
return *this;

View File

@@ -11,6 +11,8 @@
# include <boost/python/converter/pointee_to_python_function.hpp>
# include <boost/python/converter/arg_to_python_base.hpp>
# include <boost/python/to_python_indirect.hpp>
// Bring in specializations
# include <boost/python/converter/builtin_converters.hpp>
namespace boost { namespace python { namespace converter {
@@ -19,7 +21,7 @@ namespace detail
BOOST_PYTHON_DECL void throw_no_class_registered();
template <class T>
struct reference_arg_to_python : arg_to_python_holder
struct reference_arg_to_python : handle<>
{
reference_arg_to_python(T& x);
private:
@@ -41,7 +43,7 @@ namespace detail
};
template <class Ptr>
struct pointer_shallow_arg_to_python : arg_to_python_holder
struct pointer_shallow_arg_to_python : handle<>
{
// Throw an exception if the conversion can't succeed
pointer_shallow_arg_to_python(Ptr);
@@ -129,13 +131,13 @@ namespace detail
template <class T>
inline reference_arg_to_python<T>::reference_arg_to_python(T& x)
: arg_to_python_holder(get_object(x))
: handle<>(get_object(x))
{
}
template <class Ptr>
inline pointer_shallow_arg_to_python<Ptr>::pointer_shallow_arg_to_python(Ptr x)
: arg_to_python_holder(get_object(x))
: handle<>(get_object(x))
{}
template <class Ptr>

View File

@@ -7,45 +7,16 @@
# define ARG_TO_PYTHON_BASE_DWA200237_HPP
# include <boost/python/converter/to_python_function_type.hpp>
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/reference.hpp>
# include <boost/python/handle.hpp>
namespace boost { namespace python { namespace converter {
namespace detail
{
struct arg_to_python_holder
{
arg_to_python_holder(PyObject* obj);
PyObject* get() const;
PyObject* get_incref() const;
private:
ref m_held;
};
struct BOOST_PYTHON_DECL arg_to_python_base : arg_to_python_holder
struct BOOST_PYTHON_DECL arg_to_python_base : handle<>
{
arg_to_python_base(void const volatile* source, to_python_function_t);
};
//
// implmentation
//
inline arg_to_python_holder::arg_to_python_holder(PyObject* obj)
: m_held(obj)
{
}
inline PyObject* arg_to_python_holder::get() const
{
return m_held.get();
}
inline PyObject* arg_to_python_holder::get_incref() const
{
PyObject* result = m_held.get();
Py_XINCREF(result);
return result;
}
}
}}} // namespace boost::python::converter

View File

@@ -7,8 +7,7 @@
# define BUILTIN_CONVERTERS_DWA2002124_HPP
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/detail/none.hpp>
# include <boost/python/reference.hpp>
# include <boost/python/converter/arg_to_python_base.hpp>
# include <boost/python/handle.hpp>
# include <string>
# include <complex>
@@ -65,10 +64,10 @@ namespace detail
namespace converter \
{ \
template <> struct arg_to_python< T > \
: detail::arg_to_python_holder \
: handle<> \
{ \
arg_to_python(T const& x) \
: detail::arg_to_python_holder(expr) {} \
: python::handle<>(expr) {} \
}; \
}

View File

@@ -1,53 +0,0 @@
// 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 CALLBACK_TO_PYTHON_BASE_DWA200237_HPP
# define CALLBACK_TO_PYTHON_BASE_DWA200237_HPP
# include <boost/python/converter/to_python_function_type.hpp>
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/reference.hpp>
namespace boost { namespace python { namespace converter {
namespace detail
{
struct callback_to_python_holder
{
callback_to_python_holder(PyObject* obj);
PyObject* get() const;
PyObject* get_incref() const;
private:
ref m_held;
};
struct BOOST_PYTHON_DECL callback_to_python_base : callback_to_python_holder
{
callback_to_python_base(void const volatile* source, to_python_function_t);
};
//
// implmentation
//
inline callback_to_python_holder::callback_to_python_holder(PyObject* obj)
: m_held(obj)
{
}
inline PyObject* callback_to_python_holder::get() const
{
return m_held.get();
}
inline PyObject* callback_to_python_holder::get_incref() const
{
PyObject* result = m_held.get();
Py_XINCREF(result);
return result;
}
}
}}} // namespace boost::python::converter
#endif // CALLBACK_TO_PYTHON_BASE_DWA200237_HPP

View File

@@ -6,7 +6,7 @@
#ifndef MODULE_BASE_DWA2002227_HPP
# define MODULE_BASE_DWA2002227_HPP
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/reference.hpp>
# include <boost/python/handle.hpp>
namespace boost { namespace python { namespace detail {
@@ -19,25 +19,24 @@ class BOOST_PYTHON_DECL module_base
// Add elements to the module
void setattr(const char* name, PyObject*);
void setattr(const char* name, ref const&);
void add(PyTypeObject* x); // just use the type's name
void add_type(ref);
void setattr(const char* name, handle<> const&);
void add(type_handle const&); // just use the type's name
// Return a reference to the Python module object being built
inline ref object() const;
inline handle<> object() const;
protected:
void add_class(ref const& class_obj);
void add_class(type_handle const& class_obj);
private:
ref m_module;
handle<> m_module;
static PyMethodDef initial_methods[1];
};
//
// inline implementations
//
inline ref module_base::object() const
inline handle<> module_base::object() const
{
return m_module;
}

View File

@@ -31,7 +31,7 @@ template <class F>
inline PyObject* wrap_function_aux(F f, PyObject*) { return f; }
template <class F, class T>
inline PyObject* wrap_function_aux(F f, boost::python::reference<T> x) { return x.release(); }
inline PyObject* wrap_function_aux(F f, boost::python::handle<T> x) { return x.release(); }
template <class F>
inline PyObject* wrap_function_aux(F f, ...) { return make_function(f); }

View File

@@ -1,53 +0,0 @@
#error obsolete
// 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 FROM_PYTHON_DWA2002128_HPP
# define FROM_PYTHON_DWA2002128_HPP
# include <boost/python/converter/from_python.hpp>
namespace boost { namespace python {
template <class T>
struct from_python
: converter::select_arg_from_python<T>::type
{
typedef typename converter::select_from_python<T>::type base;
from_python(PyObject*);
};
// specialization for PyObject*
template <>
struct from_python<PyObject*>
{
typedef PyObject* result_type;
from_python(PyObject*) {}
bool convertible() const { return true; }
PyObject* operator()(PyObject* source) const { return source; }
};
template <>
struct from_python<PyObject* const&>
{
typedef PyObject* const& result_type;
from_python(PyObject*) {}
bool convertible() const { return true; }
PyObject*const& operator()(PyObject*const& source) const { return source; }
};
//
// implementations
//
template <class T>
inline from_python<T>::from_python(PyObject* source)
: base(source)
{
}
}} // namespace boost::python
#endif // FROM_PYTHON_DWA2002128_HPP

223
include/boost/python/handle.hpp Executable file
View File

@@ -0,0 +1,223 @@
// 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 HANDLE_DWA200269_HPP
# define HANDLE_DWA200269_HPP
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/cast.hpp>
# include <boost/python/errors.hpp>
namespace boost { namespace python {
template <class T>
inline T* incref(T* p)
{
Py_INCREF(python::upcast<PyObject>(p));
return p;
}
template <class T>
inline T* xincref(T* p)
{
Py_XINCREF(python::upcast<PyObject>(p));
return p;
}
template <class T>
inline void decref(T* p)
{
Py_DECREF(python::upcast<PyObject>(p));
}
template <class T>
inline void xdecref(T* p)
{
Py_XDECREF(python::upcast<PyObject>(p));
}
template <class T> struct borrowed;
template <class T> struct null_ok;
template <class T>
inline borrowed<T>* borrow(T* p)
{
return (borrowed<T>*)p;
}
template <class T>
inline null_ok<T>* allow_null(T* p)
{
return (null_ok<T>*)p;
}
namespace detail
{
template <class T>
inline T* manage_ptr(borrowed<null_ok<T> >* p, int)
{
return python::xincref((T*)p);
}
template <class T>
inline T* manage_ptr(null_ok<borrowed<T> >* p, int)
{
return python::xincref((T*)p);
}
template <class T>
inline T* manage_ptr(borrowed<T>* p, long)
{
return python::incref(expect_non_null((T*)p));
}
template <class T>
inline T* manage_ptr(null_ok<T>* p, long)
{
return (T*)p;
}
template <class T>
inline T* manage_ptr(T* p, ...)
{
return expect_non_null(p);
}
#if 0
template <class T>
struct handle_proxy
: handle_proxy<typename base_type_traits<T>::type>
{
typedef typename base_type_traits<T>::type base_t;
handle_proxy(PyObject* p)
: handle_proxy<base_t>(p)
{}
operator T*() const { return python::downcast<T>(m_p); }
};
template <>
struct handle_proxy<PyObject>
{
handle_proxy(PyObject* p) : m_p(p) {}
operator PyObject*() const { return (PyObject*)m_p; }
private:
PyObject* m_p;
};
#endif
}
template <class T = PyObject>
class handle
{
typedef T* (handle::*bool_type);
public:
handle();
~handle();
template <class Y>
handle(Y* p)
: m_p(
python::upcast<T>(
detail::manage_ptr(p, 0)
)
)
{
}
handle& operator=(handle const& r);
#if !defined(BOOST_MSVC) || (BOOST_MSVC > 1200)
template<typename Y>
handle& operator=(handle<Y> const & r) // never throws
{
python::xdecref(m_p);
m_p = python::xincref(python::upcast<T>(r.get()));
return *this;
}
#endif
template <typename Y>
handle(handle<Y> const& r)
: m_p(python::xincref(python::upcast<T>(r.get())))
{
}
handle(handle const& r)
: m_p(python::xincref(r.m_p))
{
}
T* operator-> () const;
T* get() const;
T* release();
operator bool_type() const // never throws
{
return m_p ? &handle<T>::m_p : 0;
}
bool operator! () const; // never throws
private: // data members
T* m_p;
};
typedef handle<PyTypeObject> type_handle;
//
// implementations
//
template <class T>
inline handle<T>::handle()
: m_p(0)
{
}
template <class T>
inline handle<T>::~handle()
{
python::xdecref(m_p);
}
template <class T>
inline handle<T>& handle<T>::operator=(handle<T> const& r)
{
python::xdecref(m_p);
m_p = python::xincref(r.m_p);
return *this;
}
template <class T>
inline T* handle<T>::operator->() const
{
return m_p;
}
template <class T>
inline T* handle<T>::get() const
{
return m_p;
}
template <class T>
inline bool handle<T>::operator!() const
{
return m_p == 0;
}
template <class T>
inline T* handle<T>::release()
{
T* result = m_p;
m_p = 0;
return result;
}
}} // namespace boost::python
#endif // HANDLE_DWA200269_HPP

View File

@@ -19,7 +19,7 @@ namespace detail
// objects::make_iterator(...), which allows us to pass member
// function and member data pointers.
template <class NextPolicies, class Target, class Accessor1, class Accessor2>
inline ref make_iterator(
inline handle<> make_iterator(
Accessor1 get_start, Accessor2 get_finish, boost::type<Target>* target = 0, NextPolicies* = 0)
{
return objects::make_iterator_function<NextPolicies,Target>(
@@ -68,7 +68,7 @@ struct iterators
// accessors. Deduce the Target type from the accessors. The iterator
// returns copies of the inderlying elements.
template <class Accessor1, class Accessor2>
ref range(Accessor1 start, Accessor2 finish)
handle<> range(Accessor1 start, Accessor2 finish)
{
return detail::make_iterator<objects::default_iterator_call_policies>(
start, finish
@@ -78,7 +78,7 @@ ref range(Accessor1 start, Accessor2 finish)
// Create an iterator-building function which uses the given accessors
// and next() policies. Deduce the Target type.
template <class NextPolicies, class Accessor1, class Accessor2>
ref range(Accessor1 start, Accessor2 finish, NextPolicies* = 0)
handle<> range(Accessor1 start, Accessor2 finish, NextPolicies* = 0)
{
return detail::make_iterator<NextPolicies>(start, finish, detail::target(start));
}
@@ -86,7 +86,7 @@ ref range(Accessor1 start, Accessor2 finish, NextPolicies* = 0)
// 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>
ref range(Accessor1 start, Accessor2 finish, NextPolicies* = 0, boost::type<Target>* = 0)
handle<> range(Accessor1 start, Accessor2 finish, NextPolicies* = 0, boost::type<Target>* = 0)
{
typedef typename add_reference<Target>::type target;
return detail::make_iterator<NextPolicies, target>(start, finish);
@@ -98,10 +98,10 @@ ref range(Accessor1 start, Accessor2 finish, NextPolicies* = 0, boost::type<Targ
// next() function.
template <class Container
, class NextPolicies = objects::default_iterator_call_policies>
struct iterator : ref
struct iterator : handle<>
{
iterator()
: ref(
: handle<>(
range<NextPolicies>(
&iterators<Container>::begin, &iterators<Container>::end
))

View File

@@ -27,7 +27,7 @@ class module : public detail::module_base
// Add elements to the module
module& setattr(const char* name, PyObject*);
module& setattr(const char* name, PyTypeObject*);
module& setattr(const char* name, ref const&);
module& setattr(const char* name, handle<> const&);
module& add(PyTypeObject* x); // just use the type's name
template <class T, class Bases, class HolderGenerator>
@@ -68,7 +68,7 @@ inline module& module::setattr(const char* name, PyTypeObject* x)
return *this;
}
inline module& module::setattr(const char* name, ref const& x)
inline module& module::setattr(const char* name, handle<> const& x)
{
this->base::setattr(name, x);
return *this;

View File

@@ -10,7 +10,7 @@
# include <boost/python/detail/config.hpp>
# include <boost/utility.hpp>
# include <boost/python/type_id.hpp>
# include <boost/python/reference.hpp>
# include <boost/python/handle.hpp>
# include <boost/python/instance_holder.hpp>
# include <cstddef>
@@ -35,15 +35,15 @@ struct BOOST_PYTHON_DECL class_base : private noncopyable
);
// Retrieve the underlying object
ref object() const { return m_object; }
void add_property(char const* name, ref const& fget);
void add_property(char const* name, ref const& fget, ref const& fset);
void setattr(char const* name, ref const&);
type_handle object() const { return m_object; }
void add_property(char const* name, handle<> const& fget);
void add_property(char const* name, handle<> const& fget, handle<> const& fset);
void setattr(char const* name, handle<> const&);
private:
ref m_object;
type_handle m_object;
};
BOOST_PYTHON_DECL ref registered_class_object(class_id id);
BOOST_PYTHON_DECL type_handle registered_class_object(class_id id);
// Each extension instance will be one of these
struct instance
@@ -52,8 +52,8 @@ struct instance
instance_holder* objects;
};
BOOST_PYTHON_DECL ref class_metatype();
BOOST_PYTHON_DECL ref class_type();
BOOST_PYTHON_DECL type_handle class_metatype();
BOOST_PYTHON_DECL type_handle class_type();
}}} // namespace boost::python::objects

View File

@@ -8,7 +8,7 @@
# include <boost/python/object/class_wrapper.hpp>
# include <boost/mpl/for_each.hpp>
# include <boost/python/reference.hpp>
# include <boost/python/handle.hpp>
# include <boost/python/converter/registry.hpp>
# include <boost/python/object/find_instance.hpp>
# include <boost/python/object/inheritance.hpp>

View File

@@ -6,7 +6,7 @@
#ifndef CLASS_WRAPPER_DWA20011221_HPP
# define CLASS_WRAPPER_DWA20011221_HPP
# include <boost/python/reference.hpp>
# include <boost/python/handle.hpp>
# include <boost/python/to_python_converter.hpp>
namespace boost { namespace python { namespace objects {
@@ -15,7 +15,7 @@ template <class T, class Holder>
struct class_wrapper
: to_python_converter<T,class_wrapper<T,Holder> >
{
class_wrapper(ref const& type_)
class_wrapper(handle<> const& type_)
: m_class_object_keeper(type_)
{
assert(type_->ob_type == (PyTypeObject*)class_metatype().get());
@@ -34,7 +34,7 @@ struct class_wrapper
// Everything's OK; Bypass NULL checks but guard against
// exceptions.
ref result(raw_result, ref::allow_null());
handle<> result(python::allow_null(raw_result));
// Build a value_holder to contain the object using the copy
// constructor
@@ -48,7 +48,7 @@ struct class_wrapper
}
private:
ref m_class_object_keeper;
handle<> m_class_object_keeper;
static PyTypeObject* m_class_object;
};

View File

@@ -8,7 +8,7 @@
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/detail/config.hpp>
# include <boost/python/reference.hpp>
# include <boost/python/handle.hpp>
# include <boost/function/function2.hpp>
namespace boost { namespace python { namespace objects {
@@ -27,7 +27,7 @@ struct BOOST_PYTHON_DECL function : PyObject
// a function object (this class), and an existing function is
// already there, add it as an overload.
static void add_to_namespace(
ref const& name_space, char const* name, ref const& attribute);
handle<> const& name_space, char const* name, handle<> const& attribute);
private: // helper functions
void argument_error(PyObject* args, PyObject* keywords) const;

View File

@@ -11,7 +11,7 @@
# include <boost/python/return_value_policy.hpp>
# include <boost/python/copy_const_reference.hpp>
# include <boost/python/object/function.hpp>
# include <boost/python/reference.hpp>
# include <boost/python/handle.hpp>
# include <boost/type.hpp>
# include <boost/python/arg_from_python.hpp>
# include <boost/mpl/apply.hpp>
@@ -42,9 +42,9 @@ struct default_iterator_call_policies
template <class NextPolicies, class Iterator>
struct iterator_range
{
iterator_range(ref sequence, Iterator start, Iterator finish);
iterator_range(handle<> sequence, Iterator start, Iterator finish);
ref m_sequence; // Keeps the sequence alive while iterating.
handle<> m_sequence; // Keeps the sequence alive while iterating.
Iterator m_start;
Iterator m_finish;
};
@@ -113,18 +113,18 @@ namespace detail
// policies, creating it if neccessary. Requires: NextPolicies is
// default-constructible.
template <class Iterator, class NextPolicies>
ref demand_iterator_class(char const* name, Iterator* = 0, NextPolicies const& policies = NextPolicies())
handle<> demand_iterator_class(char const* name, Iterator* = 0, NextPolicies const& policies = NextPolicies())
{
typedef iterator_range<NextPolicies,Iterator> range_;
// Check the registry. If one is already registered, return it.
ref result(
handle<> result(
objects::registered_class_object(python::type_id<range_>()));
if (result.get() == 0)
{
// Make a callable object which can be used as the iterator's next() function.
ref next_function(
handle<> next_function(
new objects::function(
objects::py_function(
bind(&detail::iterator_next<Iterator,NextPolicies>::execute, _1, _2, policies))
@@ -174,7 +174,7 @@ namespace detail
// Build and convert the iterator_range<>.
return cr(
iterator_range<NextPolicies,Iterator>(
ref(arg0, ref::increment_count)
handle<>(python::borrow(arg0))
, get_start(x), get_finish(x)));
}
};
@@ -187,13 +187,13 @@ namespace detail
// iterators for the range, and an instance of NextPolicies is used as
// CallPolicies for the Python iterator's next() function.
template <class NextPolicies, class Target, class Accessor1, class Accessor2>
inline ref make_iterator_function(
inline handle<> make_iterator_function(
Accessor1 const& get_start, Accessor2 const& get_finish
, boost::type<Target>* = 0, NextPolicies* = 0)
{
typedef typename Accessor1::result_type result_type;
return ref(
return handle<>(
new objects::function(
objects::py_function(
boost::bind(
@@ -210,7 +210,7 @@ inline ref make_iterator_function(
//
template <class NextPolicies, class Iterator>
inline iterator_range<NextPolicies,Iterator>::iterator_range(
ref sequence, Iterator start, Iterator finish)
handle<> sequence, Iterator start, Iterator finish)
: m_sequence(sequence), m_start(start), m_finish(finish)
{
}

View File

@@ -6,11 +6,11 @@
#ifndef ITERATOR_CORE_DWA2002512_HPP
# define ITERATOR_CORE_DWA2002512_HPP
# include <boost/python/reference.hpp>
# include <boost/python/handle.hpp>
namespace boost { namespace python { namespace objects {
BOOST_PYTHON_DECL ref identity_function();
BOOST_PYTHON_DECL handle<> identity_function();
BOOST_PYTHON_DECL void set_stop_iteration_error();
}}} // namespace boost::python::object

View File

@@ -9,11 +9,14 @@
#ifndef OBJECTS_DWA051100_H_
# define OBJECTS_DWA051100_H_
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/detail/config.hpp>
# include <boost/python/reference.hpp>
# include "boost/operators.hpp"
# include <utility>
# ifdef BOOST_PYTHON_V2
# include <boost/python/objects2.hpp>
# else
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/detail/config.hpp>
# include <boost/python/reference.hpp>
# include "boost/operators.hpp"
# include <utility>
namespace boost { namespace python {
@@ -354,8 +357,6 @@ struct BOOST_PYTHON_DECL list_slice_proxy
}} // namespace boost::python
# ifndef BOOST_PYTHON_V2
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
BOOST_PYTHON_DECL PyObject* to_python(const boost::python::tuple&);
@@ -391,5 +392,5 @@ inline boost::python::dictionary from_python(PyObject* p, boost::python::type<co
}
BOOST_PYTHON_END_CONVERSION_NAMESPACE
# endif
# endif // !BOOST_PYTHON_V2
#endif // OBJECTS_DWA051100_H_

348
include/boost/python/objects2.hpp Executable file
View File

@@ -0,0 +1,348 @@
// (C) 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 OBJECTS_DWA20020611_H
# define OBJECTS_DWA20020611_H
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/detail/config.hpp>
# include <boost/python/handle.hpp>
# include "boost/operators.hpp"
# include <utility>
namespace boost { namespace python {
class BOOST_PYTHON_DECL objects_base
{
public:
explicit objects_base(handle<> const& p);
// Return a reference to the held object
handle<> reference() const;
// Return a raw pointer to the held object
PyObject* get() const;
private:
handle<> m_p;
};
class tuple;
class BOOST_PYTHON_DECL tuple_base : public objects_base
{
public:
explicit tuple_base(std::size_t n = 0);
explicit tuple_base(handle<> p);
static PyTypeObject* type_obj();
static bool accepts(handle<> p);
std::size_t size() const;
handle<> operator[](std::size_t pos) const;
void set_item(std::size_t pos, const handle<>& rhs);
tuple slice(int low, int high) const;
friend BOOST_PYTHON_DECL tuple operator+(const tuple&, const tuple&);
friend BOOST_PYTHON_DECL tuple& operator+=(tuple&, const tuple&);
};
class tuple : public tuple_base
{
public:
explicit tuple(std::size_t n = 0) : tuple_base(n) {}
explicit tuple(handle<> p) : tuple_base(p) {}
template <class First, class Second>
tuple(const std::pair<First,Second>& x)
: tuple_base(handle<>(PyTuple_New(2)))
{
set_item(0, x.first);
set_item(1, x.second);
}
template <class First, class Second>
tuple(const First& first, const Second& second)
: tuple_base(handle<>(PyTuple_New(2)))
{
set_item(0, first);
set_item(1, second);
}
template <class First, class Second, class Third>
tuple(const First& first, const Second& second, const Third& third)
: tuple_base(handle<>(PyTuple_New(3)))
{
set_item(0, first);
set_item(1, second);
set_item(2, third);
}
template <class First, class Second, class Third, class Fourth>
tuple(const First& first, const Second& second, const Third& third, const Fourth& fourth)
: tuple_base(handle<>(PyTuple_New(4)))
{
set_item(0, first);
set_item(1, second);
set_item(2, third);
set_item(3, fourth);
}
void set_item(std::size_t pos, const handle<>& rhs)
{
tuple_base::set_item(pos, rhs);
}
};
class list;
struct BOOST_PYTHON_DECL list_proxy;
struct BOOST_PYTHON_DECL list_slice_proxy;
class BOOST_PYTHON_DECL list_base : public objects_base
{
protected:
typedef list_proxy proxy;
typedef list_slice_proxy slice_proxy;
public:
explicit list_base(handle<> p);
explicit list_base(std::size_t sz = 0);
static PyTypeObject* type_obj();
static bool accepts(handle<> p);
std::size_t size() const;
handle<> operator[](std::size_t pos) const;
proxy operator[](std::size_t pos);
handle<> get_item(std::size_t pos) const;
void set_item(std::size_t pos, const handle<>& );
// void set_item(std::size_t pos, const object& );
void insert(std::size_t index, const handle<>& item);
void push_back(const handle<>& item);
void append(const handle<>& item);
list slice(int low, int high) const;
slice_proxy slice(int low, int high);
void sort();
void reverse();
tuple as_tuple() const;
};
class list : public list_base
{
public:
explicit list(handle<> p) : list_base(p) {}
explicit list(std::size_t sz = 0) : list_base(sz) {}
template <class T>
void set_item(std::size_t pos, const T& x)
{ this->set_item(pos, make_ref(x)); }
template <class T>
void insert(std::size_t index, const T& x)
{ this->insert(index, make_ref(x)); }
template <class T>
void push_back(const T& item)
{ this->push_back(make_ref(item)); }
template <class T>
void append(const T& item)
{ this->append(make_ref(item)); }
void set_item(std::size_t pos, const handle<>& x) { list_base::set_item(pos, x); }
void insert(std::size_t index, const handle<>& item) { list_base::insert(index, item); }
void push_back(const handle<>& item) { list_base::push_back(item); }
void append(const handle<>& item) { list_base::append(item); }
};
class BOOST_PYTHON_DECL string
: public objects_base, public boost::multipliable2<string, unsigned int>
{
public:
// Construct from an owned PyObject*.
// Precondition: p must point to a python string.
explicit string(handle<> p);
explicit string(const char* s);
string(const char* s, std::size_t length);
string(const string& rhs);
enum interned_t { interned };
string(const char* s, interned_t);
// Get the type object for Strings
static PyTypeObject* type_obj();
// Return true if the given object is a python string
static bool accepts(handle<> o);
// Return the length of the string.
std::size_t size() const;
// Returns a null-terminated representation of the contents of string.
// The pointer refers to the internal buffer of string, not a copy.
// The data must not be modified in any way. It must not be de-allocated.
const char* c_str() const;
string& operator*=(unsigned int repeat_count);
string& operator+=(const string& rhs);
friend string operator+(string x, string y);
string& operator+=(const char* rhs);
friend string operator+(string x, const char* y);
friend string operator+(const char* x, string y);
void intern();
friend string operator%(const string& format, const tuple& args);
};
class dictionary;
struct BOOST_PYTHON_DECL dictionary_proxy;
class BOOST_PYTHON_DECL dictionary_base : public objects_base
{
protected:
typedef dictionary_proxy proxy;
public:
explicit dictionary_base(handle<> p);
dictionary_base();
void clear();
static PyTypeObject* type_obj();
static bool accepts(handle<> p);
public:
proxy operator[](handle<> key);
handle<> operator[](handle<> key) const;
handle<> get_item(const handle<>& key) const;
handle<> get_item(const handle<>& key, const handle<>& default_) const;
void set_item(const handle<>& key, const handle<>& value);
void erase(handle<> key);
// proxy operator[](const object& key);
// ref operator[](const object& key) const;
// ref get_item(const object& key, ref default_ = ref()) const;
// void set_item(const object& key, const ref& value);
// void erase(const object& key);
list items() const;
list keys() const;
list values() const;
std::size_t size() const;
// TODO: iterator support
};
struct BOOST_PYTHON_DECL dictionary_proxy
{
template <class T>
const handle<>& operator=(const T& rhs)
{ return (*this) = make_ref(rhs); }
const handle<>& operator=(const handle<>& rhs);
operator handle<>() const;
private:
friend class dictionary_base;
dictionary_proxy(const handle<>& dict, const handle<>& key);
// This is needed to work around the very strange MSVC error report that the
// return type of the built-in operator= differs from that of the ones
// defined above. Couldn't hurt to make these un-assignable anyway, though.
const handle<>& operator=(const dictionary_proxy&); // Not actually implemented
private:
handle<> m_dict;
handle<> m_key;
};
class dictionary : public dictionary_base
{
typedef dictionary_proxy proxy;
public:
explicit dictionary(handle<> p) : dictionary_base(p) {}
dictionary() : dictionary_base() {}
template <class Key>
proxy operator[](const Key& key)
{ return this->operator[](make_ref(key)); }
proxy operator[](handle<> key)
{ return dictionary_base::operator[](key); }
template <class Key>
handle<> operator[](const Key& key) const
{ return this->operator[](make_ref(key)); }
handle<> operator[](handle<> key) const
{ return dictionary_base::operator[](key); }
template <class Key>
handle<> get_item(const Key& key) const
{ return this->get_item(make_ref(key)); }
handle<> get_item(const handle<>& key) const
{ return dictionary_base::get_item(key); }
template <class Key, class Default>
handle<> get_item(const Key& key, const Default& default_) const
{ return this->get_item(make_ref(key), make_ref(default_)); }
handle<> get_item(const handle<>& key, const handle<>& default_) const
{ return dictionary_base::get_item(key, default_); }
template <class Key, class Value>
void set_item(const Key& key, const Value& value)
{ this->set_item(make_ref(key), make_ref(value)); }
void set_item(const handle<>& key, const handle<>& value)
{ dictionary_base::set_item(key, value); }
template <class Key>
void erase(const Key& key)
{ this->erase(make_ref(key)); }
void erase(handle<> key)
{ dictionary_base::erase(key); }
};
struct BOOST_PYTHON_DECL list_proxy
{
template <class T>
const handle<>& operator=(const T& rhs)
{ return (*this) = make_ref(rhs); }
const handle<>& operator=(const handle<>& rhs);
operator handle<>() const;
private:
friend class list_base;
list_proxy(const handle<>& list, std::size_t index);
// This is needed to work around the very strange MSVC error report that the
// return type of the built-in operator= differs from that of the ones
// defined above. Couldn't hurt to make these un-assignable anyway, though.
const handle<>& operator=(const list_proxy&); // Not actually implemented
private:
list m_list;
std::size_t m_index;
};
struct BOOST_PYTHON_DECL list_slice_proxy
{
const list& operator=(const list& rhs);
operator handle<>() const;
operator list() const;
std::size_t size() const;
handle<> operator[](std::size_t pos) const;
private:
friend class list_base;
list_slice_proxy(const handle<>& list, int low, int high);
private:
handle<> m_list;
int m_low, m_high;
};
}} // namespace boost::python
#endif // OBJECTS_DWA20020611_H

View File

@@ -27,7 +27,7 @@ namespace detail
template <class T>
PyObject* convert_result(T const& x)
{
return converter::arg_to_python<T>(x).get_incref();
return converter::arg_to_python<T>(x).release();
}
// Operator implementation template declarations. The nested apply

View File

@@ -9,6 +9,12 @@
#ifndef PYPTR_DWA050400_H_
# define PYPTR_DWA050400_H_
# ifdef BOOST_PYTHON_V2
# error obsolete
# else
# include <boost/python/detail/config.hpp>
# include <boost/operators.hpp>
# include <boost/python/detail/wrap_python.hpp>
@@ -19,7 +25,6 @@
# include <boost/python/errors.hpp>
# include <boost/python/detail/cast.hpp>
# ifndef BOOST_PYTHON_V2
# include <boost/python/conversions.hpp>
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
@@ -39,19 +44,14 @@ struct py_ptr_conversions : Base
};
BOOST_PYTHON_END_CONVERSION_NAMESPACE
# endif
namespace boost { namespace python {
# ifndef BOOST_PYTHON_V2
BOOST_PYTHON_IMPORT_CONVERSION(py_ptr_conversions);
# endif
template <class T>
class reference
# ifndef BOOST_PYTHON_V2
: public py_ptr_conversions<reference<T>, T>
# endif
{
public:
typedef T value_type;
@@ -215,12 +215,7 @@ private:
inline PyObject* object() const
{
#ifdef BOOST_PYTHON_V2
return (PyObject*)(
(char*)&m_p->ob_type - offsetof(PyObject,ob_type));
#else
return as_object(m_p);
#endif
}
T* m_p;
@@ -228,14 +223,14 @@ private:
typedef reference<PyObject> ref;
#ifndef BOOST_PYTHON_V2
template <class T>
ref make_ref(const T& x)
{
return ref(to_python(x));
}
#endif
}} // namespace boost::python
#endif // BOOST_PYTHON_V2
#endif // PYPTR_DWA050400_H_

View File

@@ -107,7 +107,7 @@ inline PyObject* to_python_indirect<T,MakeHolder>::operator()(T x) const
// Everything's OK; Bypass NULL checks but guard against
// exceptions.
ref result(raw_result, ref::allow_null());
handle<> result(python::allow_null(raw_result));
// Build a value_holder to contain the object using the copy
// constructor