• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     typedef proxy const& assignment_self;
19  public:
20     proxy(object const& target, key_type const& key);
21     operator object() const;
22 
23     // to support a[b] = c[d]
24     proxy const& operator=(assignment_self) const;
25 
26     template <class T>
operator =(T const & rhs) const27     inline proxy const& operator=(T const& rhs) const
28     {
29         Policies::set(m_target, m_key, object(rhs));
30         return *this;
31     }
32 
33  public: // implementation detail
34     void del() const;
35 
36  private:
37     object m_target;
38     key_type m_key;
39 };
40 
41 
42 template <class T>
del(proxy<T> const & x)43 inline void del(proxy<T> const& x)
44 {
45     x.del();
46 }
47 
48 //
49 // implementation
50 //
51 
52 template <class Policies>
proxy(object const & target,key_type const & key)53 inline proxy<Policies>::proxy(object const& target, key_type const& key)
54     : m_target(target), m_key(key)
55 {}
56 
57 template <class Policies>
operator object() const58 inline proxy<Policies>::operator object() const
59 {
60     return Policies::get(m_target, m_key);
61 }
62 
63 // to support a[b] = c[d]
64 template <class Policies>
operator =(typename proxy::assignment_self rhs) const65 inline proxy<Policies> const& proxy<Policies>::operator=(typename proxy::assignment_self rhs) const
66 {
67     return *this = python::object(rhs);
68 }
69 
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)    \
73 {                                                                               \
74     object old(lhs);                                                            \
75     return lhs = (old op rhs);                                                  \
76 }
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
88 
89 template <class Policies>
del() const90 inline void proxy<Policies>::del() const
91 {
92     Policies::del(m_target, m_key);
93 }
94 
95 }}} // namespace boost::python::api
96 
97 #endif // PROXY_DWA2002615_HPP
98