File indexing completed on 2025-02-22 10:26:16
0001
0002
0003
0004
0005
0006 #ifndef INDEXING_SUITE_JDG20036_HPP
0007 # define INDEXING_SUITE_JDG20036_HPP
0008
0009 # include <boost/python/class.hpp>
0010 # include <boost/python/def_visitor.hpp>
0011 # include <boost/python/register_ptr_to_python.hpp>
0012 # include <boost/python/suite/indexing/detail/indexing_suite_detail.hpp>
0013 # include <boost/python/return_internal_reference.hpp>
0014 # include <boost/python/iterator.hpp>
0015 # include <boost/mpl/or.hpp>
0016 # include <boost/mpl/not.hpp>
0017 # include <boost/python/detail/type_traits.hpp>
0018
0019 namespace boost { namespace python {
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098 template <
0099 class Container
0100 , class DerivedPolicies
0101 , bool NoProxy = false
0102 , bool NoSlice = false
0103 , class Data = typename Container::value_type
0104 , class Index = typename Container::size_type
0105 , class Key = typename Container::value_type
0106 >
0107 class indexing_suite
0108 : public def_visitor<
0109 indexing_suite<
0110 Container
0111 , DerivedPolicies
0112 , NoProxy
0113 , NoSlice
0114 , Data
0115 , Index
0116 , Key
0117 > >
0118 {
0119 private:
0120
0121 typedef mpl::or_<
0122 mpl::bool_<NoProxy>
0123 , mpl::not_<is_class<Data> >
0124 , typename mpl::or_<
0125 detail::is_same<Data, std::string>
0126 , detail::is_same<Data, std::complex<float> >
0127 , detail::is_same<Data, std::complex<double> >
0128 , detail::is_same<Data, std::complex<long double> > >::type>
0129 no_proxy;
0130
0131 typedef detail::container_element<Container, Index, DerivedPolicies>
0132 container_element_t;
0133
0134 typedef return_internal_reference<> return_policy;
0135
0136 typedef typename mpl::if_<
0137 no_proxy
0138 , iterator<Container>
0139 , iterator<Container, return_policy> >::type
0140 def_iterator;
0141
0142 typedef typename mpl::if_<
0143 no_proxy
0144 , detail::no_proxy_helper<
0145 Container
0146 , DerivedPolicies
0147 , container_element_t
0148 , Index>
0149 , detail::proxy_helper<
0150 Container
0151 , DerivedPolicies
0152 , container_element_t
0153 , Index> >::type
0154 proxy_handler;
0155
0156 typedef typename mpl::if_<
0157 mpl::bool_<NoSlice>
0158 , detail::no_slice_helper<
0159 Container
0160 , DerivedPolicies
0161 , proxy_handler
0162 , Data
0163 , Index>
0164 , detail::slice_helper<
0165 Container
0166 , DerivedPolicies
0167 , proxy_handler
0168 , Data
0169 , Index> >::type
0170 slice_handler;
0171
0172 public:
0173
0174 template <class Class>
0175 void visit(Class& cl) const
0176 {
0177
0178 proxy_handler::register_container_element();
0179
0180 cl
0181 .def("__len__", base_size)
0182 .def("__setitem__", &base_set_item)
0183 .def("__delitem__", &base_delete_item)
0184 .def("__getitem__", &base_get_item)
0185 .def("__contains__", &base_contains)
0186 .def("__iter__", def_iterator())
0187 ;
0188
0189 DerivedPolicies::extension_def(cl);
0190 }
0191
0192 template <class Class>
0193 static void
0194 extension_def(Class& cl)
0195 {
0196
0197
0198 }
0199
0200 private:
0201
0202 static object
0203 base_get_item(back_reference<Container&> container, PyObject* i)
0204 {
0205 if (PySlice_Check(i))
0206 return slice_handler::base_get_slice(
0207 container.get(), static_cast<PySliceObject*>(static_cast<void*>(i)));
0208
0209 return proxy_handler::base_get_item_(container, i);
0210 }
0211
0212 static void
0213 base_set_item(Container& container, PyObject* i, PyObject* v)
0214 {
0215 if (PySlice_Check(i))
0216 {
0217 slice_handler::base_set_slice(container,
0218 static_cast<PySliceObject*>(static_cast<void*>(i)), v);
0219 }
0220 else
0221 {
0222 extract<Data&> elem(v);
0223
0224 if (elem.check())
0225 {
0226 DerivedPolicies::
0227 set_item(container,
0228 DerivedPolicies::
0229 convert_index(container, i), elem());
0230 }
0231 else
0232 {
0233
0234 extract<Data> elem(v);
0235 if (elem.check())
0236 {
0237 DerivedPolicies::
0238 set_item(container,
0239 DerivedPolicies::
0240 convert_index(container, i), elem());
0241 }
0242 else
0243 {
0244 PyErr_SetString(PyExc_TypeError, "Invalid assignment");
0245 throw_error_already_set();
0246 }
0247 }
0248 }
0249 }
0250
0251 static void
0252 base_delete_item(Container& container, PyObject* i)
0253 {
0254 if (PySlice_Check(i))
0255 {
0256 slice_handler::base_delete_slice(
0257 container, static_cast<PySliceObject*>(static_cast<void*>(i)));
0258 return;
0259 }
0260
0261 Index index = DerivedPolicies::convert_index(container, i);
0262 proxy_handler::base_erase_index(container, index, mpl::bool_<NoSlice>());
0263 DerivedPolicies::delete_item(container, index);
0264 }
0265
0266 static size_t
0267 base_size(Container& container)
0268 {
0269 return DerivedPolicies::size(container);
0270 }
0271
0272 static bool
0273 base_contains(Container& container, PyObject* key)
0274 {
0275 extract<Key const&> x(key);
0276
0277 if (x.check())
0278 {
0279 return DerivedPolicies::contains(container, x());
0280 }
0281 else
0282 {
0283
0284 extract<Key> x(key);
0285 if (x.check())
0286 return DerivedPolicies::contains(container, x());
0287 else
0288 return false;
0289 }
0290 }
0291 };
0292
0293 }}
0294
0295 #endif