os/ossrv/ossrv_pub/boost_apis/boost/python/proxy.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright David Abrahams 2002.
sl@0
     2
// Distributed under the Boost Software License, Version 1.0. (See
sl@0
     3
// accompanying file LICENSE_1_0.txt or copy at
sl@0
     4
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
     5
#ifndef PROXY_DWA2002615_HPP
sl@0
     6
# define PROXY_DWA2002615_HPP
sl@0
     7
# include <boost/python/detail/prefix.hpp>
sl@0
     8
# include <boost/python/object_core.hpp>
sl@0
     9
# include <boost/python/object_operators.hpp>
sl@0
    10
sl@0
    11
namespace boost { namespace python { namespace api {
sl@0
    12
sl@0
    13
template <class Policies>
sl@0
    14
class proxy : public object_operators<proxy<Policies> >
sl@0
    15
{
sl@0
    16
    typedef typename Policies::key_type key_type;
sl@0
    17
    
sl@0
    18
# if !defined(BOOST_MSVC) || BOOST_MSVC >= 1300
sl@0
    19
    typedef proxy const& assignment_self;
sl@0
    20
# else
sl@0
    21
    typedef proxy assignment_self;
sl@0
    22
# endif
sl@0
    23
 public:
sl@0
    24
    proxy(object const& target, key_type const& key);
sl@0
    25
    operator object() const;
sl@0
    26
sl@0
    27
    // to support a[b] = c[d]
sl@0
    28
    proxy const& operator=(assignment_self) const;
sl@0
    29
    
sl@0
    30
    template <class T>
sl@0
    31
    inline proxy const& operator=(T const& rhs) const
sl@0
    32
    {
sl@0
    33
        Policies::set(m_target, m_key, object(rhs));
sl@0
    34
        return *this;
sl@0
    35
    }
sl@0
    36
sl@0
    37
 public: // implementation detail
sl@0
    38
    void del() const;
sl@0
    39
        
sl@0
    40
 private:
sl@0
    41
    object m_target;
sl@0
    42
    key_type m_key;
sl@0
    43
};
sl@0
    44
sl@0
    45
sl@0
    46
template <class T>
sl@0
    47
inline void del(proxy<T> const& x)
sl@0
    48
{
sl@0
    49
    x.del();
sl@0
    50
}
sl@0
    51
sl@0
    52
//
sl@0
    53
// implementation
sl@0
    54
//
sl@0
    55
sl@0
    56
template <class Policies>
sl@0
    57
inline proxy<Policies>::proxy(object const& target, key_type const& key)
sl@0
    58
    : m_target(target), m_key(key)
sl@0
    59
{}
sl@0
    60
sl@0
    61
template <class Policies>
sl@0
    62
inline proxy<Policies>::operator object() const
sl@0
    63
{
sl@0
    64
    return Policies::get(m_target, m_key);
sl@0
    65
}
sl@0
    66
sl@0
    67
// to support a[b] = c[d]
sl@0
    68
template <class Policies>
sl@0
    69
inline proxy<Policies> const& proxy<Policies>::operator=(typename proxy::assignment_self rhs) const
sl@0
    70
{
sl@0
    71
    return *this = python::object(rhs);
sl@0
    72
}
sl@0
    73
sl@0
    74
# define BOOST_PYTHON_PROXY_INPLACE(op)                                         \
sl@0
    75
template <class Policies, class R>                                              \
sl@0
    76
proxy<Policies> const& operator op(proxy<Policies> const& lhs, R const& rhs)    \
sl@0
    77
{                                                                               \
sl@0
    78
    object old(lhs);                                                            \
sl@0
    79
    return lhs = (old op rhs);                                                  \
sl@0
    80
} 
sl@0
    81
BOOST_PYTHON_PROXY_INPLACE(+=)
sl@0
    82
BOOST_PYTHON_PROXY_INPLACE(-=)
sl@0
    83
BOOST_PYTHON_PROXY_INPLACE(*=)
sl@0
    84
BOOST_PYTHON_PROXY_INPLACE(/=)
sl@0
    85
BOOST_PYTHON_PROXY_INPLACE(%=)
sl@0
    86
BOOST_PYTHON_PROXY_INPLACE(<<=)
sl@0
    87
BOOST_PYTHON_PROXY_INPLACE(>>=)
sl@0
    88
BOOST_PYTHON_PROXY_INPLACE(&=)
sl@0
    89
BOOST_PYTHON_PROXY_INPLACE(^=)
sl@0
    90
BOOST_PYTHON_PROXY_INPLACE(|=)
sl@0
    91
# undef BOOST_PYTHON_PROXY_INPLACE
sl@0
    92
sl@0
    93
template <class Policies>
sl@0
    94
inline void proxy<Policies>::del() const
sl@0
    95
{
sl@0
    96
    Policies::del(m_target, m_key);
sl@0
    97
}
sl@0
    98
sl@0
    99
}}} // namespace boost::python::api
sl@0
   100
sl@0
   101
#endif // PROXY_DWA2002615_HPP