fix doc example typo
[boost.git] / boost / python / proxy.hpp
bloba956eac1cc7e062c974364a7237ae6299c0882bc
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 # 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;
27 // to support a[b] = c[d]
28 proxy const& operator=(assignment_self) const;
30 template <class T>
31 inline proxy const& operator=(T const& rhs) const
33 Policies::set(m_target, m_key, object(rhs));
34 return *this;
37 public: // implementation detail
38 void del() const;
40 private:
41 object m_target;
42 key_type m_key;
46 template <class T>
47 inline void del(proxy<T> const& x)
49 x.del();
53 // implementation
56 template <class Policies>
57 inline proxy<Policies>::proxy(object const& target, key_type const& key)
58 : m_target(target), m_key(key)
61 template <class Policies>
62 inline proxy<Policies>::operator object() const
64 return Policies::get(m_target, m_key);
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
71 return *this = python::object(rhs);
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); \
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
93 template <class Policies>
94 inline void proxy<Policies>::del() const
96 Policies::del(m_target, m_key);
99 }}} // namespace boost::python::api
101 #endif // PROXY_DWA2002615_HPP