handle.qbk 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. [section boost/python/handle.hpp]
  2. [section Introduction]
  3. <boost/python/handle.hpp> provides class template `handle`, a smart pointer for managing reference-counted Python objects.
  4. [endsect]
  5. [section Class template `handle`]
  6. `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.
  7. 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`.
  8. ``
  9. namespace boost { namespace python
  10. {
  11. template <class T>
  12. class handle
  13. {
  14. typedef unspecified-member-function-pointer bool_type;
  15. public: // types
  16. typedef T element_type;
  17. public: // member functions
  18. ~handle();
  19. template <class Y>
  20. explicit handle(detail::borrowed<null_ok<Y> >* p);
  21. template <class Y>
  22. explicit handle(null_ok<detail::borrowed<Y> >* p);
  23. template <class Y>
  24. explicit handle(detail::borrowed<Y>* p);
  25. template <class Y>
  26. explicit handle(null_ok<Y>* p);
  27. template <class Y>
  28. explicit handle(Y* p);
  29. handle();
  30. handle& operator=(handle const& r);
  31. template<typename Y>
  32. handle& operator=(handle<Y> const & r); // never throws
  33. template <typename Y>
  34. handle(handle<Y> const& r);
  35. handle(handle const& r);
  36. T* operator-> () const;
  37. T& operator* () const;
  38. T* get() const;
  39. void reset();
  40. T* release();
  41. operator bool_type() const; // never throws
  42. private:
  43. T* m_p;
  44. };
  45. template <class T> struct null_ok;
  46. namespace detail { template <class T> struct borrowed; }
  47. }}
  48. ``
  49. [section Class template `handle` constructors and destructor]
  50. ``virtual ~handle();``
  51. [variablelist
  52. [[Effects][`Py_XDECREF(upcast<PyObject*>(m_p))`]]
  53. ]
  54. ``template <class Y>
  55. explicit handle(detail::borrowed<null_ok<Y> >* p);
  56. ``
  57. [variablelist
  58. [[Effects][
  59. ``Py_XINCREF(upcast<PyObject*>(p));
  60. m_p = upcast<T*>(p);
  61. ``
  62. ]]
  63. ]
  64. ``template <class Y>
  65. explicit handle(null_ok<detail::borrowed<Y> >* p);``
  66. [variablelist
  67. [[Effects][
  68. ``Py_XINCREF(upcast<PyObject*>(p));
  69. m_p = upcast<T*>(p);
  70. ``
  71. ]]
  72. ]
  73. ``template <class Y>
  74. explicit handle(detail::borrowed<Y>* p);``
  75. [variablelist
  76. [[Effects][
  77. ``Py_XINCREF(upcast<PyObject*>(p));
  78. m_p = upcast<T*>(expect_non_null(p));
  79. ``
  80. ]]
  81. ]
  82. ``template <class Y>
  83. explicit handle(null_ok<Y>* p);
  84. ``
  85. [variablelist
  86. [[Effects][`m_p = upcast<T*>(p);`]]
  87. ]
  88. ``
  89. template <class Y>
  90. explicit handle(Y* p);
  91. ``
  92. [variablelist
  93. [[Effects][`m_p = upcast<T*>(expect_non_null(p));`]]
  94. ]
  95. ``
  96. handle();
  97. ``
  98. [variablelist
  99. [[Effects][`m_p = 0;`]]
  100. ]
  101. ``
  102. template <typename Y>
  103. handle(handle<Y> const& r);
  104. handle(handle const& r);
  105. ``
  106. [variablelist
  107. [[Effects][m_p = r.m_p; Py_XINCREF(upcast<PyObject*>(m_p));]]
  108. ]
  109. [endsect]
  110. [section Class template `handle` modifiers]
  111. ``
  112. handle& operator=(handle const& r);
  113. template<typename Y>
  114. handle& operator=(handle<Y> const & r); // never throws
  115. ``
  116. [variablelist
  117. [[Effects][`Py_XINCREF(upcast<PyObject*>(r.m_p)); Py_XDECREF( upcast<PyObject*>(m_p)); m_p = r.m_p;`]]
  118. ]
  119. ``
  120. T* release();
  121. ``
  122. [variablelist
  123. [[Effects][`T* x = m_p; m_p = 0; return x;`]]
  124. ]
  125. ``
  126. void reset();
  127. ``
  128. [variablelist
  129. [[Effects][`*this = handle<T>();`]]
  130. ]
  131. [endsect]
  132. [section Class template `handle` observers]
  133. ``
  134. T* operator-> () const;
  135. T* get() const;
  136. ``
  137. [variablelist
  138. [[Returns][`m_p;`]]
  139. ]
  140. ``
  141. T& operator* () const;
  142. ``
  143. [variablelist
  144. [[Returns][`*m_p;`]]
  145. ]
  146. ``
  147. operator bool_type() const; // never throws
  148. ``
  149. [variablelist
  150. [[Returns][`0` if `m_p == 0`, a pointer convertible to true otherwise.]]
  151. ]
  152. [endsect]
  153. [endsect]
  154. [section Function `borrowed`]
  155. ``
  156. template <class T>
  157. detail::borrowed<T>* borrowed(T* p)
  158. {
  159. return (detail::borrowed<T>*)p;
  160. }
  161. ``
  162. [endsect]
  163. [section Function `allow_null`]
  164. ``
  165. template <class T>
  166. null_ok<T>* allow_null(T* p)
  167. {
  168. return (null_ok<T>*)p;
  169. }
  170. ``
  171. [endsect]
  172. [endsect]