extract.hpp 5.9 KB

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