handle.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
  75. template<typename Y>
  76. handle& operator=(handle<Y> const & r) // never throws
  77. {
  78. python::xdecref(m_p);
  79. m_p = python::xincref(python::upcast<T>(r.get()));
  80. return *this;
  81. }
  82. #endif
  83. template <typename Y>
  84. handle(handle<Y> const& r)
  85. : m_p(python::xincref(python::upcast<T>(r.get())))
  86. {
  87. }
  88. handle(handle const& r)
  89. : m_p(python::xincref(r.m_p))
  90. {
  91. }
  92. T* operator-> () const;
  93. T& operator* () const;
  94. T* get() const;
  95. T* release();
  96. void reset();
  97. operator bool_type() const // never throws
  98. {
  99. return m_p ? &handle<T>::get : 0;
  100. }
  101. bool operator! () const; // never throws
  102. public: // implementation details -- do not touch
  103. // Defining this in the class body suppresses a VC7 link failure
  104. inline handle(detail::borrowed_reference x)
  105. : m_p(
  106. python::incref(
  107. downcast<T>((PyObject*)x)
  108. ))
  109. {
  110. }
  111. private: // data members
  112. T* m_p;
  113. };
  114. #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
  115. } // namespace python
  116. #endif
  117. template<class T> inline T * get_pointer(python::handle<T> const & p)
  118. {
  119. return p.get();
  120. }
  121. #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
  122. namespace python {
  123. #else
  124. // We don't want get_pointer above to hide the others
  125. using boost::get_pointer;
  126. #endif
  127. typedef handle<PyTypeObject> type_handle;
  128. //
  129. // Compile-time introspection
  130. //
  131. # ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  132. template<typename T>
  133. class is_handle
  134. {
  135. public:
  136. BOOST_STATIC_CONSTANT(bool, value = false);
  137. };
  138. template<typename T>
  139. class is_handle<handle<T> >
  140. {
  141. public:
  142. BOOST_STATIC_CONSTANT(bool, value = true);
  143. };
  144. # else
  145. namespace detail
  146. {
  147. typedef char (&yes_handle_t)[1];
  148. typedef char (&no_handle_t)[2];
  149. no_handle_t is_handle_test(...);
  150. template<typename T>
  151. yes_handle_t is_handle_test(boost::type< handle<T> >);
  152. }
  153. template<typename T>
  154. class is_handle
  155. {
  156. public:
  157. BOOST_STATIC_CONSTANT(
  158. bool, value = (
  159. sizeof(detail::is_handle_test(boost::type<T>()))
  160. == sizeof(detail::yes_handle_t)));
  161. };
  162. # endif
  163. //
  164. // implementations
  165. //
  166. template <class T>
  167. inline handle<T>::handle()
  168. : m_p(0)
  169. {
  170. }
  171. template <class T>
  172. inline handle<T>::~handle()
  173. {
  174. python::xdecref(m_p);
  175. }
  176. template <class T>
  177. inline T* handle<T>::operator->() const
  178. {
  179. return m_p;
  180. }
  181. template <class T>
  182. inline T& handle<T>::operator*() const
  183. {
  184. return *m_p;
  185. }
  186. template <class T>
  187. inline T* handle<T>::get() const
  188. {
  189. return m_p;
  190. }
  191. template <class T>
  192. inline bool handle<T>::operator!() const
  193. {
  194. return m_p == 0;
  195. }
  196. template <class T>
  197. inline T* handle<T>::release()
  198. {
  199. T* result = m_p;
  200. m_p = 0;
  201. return result;
  202. }
  203. template <class T>
  204. inline void handle<T>::reset()
  205. {
  206. python::xdecref(m_p);
  207. m_p = 0;
  208. }
  209. // Because get_managed_object must return a non-null PyObject*, we
  210. // return Py_None if the handle is null.
  211. template <class T>
  212. inline PyObject* get_managed_object(handle<T> const& h, tag_t)
  213. {
  214. return h.get() ? python::upcast<PyObject>(h.get()) : Py_None;
  215. }
  216. }} // namespace boost::python
  217. #endif // HANDLE_DWA200269_HPP