1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
[section boost/python/handle.hpp]
[section Introduction]
<boost/python/handle.hpp> provides class template `handle`, a smart pointer for managing reference-counted Python objects.
[endsect]
[section Class template `handle`]
`handle` is a smart pointer to a Python object type; it holds a pointer of type `T*`, where `T` is its template parameter. T must be either a type derived from `PyObject` or a [link pod POD] type whose initial `sizeof(PyObject)` bytes are layout-compatible with `PyObject`. Use `handle<>` at the boundary between the Python/'C' API and high-level code; prefer object for a generalized interface to Python objects.
In this document, the term "upcast" refers to an operation which converts a pointer `Y*` to a base class `pointer T*` via `static_cast<T*>` if `Y` is derived from `T`, or via C-style cast (`T*`) if it is not. However, in the latter case the "upcast" is ill-formed if the initial `sizeof(PyObject)` bytes of `Y` are not layout-compatible with `PyObject`.
``
namespace boost { namespace python
{
template <class T>
class handle
{
typedef unspecified-member-function-pointer bool_type;
public: // types
typedef T element_type;
public: // member functions
~handle();
template <class Y>
explicit handle(detail::borrowed<null_ok<Y> >* p);
template <class Y>
explicit handle(null_ok<detail::borrowed<Y> >* p);
template <class Y>
explicit handle(detail::borrowed<Y>* p);
template <class Y>
explicit handle(null_ok<Y>* p);
template <class Y>
explicit handle(Y* p);
handle();
handle& operator=(handle const& r);
template<typename Y>
handle& operator=(handle<Y> const & r); // never throws
template <typename Y>
handle(handle<Y> const& r);
handle(handle const& r);
T* operator-> () const;
T& operator* () const;
T* get() const;
void reset();
T* release();
operator bool_type() const; // never throws
private:
T* m_p;
};
template <class T> struct null_ok;
namespace detail { template <class T> struct borrowed; }
}}
``
[section Class template `handle` constructors and destructor]
``virtual ~handle();``
[variablelist
[[Effects][`Py_XDECREF(upcast<PyObject*>(m_p))`]]
]
``template <class Y>
explicit handle(detail::borrowed<null_ok<Y> >* p);
``
[variablelist
[[Effects][
``Py_XINCREF(upcast<PyObject*>(p));
m_p = upcast<T*>(p);
``
]]
]
``template <class Y>
explicit handle(null_ok<detail::borrowed<Y> >* p);``
[variablelist
[[Effects][
``Py_XINCREF(upcast<PyObject*>(p));
m_p = upcast<T*>(p);
``
]]
]
``template <class Y>
explicit handle(detail::borrowed<Y>* p);``
[variablelist
[[Effects][
``Py_XINCREF(upcast<PyObject*>(p));
m_p = upcast<T*>(expect_non_null(p));
``
]]
]
``template <class Y>
explicit handle(null_ok<Y>* p);
``
[variablelist
[[Effects][`m_p = upcast<T*>(p);`]]
]
``
template <class Y>
explicit handle(Y* p);
``
[variablelist
[[Effects][`m_p = upcast<T*>(expect_non_null(p));`]]
]
``
handle();
``
[variablelist
[[Effects][`m_p = 0;`]]
]
``
template <typename Y>
handle(handle<Y> const& r);
handle(handle const& r);
``
[variablelist
[[Effects][m_p = r.m_p; Py_XINCREF(upcast<PyObject*>(m_p));]]
]
[endsect]
[section Class template `handle` modifiers]
``
handle& operator=(handle const& r);
template<typename Y>
handle& operator=(handle<Y> const & r); // never throws
``
[variablelist
[[Effects][`Py_XINCREF(upcast<PyObject*>(r.m_p)); Py_XDECREF( upcast<PyObject*>(m_p)); m_p = r.m_p;`]]
]
``
T* release();
``
[variablelist
[[Effects][`T* x = m_p; m_p = 0; return x;`]]
]
``
void reset();
``
[variablelist
[[Effects][`*this = handle<T>();`]]
]
[endsect]
[section Class template `handle` observers]
``
T* operator-> () const;
T* get() const;
``
[variablelist
[[Returns][`m_p;`]]
]
``
T& operator* () const;
``
[variablelist
[[Returns][`*m_p;`]]
]
``
operator bool_type() const; // never throws
``
[variablelist
[[Returns][`0` if `m_p == 0`, a pointer convertible to true otherwise.]]
]
[endsect]
[endsect]
[section Function `borrowed`]
``
template <class T>
detail::borrowed<T>* borrowed(T* p)
{
return (detail::borrowed<T>*)p;
}
``
[endsect]
[section Function `allow_null`]
``
template <class T>
null_ok<T>* allow_null(T* p)
{
return (null_ok<T>*)p;
}
``
[endsect]
[endsect]
|