borrowed_ptr.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef BORROWED_PTR_DWA20020601_HPP
  2. # define BORROWED_PTR_DWA20020601_HPP
  3. // Copyright David Abrahams 2002.
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. # include <boost/config.hpp>
  8. # include <boost/type.hpp>
  9. # include <boost/mpl/if.hpp>
  10. # include <boost/type_traits/object_traits.hpp>
  11. # include <boost/type_traits/cv_traits.hpp>
  12. # include <boost/python/tag.hpp>
  13. namespace boost { namespace python { namespace detail {
  14. template<class T> class borrowed
  15. {
  16. typedef T type;
  17. };
  18. # ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  19. template<typename T>
  20. struct is_borrowed_ptr
  21. {
  22. BOOST_STATIC_CONSTANT(bool, value = false);
  23. };
  24. # if !defined(__MWERKS__) || __MWERKS__ > 0x3000
  25. template<typename T>
  26. struct is_borrowed_ptr<borrowed<T>*>
  27. {
  28. BOOST_STATIC_CONSTANT(bool, value = true);
  29. };
  30. template<typename T>
  31. struct is_borrowed_ptr<borrowed<T> const*>
  32. {
  33. BOOST_STATIC_CONSTANT(bool, value = true);
  34. };
  35. template<typename T>
  36. struct is_borrowed_ptr<borrowed<T> volatile*>
  37. {
  38. BOOST_STATIC_CONSTANT(bool, value = true);
  39. };
  40. template<typename T>
  41. struct is_borrowed_ptr<borrowed<T> const volatile*>
  42. {
  43. BOOST_STATIC_CONSTANT(bool, value = true);
  44. };
  45. # else
  46. template<typename T>
  47. struct is_borrowed
  48. {
  49. BOOST_STATIC_CONSTANT(bool, value = false);
  50. };
  51. template<typename T>
  52. struct is_borrowed<borrowed<T> >
  53. {
  54. BOOST_STATIC_CONSTANT(bool, value = true);
  55. };
  56. template<typename T>
  57. struct is_borrowed_ptr<T*>
  58. : is_borrowed<typename remove_cv<T>::type>
  59. {
  60. };
  61. # endif
  62. # else // no partial specialization
  63. typedef char (&yes_borrowed_ptr_t)[1];
  64. typedef char (&no_borrowed_ptr_t)[2];
  65. no_borrowed_ptr_t is_borrowed_ptr_test(...);
  66. template <class T>
  67. typename mpl::if_c<
  68. is_pointer<T>::value
  69. , T
  70. , int
  71. >::type
  72. is_borrowed_ptr_test1(boost::type<T>);
  73. template<typename T>
  74. yes_borrowed_ptr_t is_borrowed_ptr_test(borrowed<T> const volatile*);
  75. template<typename T>
  76. class is_borrowed_ptr
  77. {
  78. public:
  79. BOOST_STATIC_CONSTANT(
  80. bool, value = (
  81. sizeof(detail::is_borrowed_ptr_test(is_borrowed_ptr_test1(boost::type<T>())))
  82. == sizeof(detail::yes_borrowed_ptr_t)));
  83. };
  84. # endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  85. }
  86. template <class T>
  87. inline T* get_managed_object(detail::borrowed<T> const volatile* p, tag_t)
  88. {
  89. return (T*)p;
  90. }
  91. }} // namespace boost::python::detail
  92. #endif // #ifndef BORROWED_PTR_DWA20020601_HPP