rvalue_from_python_data.hpp 6.1 KB

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