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>
11 namespace boost { namespace python { namespace api {
13 template <class Policies>
14 class proxy : public object_operators<proxy<Policies> >
16 typedef typename Policies::key_type key_type;
18 typedef proxy const& assignment_self;
20 proxy(object const& target, key_type const& key);
21 operator object() const;
23 // to support a[b] = c[d]
24 proxy const& operator=(assignment_self) const;
27 inline proxy const& operator=(T const& rhs) const
29 Policies::set(m_target, m_key, object(rhs));
33 public: // implementation detail
43 inline void del(proxy<T> const& x)
52 template <class Policies>
53 inline proxy<Policies>::proxy(object const& target, key_type const& key)
54 : m_target(target), m_key(key)
57 template <class Policies>
58 inline proxy<Policies>::operator object() const
60 return Policies::get(m_target, m_key);
63 // to support a[b] = c[d]
64 template <class Policies>
65 inline proxy<Policies> const& proxy<Policies>::operator=(typename proxy::assignment_self rhs) const
67 return *this = python::object(rhs);
70 # define BOOST_PYTHON_PROXY_INPLACE(op) \
71 template <class Policies, class R> \
72 proxy<Policies> const& operator op(proxy<Policies> const& lhs, R const& rhs) \
75 return lhs = (old op rhs); \
77 BOOST_PYTHON_PROXY_INPLACE(+=)
78 BOOST_PYTHON_PROXY_INPLACE(-=)
79 BOOST_PYTHON_PROXY_INPLACE(*=)
80 BOOST_PYTHON_PROXY_INPLACE(/=)
81 BOOST_PYTHON_PROXY_INPLACE(%=)
82 BOOST_PYTHON_PROXY_INPLACE(<<=)
83 BOOST_PYTHON_PROXY_INPLACE(>>=)
84 BOOST_PYTHON_PROXY_INPLACE(&=)
85 BOOST_PYTHON_PROXY_INPLACE(^=)
86 BOOST_PYTHON_PROXY_INPLACE(|=)
87 # undef BOOST_PYTHON_PROXY_INPLACE
89 template <class Policies>
90 inline void proxy<Policies>::del() const
92 Policies::del(m_target, m_key);
95 }}} // namespace boost::python::api
97 #endif // PROXY_DWA2002615_HPP