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