back_reference.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 BACK_REFERENCE_DWA2002510_HPP
  6. # define BACK_REFERENCE_DWA2002510_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/python/object_fwd.hpp>
  9. # include <boost/python/detail/dependent.hpp>
  10. # include <boost/python/detail/raw_pyobject.hpp>
  11. namespace boost { namespace python {
  12. template <class T>
  13. struct back_reference
  14. {
  15. private: // types
  16. typedef typename detail::dependent<object,T>::type source_t;
  17. public:
  18. typedef T type;
  19. back_reference(PyObject*, T);
  20. source_t const& source() const;
  21. T get() const;
  22. private:
  23. source_t m_source;
  24. T m_value;
  25. };
  26. # ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  27. template<typename T>
  28. class is_back_reference
  29. {
  30. public:
  31. BOOST_STATIC_CONSTANT(bool, value = false);
  32. };
  33. template<typename T>
  34. class is_back_reference<back_reference<T> >
  35. {
  36. public:
  37. BOOST_STATIC_CONSTANT(bool, value = true);
  38. };
  39. # else // no partial specialization
  40. }} // namespace boost::python
  41. #include <boost/type.hpp>
  42. namespace boost { namespace python {
  43. namespace detail
  44. {
  45. typedef char (&yes_back_reference_t)[1];
  46. typedef char (&no_back_reference_t)[2];
  47. no_back_reference_t is_back_reference_test(...);
  48. template<typename T>
  49. yes_back_reference_t is_back_reference_test(boost::type< back_reference<T> >);
  50. }
  51. template<typename T>
  52. class is_back_reference
  53. {
  54. public:
  55. BOOST_STATIC_CONSTANT(
  56. bool, value = (
  57. sizeof(detail::is_back_reference_test(boost::type<T>()))
  58. == sizeof(detail::yes_back_reference_t)));
  59. };
  60. # endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  61. //
  62. // implementations
  63. //
  64. template <class T>
  65. back_reference<T>::back_reference(PyObject* p, T x)
  66. : m_source(detail::borrowed_reference(p))
  67. , m_value(x)
  68. {
  69. }
  70. template <class T>
  71. typename back_reference<T>::source_t const& back_reference<T>::source() const
  72. {
  73. return m_source;
  74. }
  75. template <class T>
  76. T back_reference<T>::get() const
  77. {
  78. return m_value;
  79. }
  80. }} // namespace boost::python
  81. #endif // BACK_REFERENCE_DWA2002510_HPP