object_manager.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 OBJECT_MANAGER_DWA2002614_HPP
  6. # define OBJECT_MANAGER_DWA2002614_HPP
  7. # include <boost/python/handle.hpp>
  8. # include <boost/python/cast.hpp>
  9. # include <boost/python/converter/pyobject_traits.hpp>
  10. # include <boost/python/detail/type_traits.hpp>
  11. # include <boost/mpl/if.hpp>
  12. # include <boost/python/detail/indirect_traits.hpp>
  13. # include <boost/mpl/bool.hpp>
  14. // Facilities for dealing with types which always manage Python
  15. // objects. Some examples are object, list, str, et. al. Different
  16. // to_python/from_python conversion rules apply here because in
  17. // contrast to other types which are typically embedded inside a
  18. // Python object, these are wrapped around a Python object. For most
  19. // object managers T, a C++ non-const T reference argument does not
  20. // imply the existence of a T lvalue embedded in the corresponding
  21. // Python argument, since mutating member functions on T actually only
  22. // modify the held Python object.
  23. //
  24. // handle<T> is an object manager, though strictly speaking it should
  25. // not be. In other words, even though mutating member functions of
  26. // hanlde<T> actually modify the handle<T> and not the T object,
  27. // handle<T>& arguments of wrapped functions will bind to "rvalues"
  28. // wrapping the actual Python argument, just as with other object
  29. // manager classes. Making an exception for handle<T> is simply not
  30. // worth the trouble.
  31. //
  32. // borrowed<T> cv* is an object manager so that we can use the general
  33. // to_python mechanisms to convert raw Python object pointers to
  34. // python, without the usual semantic problems of using raw pointers.
  35. // Object Manager Concept requirements:
  36. //
  37. // T is an Object Manager
  38. // p is a PyObject*
  39. // x is a T
  40. //
  41. // * object_manager_traits<T>::is_specialized == true
  42. //
  43. // * T(detail::borrowed_reference(p))
  44. // Manages p without checking its type
  45. //
  46. // * get_managed_object(x, boost::python::tag)
  47. // Convertible to PyObject*
  48. //
  49. // Additional requirements if T can be converted from_python:
  50. //
  51. // * T(object_manager_traits<T>::adopt(p))
  52. // steals a reference to p, or throws a TypeError exception if
  53. // p doesn't have an appropriate type. May assume p is non-null
  54. //
  55. // * X::check(p)
  56. // convertible to bool. True iff T(X::construct(p)) will not
  57. // throw.
  58. // Forward declarations
  59. //
  60. namespace boost { namespace python
  61. {
  62. namespace api
  63. {
  64. class object;
  65. }
  66. }}
  67. namespace boost { namespace python { namespace converter {
  68. // Specializations for handle<T>
  69. template <class T>
  70. struct handle_object_manager_traits
  71. : pyobject_traits<typename T::element_type>
  72. {
  73. private:
  74. typedef pyobject_traits<typename T::element_type> base;
  75. public:
  76. BOOST_STATIC_CONSTANT(bool, is_specialized = true);
  77. // Initialize with a null_ok pointer for efficiency, bypassing the
  78. // null check since the source is always non-null.
  79. static null_ok<typename T::element_type>* adopt(PyObject* p)
  80. {
  81. return python::allow_null(base::checked_downcast(p));
  82. }
  83. };
  84. template <class T>
  85. struct default_object_manager_traits
  86. {
  87. BOOST_STATIC_CONSTANT(
  88. bool, is_specialized = python::detail::is_borrowed_ptr<T>::value
  89. );
  90. };
  91. template <class T>
  92. struct object_manager_traits
  93. : mpl::if_c<
  94. is_handle<T>::value
  95. , handle_object_manager_traits<T>
  96. , default_object_manager_traits<T>
  97. >::type
  98. {
  99. };
  100. //
  101. // Traits for detecting whether a type is an object manager or a
  102. // (cv-qualified) reference to an object manager.
  103. //
  104. template <class T>
  105. struct is_object_manager
  106. : mpl::bool_<object_manager_traits<T>::is_specialized>
  107. {
  108. };
  109. template <class T>
  110. struct is_reference_to_object_manager
  111. : mpl::false_
  112. {
  113. };
  114. template <class T>
  115. struct is_reference_to_object_manager<T&>
  116. : is_object_manager<T>
  117. {
  118. };
  119. template <class T>
  120. struct is_reference_to_object_manager<T const&>
  121. : is_object_manager<T>
  122. {
  123. };
  124. template <class T>
  125. struct is_reference_to_object_manager<T volatile&>
  126. : is_object_manager<T>
  127. {
  128. };
  129. template <class T>
  130. struct is_reference_to_object_manager<T const volatile&>
  131. : is_object_manager<T>
  132. {
  133. };
  134. }}} // namespace boost::python::converter
  135. #endif // OBJECT_MANAGER_DWA2002614_HPP