| 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 OBJECT_SLICES_DWA2002615_HPP | 
|---|
| 6 | # define OBJECT_SLICES_DWA2002615_HPP | 
|---|
| 7 |  | 
|---|
| 8 | # include <boost/python/detail/prefix.hpp> | 
|---|
| 9 |  | 
|---|
| 10 | # include <boost/python/proxy.hpp> | 
|---|
| 11 | # include <boost/python/object_core.hpp> | 
|---|
| 12 | # include <boost/python/object_protocol.hpp> | 
|---|
| 13 | # include <boost/python/handle.hpp> | 
|---|
| 14 | # include <utility> | 
|---|
| 15 |  | 
|---|
| 16 | namespace boost { namespace python { namespace api { | 
|---|
| 17 |  | 
|---|
| 18 | struct const_slice_policies | 
|---|
| 19 | { | 
|---|
| 20 | typedef std::pair<handle<>, handle<> > key_type; | 
|---|
| 21 | static object get(object const& target, key_type const& key); | 
|---|
| 22 | }; | 
|---|
| 23 |  | 
|---|
| 24 | struct slice_policies : const_slice_policies | 
|---|
| 25 | { | 
|---|
| 26 | static object const& set(object const& target, key_type const& key, object const& value); | 
|---|
| 27 | static void del(object const& target, key_type const& key); | 
|---|
| 28 | }; | 
|---|
| 29 |  | 
|---|
| 30 | // | 
|---|
| 31 | // implementation | 
|---|
| 32 | // | 
|---|
| 33 | template <class U> | 
|---|
| 34 | object_slice | 
|---|
| 35 | object_operators<U>::slice(object_cref start, object_cref finish) | 
|---|
| 36 | { | 
|---|
| 37 | object_cref2 x = *static_cast<U*>(this); | 
|---|
| 38 | return object_slice(x, std::make_pair(borrowed(start.ptr()), borrowed(finish.ptr()))); | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | template <class U> | 
|---|
| 42 | const_object_slice | 
|---|
| 43 | object_operators<U>::slice(object_cref start, object_cref finish) const | 
|---|
| 44 | { | 
|---|
| 45 | object_cref2 x = *static_cast<U const*>(this); | 
|---|
| 46 | return const_object_slice(x, std::make_pair(borrowed(start.ptr()), borrowed(finish.ptr()))); | 
|---|
| 47 | } | 
|---|
| 48 |  | 
|---|
| 49 | template <class U> | 
|---|
| 50 | object_slice | 
|---|
| 51 | object_operators<U>::slice(slice_nil, object_cref finish) | 
|---|
| 52 | { | 
|---|
| 53 | object_cref2 x = *static_cast<U*>(this); | 
|---|
| 54 | return object_slice(x, std::make_pair(allow_null((PyObject*)0), borrowed(finish.ptr()))); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | template <class U> | 
|---|
| 58 | const_object_slice | 
|---|
| 59 | object_operators<U>::slice(slice_nil, object_cref finish) const | 
|---|
| 60 | { | 
|---|
| 61 | object_cref2 x = *static_cast<U const*>(this); | 
|---|
| 62 | return const_object_slice(x, std::make_pair(allow_null((PyObject*)0), borrowed(finish.ptr()))); | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | template <class U> | 
|---|
| 66 | object_slice | 
|---|
| 67 | object_operators<U>::slice(slice_nil, slice_nil) | 
|---|
| 68 | { | 
|---|
| 69 | object_cref2 x = *static_cast<U*>(this); | 
|---|
| 70 | return object_slice(x, std::make_pair(allow_null((PyObject*)0), allow_null((PyObject*)0))); | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | template <class U> | 
|---|
| 74 | const_object_slice | 
|---|
| 75 | object_operators<U>::slice(slice_nil, slice_nil) const | 
|---|
| 76 | { | 
|---|
| 77 | object_cref2 x = *static_cast<U const*>(this); | 
|---|
| 78 | return const_object_slice(x, std::make_pair(allow_null((PyObject*)0), allow_null((PyObject*)0))); | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | template <class U> | 
|---|
| 82 | object_slice | 
|---|
| 83 | object_operators<U>::slice(object_cref start, slice_nil) | 
|---|
| 84 | { | 
|---|
| 85 | object_cref2 x = *static_cast<U*>(this); | 
|---|
| 86 | return object_slice(x, std::make_pair(borrowed(start.ptr()), allow_null((PyObject*)0))); | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | template <class U> | 
|---|
| 90 | const_object_slice | 
|---|
| 91 | object_operators<U>::slice(object_cref start, slice_nil) const | 
|---|
| 92 | { | 
|---|
| 93 | object_cref2 x = *static_cast<U const*>(this); | 
|---|
| 94 | return const_object_slice(x, std::make_pair(borrowed(start.ptr()), allow_null((PyObject*)0))); | 
|---|
| 95 | } | 
|---|
| 96 | # if !defined(BOOST_MSVC) || BOOST_MSVC > 1300 | 
|---|
| 97 | template <class U> | 
|---|
| 98 | template <class T, class V> | 
|---|
| 99 | inline const_object_slice | 
|---|
| 100 | object_operators<U>::slice(T const& start, V const& end) const | 
|---|
| 101 | { | 
|---|
| 102 | return this->slice( | 
|---|
| 103 | typename slice_bound<T>::type(start) | 
|---|
| 104 | , typename slice_bound<V>::type(end)); | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | template <class U> | 
|---|
| 108 | template <class T, class V> | 
|---|
| 109 | inline object_slice | 
|---|
| 110 | object_operators<U>::slice(T const& start, V const& end) | 
|---|
| 111 | { | 
|---|
| 112 | return this->slice( | 
|---|
| 113 | typename slice_bound<T>::type(start) | 
|---|
| 114 | , typename slice_bound<V>::type(end)); | 
|---|
| 115 | } | 
|---|
| 116 | # endif | 
|---|
| 117 |  | 
|---|
| 118 |  | 
|---|
| 119 | inline object const_slice_policies::get(object const& target, key_type const& key) | 
|---|
| 120 | { | 
|---|
| 121 | return getslice(target, key.first, key.second); | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | inline object const& slice_policies::set( | 
|---|
| 125 | object const& target | 
|---|
| 126 | , key_type const& key | 
|---|
| 127 | , object const& value) | 
|---|
| 128 | { | 
|---|
| 129 | setslice(target, key.first, key.second, value); | 
|---|
| 130 | return value; | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | inline void slice_policies::del( | 
|---|
| 134 | object const& target | 
|---|
| 135 | , key_type const& key) | 
|---|
| 136 | { | 
|---|
| 137 | delslice(target, key.first, key.second); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | }}} // namespace boost::python::api | 
|---|
| 141 |  | 
|---|
| 142 | #endif // OBJECT_SLICES_DWA2002615_HPP | 
|---|