9
3

handle.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright David Abrahams 2002.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef HANDLE_DWA200269_HPP
  6. # define HANDLE_DWA200269_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/python/cast.hpp>
  9. # include <boost/python/errors.hpp>
  10. # include <boost/python/borrowed.hpp>
  11. # include <boost/python/handle_fwd.hpp>
  12. # include <boost/python/refcount.hpp>
  13. # include <boost/python/tag.hpp>
  14. # include <boost/python/detail/raw_pyobject.hpp>
  15. namespace boost { namespace python {
  16. template <class T> struct null_ok;
  17. template <class T>
  18. inline null_ok<T>* allow_null(T* p)
  19. {
  20. return (null_ok<T>*)p;
  21. }
  22. namespace detail
  23. {
  24. template <class T>
  25. inline T* manage_ptr(detail::borrowed<null_ok<T> >* p, int)
  26. {
  27. return python::xincref((T*)p);
  28. }
  29. template <class T>
  30. inline T* manage_ptr(null_ok<detail::borrowed<T> >* p, int)
  31. {
  32. return python::xincref((T*)p);
  33. }
  34. template <class T>
  35. inline T* manage_ptr(detail::borrowed<T>* p, long)
  36. {
  37. return python::incref(expect_non_null((T*)p));
  38. }
  39. template <class T>
  40. inline T* manage_ptr(null_ok<T>* p, long)
  41. {
  42. return (T*)p;
  43. }
  44. template <class T>
  45. inline T* manage_ptr(T* p, ...)
  46. {
  47. return expect_non_null(p);
  48. }
  49. }
  50. template <class T>
  51. class handle
  52. {
  53. typedef T* (handle::* bool_type )() const;
  54. public: // types
  55. typedef T element_type;
  56. public: // member functions
  57. handle();
  58. ~handle();
  59. template <class Y>
  60. explicit handle(Y* p)
  61. : m_p(
  62. python::upcast<T>(
  63. detail::manage_ptr(p, 0)
  64. )
  65. )
  66. {
  67. }
  68. handle& operator=(handle const& r)
  69. {
  70. python::xdecref(m_p);
  71. m_p = python::xincref(r.m_p);
  72. return *this;
  73. }
  74. template<typename Y>
  75. handle& operator=(handle<Y> const & r) // never throws
  76. {
  77. python::xdecref(m_p);
  78. m_p = python::xincref(python::upcast<T>(r.get()));
  79. return *this;
  80. }
  81. template <typename Y>
  82. handle(handle<Y> const& r)
  83. : m_p(python::xincref(python::upcast<T>(r.get())))
  84. {
  85. }
  86. handle(handle const& r)
  87. : m_p(python::xincref(r.m_p))
  88. {
  89. }
  90. T* operator-> () const;
  91. T& operator* () const;
  92. T* get() const;
  93. T* release();
  94. void reset();
  95. operator bool_type() const // never throws
  96. {
  97. return m_p ? &handle<T>::get : 0;
  98. }
  99. bool operator! () const; // never throws
  100. public: // implementation details -- do not touch
  101. // Defining this in the class body suppresses a VC7 link failure
  102. inline handle(detail::borrowed_reference x)
  103. : m_p(
  104. python::incref(
  105. downcast<T>((PyObject*)x)
  106. ))
  107. {
  108. }
  109. private: // data members
  110. T* m_p;
  111. };
  112. #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
  113. } // namespace python
  114. #endif
  115. template<class T> inline T * get_pointer(python::handle<T> const & p)
  116. {
  117. return p.get();
  118. }
  119. #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
  120. namespace python {
  121. #else
  122. // We don't want get_pointer above to hide the others
  123. using boost::get_pointer;
  124. #endif
  125. typedef handle<PyTypeObject> type_handle;
  126. //
  127. // Compile-time introspection
  128. //
  129. template<typename T>
  130. class is_handle
  131. {
  132. public:
  133. BOOST_STATIC_CONSTANT(bool, value = false);
  134. };
  135. template<typename T>
  136. class is_handle<handle<T> >
  137. {
  138. public:
  139. BOOST_STATIC_CONSTANT(bool, value = true);
  140. };
  141. //
  142. // implementations
  143. //
  144. template <class T>
  145. inline handle<T>::handle()
  146. : m_p(0)
  147. {
  148. }
  149. template <class T>
  150. inline handle<T>::~handle()
  151. {
  152. python::xdecref(m_p);
  153. }
  154. template <class T>
  155. inline T* handle<T>::operator->() const
  156. {
  157. return m_p;
  158. }
  159. template <class T>
  160. inline T& handle<T>::operator*() const
  161. {
  162. return *m_p;
  163. }
  164. template <class T>
  165. inline T* handle<T>::get() const
  166. {
  167. return m_p;
  168. }
  169. template <class T>
  170. inline bool handle<T>::operator!() const
  171. {
  172. return m_p == 0;
  173. }
  174. template <class T>
  175. inline T* handle<T>::release()
  176. {
  177. T* result = m_p;
  178. m_p = 0;
  179. return result;
  180. }
  181. template <class T>
  182. inline void handle<T>::reset()
  183. {
  184. python::xdecref(m_p);
  185. m_p = 0;
  186. }
  187. // Because get_managed_object must return a non-null PyObject*, we
  188. // return Py_None if the handle is null.
  189. template <class T>
  190. inline PyObject* get_managed_object(handle<T> const& h, tag_t)
  191. {
  192. return h.get() ? python::upcast<PyObject>(h.get()) : Py_None;
  193. }
  194. }} // namespace boost::python
  195. #endif // HANDLE_DWA200269_HPP