rvalue_from_python_data.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 FROM_PYTHON_AUX_DATA_DWA2002128_HPP
  6. # define FROM_PYTHON_AUX_DATA_DWA2002128_HPP
  7. # include <boost/python/converter/constructor_function.hpp>
  8. # include <boost/python/detail/referent_storage.hpp>
  9. # include <boost/python/detail/destroy.hpp>
  10. # include <boost/python/detail/type_traits.hpp>
  11. # include <boost/static_assert.hpp>
  12. # include <cstddef>
  13. // Data management for potential rvalue conversions from Python to C++
  14. // types. When a client requests a conversion to T* or T&, we
  15. // generally require that an object of type T exists in the source
  16. // Python object, and the code here does not apply**. This implements
  17. // conversions which may create new temporaries of type T. The classic
  18. // example is a conversion which converts a Python tuple to a
  19. // std::vector. Since no std::vector lvalue exists in the Python
  20. // object -- it must be created "on-the-fly" by the converter, and
  21. // which must manage the lifetime of the created object.
  22. //
  23. // Note that the client is not precluded from using a registered
  24. // lvalue conversion to T in this case. In other words, we will
  25. // happily accept a Python object which /does/ contain a std::vector
  26. // lvalue, provided an appropriate converter is registered. So, while
  27. // this is an rvalue conversion from the client's point-of-view, the
  28. // converter registry may serve up lvalue or rvalue conversions for
  29. // the target type.
  30. //
  31. // ** C++ argument from_python conversions to T const& are an
  32. // exception to the rule for references: since in C++, const
  33. // references can bind to temporary rvalues, we allow rvalue
  34. // converters to be chosen when the target type is T const& for some
  35. // T.
  36. namespace boost { namespace python { namespace converter {
  37. // Conversions begin by filling in and returning a copy of this
  38. // structure. The process looks up a converter in the rvalue converter
  39. // registry for the target type. It calls the convertible() function
  40. // of each registered converter, passing the source PyObject* as an
  41. // argument, until a non-null result is returned. This result goes in
  42. // the convertible field, and the converter's construct() function is
  43. // stored in the construct field.
  44. //
  45. // If no appropriate converter is found, conversion fails and the
  46. // convertible field is null. When used in argument conversion for
  47. // wrapped C++ functions, it causes overload resolution to reject the
  48. // current function but not to fail completely. If an exception is
  49. // thrown, overload resolution stops and the exception propagates back
  50. // through the caller.
  51. //
  52. // If an lvalue converter is matched, its convertible() function is
  53. // expected to return a pointer to the stored T object; its
  54. // construct() function will be NULL. The convertible() function of
  55. // rvalue converters may return any non-singular pointer; the actual
  56. // target object will only be available once the converter's
  57. // construct() function is called.
  58. struct rvalue_from_python_stage1_data
  59. {
  60. void* convertible;
  61. constructor_function construct;
  62. };
  63. // Augments rvalue_from_python_stage1_data by adding storage for
  64. // constructing an object of remove_reference<T>::type. The
  65. // construct() function of rvalue converters (stored in m_construct
  66. // above) will cast the rvalue_from_python_stage1_data to an
  67. // appropriate instantiation of this template in order to access that
  68. // storage.
  69. template <class T>
  70. struct rvalue_from_python_storage
  71. {
  72. rvalue_from_python_stage1_data stage1;
  73. // Storage for the result, in case an rvalue must be constructed
  74. typename python::detail::referent_storage<
  75. typename boost::python::detail::add_lvalue_reference<T>::type
  76. >::type storage;
  77. };
  78. // Augments rvalue_from_python_storage<T> with a destructor. If
  79. // stage1.convertible == storage.bytes, it indicates that an object of
  80. // remove_reference<T>::type has been constructed in storage and
  81. // should will be destroyed in ~rvalue_from_python_data(). It is
  82. // crucial that successful rvalue conversions establish this equality
  83. // and that unsuccessful ones do not.
  84. template <class T>
  85. struct rvalue_from_python_data : rvalue_from_python_storage<T>
  86. {
  87. # if (!defined(__MWERKS__) || __MWERKS__ >= 0x3000) \
  88. && (!defined(__EDG_VERSION__) || __EDG_VERSION__ >= 245) \
  89. && (!defined(__DECCXX_VER) || __DECCXX_VER > 60590014) \
  90. && !defined(BOOST_PYTHON_SYNOPSIS) /* Synopsis' OpenCXX has trouble parsing this */
  91. // This must always be a POD struct with m_data its first member.
  92. BOOST_STATIC_ASSERT(BOOST_PYTHON_OFFSETOF(rvalue_from_python_storage<T>,stage1) == 0);
  93. # endif
  94. // The usual constructor
  95. rvalue_from_python_data(rvalue_from_python_stage1_data const&);
  96. // This constructor just sets m_convertible -- used by
  97. // implicitly_convertible<> to perform the final step of the
  98. // conversion, where the construct() function is already known.
  99. rvalue_from_python_data(void* convertible);
  100. // Destroys any object constructed in the storage.
  101. ~rvalue_from_python_data();
  102. private:
  103. typedef typename boost::python::detail::add_lvalue_reference<
  104. typename boost::python::detail::add_cv<T>::type>::type ref_type;
  105. };
  106. //
  107. // Implementataions
  108. //
  109. template <class T>
  110. inline rvalue_from_python_data<T>::rvalue_from_python_data(rvalue_from_python_stage1_data const& _stage1)
  111. {
  112. this->stage1 = _stage1;
  113. }
  114. template <class T>
  115. inline rvalue_from_python_data<T>::rvalue_from_python_data(void* convertible)
  116. {
  117. this->stage1.convertible = convertible;
  118. }
  119. template <class T>
  120. inline rvalue_from_python_data<T>::~rvalue_from_python_data()
  121. {
  122. if (this->stage1.convertible == this->storage.bytes)
  123. python::detail::destroy_referent<ref_type>(this->storage.bytes);
  124. }
  125. }}} // namespace boost::python::converter
  126. #endif // FROM_PYTHON_AUX_DATA_DWA2002128_HPP