proxy.hpp 2.7 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 PROXY_DWA2002615_HPP
  6. # define PROXY_DWA2002615_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/python/object_core.hpp>
  9. # include <boost/python/object_operators.hpp>
  10. namespace boost { namespace python { namespace api {
  11. template <class Policies>
  12. class proxy : public object_operators<proxy<Policies> >
  13. {
  14. typedef typename Policies::key_type key_type;
  15. # if !defined(BOOST_MSVC) || BOOST_MSVC >= 1300
  16. typedef proxy const& assignment_self;
  17. # else
  18. typedef proxy assignment_self;
  19. # endif
  20. public:
  21. proxy(object const& target, key_type const& key);
  22. operator object() const;
  23. // to support a[b] = c[d]
  24. proxy const& operator=(assignment_self) const;
  25. template <class T>
  26. inline proxy const& operator=(T const& rhs) const
  27. {
  28. Policies::set(m_target, m_key, object(rhs));
  29. return *this;
  30. }
  31. public: // implementation detail
  32. void del() const;
  33. private:
  34. object m_target;
  35. key_type m_key;
  36. };
  37. template <class T>
  38. inline void del(proxy<T> const& x)
  39. {
  40. x.del();
  41. }
  42. //
  43. // implementation
  44. //
  45. template <class Policies>
  46. inline proxy<Policies>::proxy(object const& target, key_type const& key)
  47. : m_target(target), m_key(key)
  48. {}
  49. template <class Policies>
  50. inline proxy<Policies>::operator object() const
  51. {
  52. return Policies::get(m_target, m_key);
  53. }
  54. // to support a[b] = c[d]
  55. template <class Policies>
  56. inline proxy<Policies> const& proxy<Policies>::operator=(typename proxy::assignment_self rhs) const
  57. {
  58. return *this = python::object(rhs);
  59. }
  60. # define BOOST_PYTHON_PROXY_INPLACE(op) \
  61. template <class Policies, class R> \
  62. proxy<Policies> const& operator op(proxy<Policies> const& lhs, R const& rhs) \
  63. { \
  64. object old(lhs); \
  65. return lhs = (old op rhs); \
  66. }
  67. BOOST_PYTHON_PROXY_INPLACE(+=)
  68. BOOST_PYTHON_PROXY_INPLACE(-=)
  69. BOOST_PYTHON_PROXY_INPLACE(*=)
  70. BOOST_PYTHON_PROXY_INPLACE(/=)
  71. BOOST_PYTHON_PROXY_INPLACE(%=)
  72. BOOST_PYTHON_PROXY_INPLACE(<<=)
  73. BOOST_PYTHON_PROXY_INPLACE(>>=)
  74. BOOST_PYTHON_PROXY_INPLACE(&=)
  75. BOOST_PYTHON_PROXY_INPLACE(^=)
  76. BOOST_PYTHON_PROXY_INPLACE(|=)
  77. # undef BOOST_PYTHON_PROXY_INPLACE
  78. template <class Policies>
  79. inline void proxy<Policies>::del() const
  80. {
  81. Policies::del(m_target, m_key);
  82. }
  83. }}} // namespace boost::python::api
  84. #endif // PROXY_DWA2002615_HPP