extract.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 EXTRACT_DWA200265_HPP
  6. # define EXTRACT_DWA200265_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/python/converter/object_manager.hpp>
  9. # include <boost/python/converter/from_python.hpp>
  10. # include <boost/python/converter/rvalue_from_python_data.hpp>
  11. # include <boost/python/converter/registered.hpp>
  12. # include <boost/python/converter/registered_pointee.hpp>
  13. # include <boost/python/object_core.hpp>
  14. # include <boost/python/refcount.hpp>
  15. # include <boost/python/detail/copy_ctor_mutates_rhs.hpp>
  16. # include <boost/python/detail/void_ptr.hpp>
  17. # include <boost/python/detail/void_return.hpp>
  18. # include <boost/call_traits.hpp>
  19. #if BOOST_WORKAROUND(BOOST_INTEL_WIN, <= 900)
  20. # define BOOST_EXTRACT_WORKAROUND ()
  21. #else
  22. # define BOOST_EXTRACT_WORKAROUND
  23. #endif
  24. namespace boost { namespace python {
  25. namespace api
  26. {
  27. class object;
  28. }
  29. namespace converter
  30. {
  31. template <class Ptr>
  32. struct extract_pointer
  33. {
  34. typedef Ptr result_type;
  35. extract_pointer(PyObject*);
  36. bool check() const;
  37. Ptr operator()() const;
  38. private:
  39. PyObject* m_source;
  40. void* m_result;
  41. };
  42. template <class Ref>
  43. struct extract_reference
  44. {
  45. typedef Ref result_type;
  46. extract_reference(PyObject*);
  47. bool check() const;
  48. Ref operator()() const;
  49. private:
  50. PyObject* m_source;
  51. void* m_result;
  52. };
  53. template <class T>
  54. struct extract_rvalue : private noncopyable
  55. {
  56. typedef typename mpl::if_<
  57. python::detail::copy_ctor_mutates_rhs<T>
  58. , T&
  59. , typename call_traits<T>::param_type
  60. >::type result_type;
  61. extract_rvalue(PyObject*);
  62. bool check() const;
  63. result_type operator()() const;
  64. private:
  65. PyObject* m_source;
  66. mutable rvalue_from_python_data<T> m_data;
  67. };
  68. template <class T>
  69. struct extract_object_manager
  70. {
  71. typedef T result_type;
  72. extract_object_manager(PyObject*);
  73. bool check() const;
  74. result_type operator()() const;
  75. private:
  76. PyObject* m_source;
  77. };
  78. template <class T>
  79. struct select_extract
  80. {
  81. BOOST_STATIC_CONSTANT(
  82. bool, obj_mgr = is_object_manager<T>::value);
  83. BOOST_STATIC_CONSTANT(
  84. bool, ptr = is_pointer<T>::value);
  85. BOOST_STATIC_CONSTANT(
  86. bool, ref = is_reference<T>::value);
  87. typedef typename mpl::if_c<
  88. obj_mgr
  89. , extract_object_manager<T>
  90. , typename mpl::if_c<
  91. ptr
  92. , extract_pointer<T>
  93. , typename mpl::if_c<
  94. ref
  95. , extract_reference<T>
  96. , extract_rvalue<T>
  97. >::type
  98. >::type
  99. >::type type;
  100. };
  101. }
  102. template <class T>
  103. struct extract
  104. : converter::select_extract<T>::type
  105. {
  106. private:
  107. typedef typename converter::select_extract<T>::type base;
  108. public:
  109. typedef typename base::result_type result_type;
  110. operator result_type() const
  111. {
  112. return (*this)();
  113. }
  114. extract(PyObject*);
  115. extract(api::object const&);
  116. };
  117. //
  118. // Implementations
  119. //
  120. template <class T>
  121. inline extract<T>::extract(PyObject* o)
  122. : base(o)
  123. {
  124. }
  125. template <class T>
  126. inline extract<T>::extract(api::object const& o)
  127. : base(o.ptr())
  128. {
  129. }
  130. namespace converter
  131. {
  132. template <class T>
  133. inline extract_rvalue<T>::extract_rvalue(PyObject* x)
  134. : m_source(x)
  135. , m_data(
  136. (rvalue_from_python_stage1)(x, registered<T>::converters)
  137. )
  138. {
  139. }
  140. template <class T>
  141. inline bool
  142. extract_rvalue<T>::check() const
  143. {
  144. return m_data.stage1.convertible;
  145. }
  146. template <class T>
  147. inline typename extract_rvalue<T>::result_type
  148. extract_rvalue<T>::operator()() const
  149. {
  150. return *(T*)(
  151. // Only do the stage2 conversion once
  152. m_data.stage1.convertible == m_data.storage.bytes
  153. ? m_data.storage.bytes
  154. : (rvalue_from_python_stage2)(m_source, m_data.stage1, registered<T>::converters)
  155. );
  156. }
  157. template <class Ref>
  158. inline extract_reference<Ref>::extract_reference(PyObject* obj)
  159. : m_source(obj)
  160. , m_result(
  161. (get_lvalue_from_python)(obj, registered<Ref>::converters)
  162. )
  163. {
  164. }
  165. template <class Ref>
  166. inline bool extract_reference<Ref>::check() const
  167. {
  168. return m_result != 0;
  169. }
  170. template <class Ref>
  171. inline Ref extract_reference<Ref>::operator()() const
  172. {
  173. if (m_result == 0)
  174. (throw_no_reference_from_python)(m_source, registered<Ref>::converters);
  175. return python::detail::void_ptr_to_reference(m_result, (Ref(*)())0);
  176. }
  177. template <class Ptr>
  178. inline extract_pointer<Ptr>::extract_pointer(PyObject* obj)
  179. : m_source(obj)
  180. , m_result(
  181. obj == Py_None ? 0 : (get_lvalue_from_python)(obj, registered_pointee<Ptr>::converters)
  182. )
  183. {
  184. }
  185. template <class Ptr>
  186. inline bool extract_pointer<Ptr>::check() const
  187. {
  188. return m_source == Py_None || m_result != 0;
  189. }
  190. template <class Ptr>
  191. inline Ptr extract_pointer<Ptr>::operator()() const
  192. {
  193. if (m_result == 0 && m_source != Py_None)
  194. (throw_no_pointer_from_python)(m_source, registered_pointee<Ptr>::converters);
  195. return Ptr(m_result);
  196. }
  197. template <class T>
  198. inline extract_object_manager<T>::extract_object_manager(PyObject* obj)
  199. : m_source(obj)
  200. {
  201. }
  202. template <class T>
  203. inline bool extract_object_manager<T>::check() const
  204. {
  205. return object_manager_traits<T>::check(m_source);
  206. }
  207. template <class T>
  208. inline T extract_object_manager<T>::operator()() const
  209. {
  210. return T(
  211. object_manager_traits<T>::adopt(python::incref(m_source))
  212. );
  213. }
  214. }
  215. }} // namespace boost::python::converter
  216. #endif // EXTRACT_DWA200265_HPP