back_reference.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. template<typename T>
  27. class is_back_reference
  28. {
  29. public:
  30. BOOST_STATIC_CONSTANT(bool, value = false);
  31. };
  32. template<typename T>
  33. class is_back_reference<back_reference<T> >
  34. {
  35. public:
  36. BOOST_STATIC_CONSTANT(bool, value = true);
  37. };
  38. //
  39. // implementations
  40. //
  41. template <class T>
  42. back_reference<T>::back_reference(PyObject* p, T x)
  43. : m_source(detail::borrowed_reference(p))
  44. , m_value(x)
  45. {
  46. }
  47. template <class T>
  48. typename back_reference<T>::source_t const& back_reference<T>::source() const
  49. {
  50. return m_source;
  51. }
  52. template <class T>
  53. T back_reference<T>::get() const
  54. {
  55. return m_value;
  56. }
  57. }} // namespace boost::python
  58. #endif // BACK_REFERENCE_DWA2002510_HPP