From fa70ddc2c52c6e6a3f0e1b71976263749a1fe6fc Mon Sep 17 00:00:00 2001 From: Joel de Guzman Date: Tue, 12 Aug 2003 21:21:47 +0000 Subject: [PATCH] Preparing for std::map suite [SVN r19566] --- .../python/suite/indexing/container_utils.hpp | 12 +-- .../indexing/detail/indexing_suite_detail.hpp | 26 +++--- .../python/suite/indexing/indexing_suite.hpp | 81 ++++++------------- .../suite/indexing/vector_indexing_suite.hpp | 65 +++++++++++++-- 4 files changed, 105 insertions(+), 79 deletions(-) diff --git a/include/boost/python/suite/indexing/container_utils.hpp b/include/boost/python/suite/indexing/container_utils.hpp index 9758fe78..2370fa8d 100644 --- a/include/boost/python/suite/indexing/container_utils.hpp +++ b/include/boost/python/suite/indexing/container_utils.hpp @@ -18,30 +18,30 @@ namespace boost { namespace python { namespace container_utils { void extend_container(Container& container, object l) { - typedef typename Container::value_type element_t; + typedef typename Container::value_type data_type; // l must be a list or some container for (int i = 0; i < l.attr("__len__")(); i++) { object elem(l[i]); - extract x(elem); - // try if elem is an exact element_t type + extract x(elem); + // try if elem is an exact data_type type if (x.check()) { container.push_back(x()); } else { - // try to convert elem to element_t type - extract x(elem); + // try to convert elem to data_type type + extract x(elem); if (x.check()) { container.push_back(x()); } else { - PyErr_SetString(PyExc_TypeError, "Incompatible Element Type"); + PyErr_SetString(PyExc_TypeError, "Incompatible Data Type"); throw_error_already_set(); } } diff --git a/include/boost/python/suite/indexing/detail/indexing_suite_detail.hpp b/include/boost/python/suite/indexing/detail/indexing_suite_detail.hpp index 8357ad36..282916ba 100644 --- a/include/boost/python/suite/indexing/detail/indexing_suite_detail.hpp +++ b/include/boost/python/suite/indexing/detail/indexing_suite_detail.hpp @@ -32,7 +32,9 @@ namespace boost { namespace python { namespace detail { template bool operator()(PyObject* prox, Index i) const { - return extract(prox)().get_index() < i; + typedef typename Proxy::policies_type policies_type; + return policies_type:: + compare_index(extract(prox)().get_index(), i); } }; @@ -476,7 +478,7 @@ namespace boost { namespace python { namespace detail { class Container , class DerivedPolicies , class ProxyHandler - , class Element + , class Data , class Index > struct slice_helper @@ -510,8 +512,8 @@ namespace boost { namespace python { namespace detail { Index from, to; base_get_slice_data(container, slice, from, to); - extract elem(v); - // try if elem is an exact Element + extract elem(v); + // try if elem is an exact Data if (elem.check()) { ProxyHandler::base_replace_indexes(container, from, to, 1); @@ -519,8 +521,8 @@ namespace boost { namespace python { namespace detail { } else { - // try to convert elem to Element - extract elem(v); + // try to convert elem to Data + extract elem(v); if (elem.check()) { ProxyHandler::base_replace_indexes(container, from, to, 1); @@ -532,20 +534,20 @@ namespace boost { namespace python { namespace detail { handle<> l_(python::borrowed(v)); object l(l_); - std::vector temp; + std::vector temp; for (int i = 0; i < l.attr("__len__")(); i++) { object elem(l[i]); - extract x(elem); - // try if elem is an exact Element type + extract x(elem); + // try if elem is an exact Data type if (x.check()) { temp.push_back(x()); } else { - // try to convert elem to Element type - extract x(elem); + // try to convert elem to Data type + extract x(elem); if (x.check()) { temp.push_back(x()); @@ -581,7 +583,7 @@ namespace boost { namespace python { namespace detail { class Container , class DerivedPolicies , class ProxyHandler - , class Element + , class Data , class Index > struct no_slice_helper diff --git a/include/boost/python/suite/indexing/indexing_suite.hpp b/include/boost/python/suite/indexing/indexing_suite.hpp index ce819ab0..0cdb10f9 100644 --- a/include/boost/python/suite/indexing/indexing_suite.hpp +++ b/include/boost/python/suite/indexing/indexing_suite.hpp @@ -10,7 +10,6 @@ # include # include # include -# include # include # include # include @@ -28,19 +27,19 @@ namespace boost { namespace python { // Derived classes provide the hooks needed by the indexing_suite // to do its job: // - // static element_type& + // static data_type& // get_item(Container& container, index_type i); // // static object // get_slice(Container& container, index_type from, index_type to); // // static void - // set_item(Container& container, index_type i, element_type const& v); + // set_item(Container& container, index_type i, data_type const& v); // // static void // set_slice( // Container& container, index_type from, - // index_type to, element_type const& v + // index_type to, data_type const& v // ); // // template @@ -100,9 +99,9 @@ namespace boost { namespace python { , class DerivedPolicies , bool NoProxy = false , bool NoSlice = false - , class Element = typename Container::value_type - , class Key = typename Container::value_type + , class Data = typename Container::value_type , class Index = typename Container::size_type + , class Key = typename Container::value_type > class indexing_suite : public def_visitor< @@ -111,16 +110,16 @@ namespace boost { namespace python { , DerivedPolicies , NoProxy , NoSlice - , Element - , Key + , Data , Index + , Key > > { private: typedef mpl::or_< mpl::bool_ - , mpl::not_ > > + , mpl::not_ > > no_proxy; typedef detail::container_element @@ -152,13 +151,13 @@ namespace boost { namespace python { Container , DerivedPolicies , proxy_handler - , Element + , Data , Index> , detail::slice_helper< Container , DerivedPolicies , proxy_handler - , Element + , Data , Index> >::type slice_handler; @@ -177,14 +176,20 @@ namespace boost { namespace python { .def("__getitem__", &base_get_item) .def("__contains__", &base_contains) .def("__iter__", def_iterator()) - - .def("append", &base_append) - .def("extend", &base_extend) - ; + ; + + DerivedPolicies::extension_def(cl); } + template + static void + extension_def(Class& cl) + { + // no more extensions + } + private: - + static object base_get_item(back_reference container, PyObject* i) { @@ -205,8 +210,8 @@ namespace boost { namespace python { } else { - extract elem(v); - // try if elem is an exact Element + extract elem(v); + // try if elem is an exact Data if (elem.check()) { DerivedPolicies:: @@ -216,8 +221,8 @@ namespace boost { namespace python { } else { - // try to convert elem to Element - extract elem(v); + // try to convert elem to Data + extract elem(v); if (elem.check()) { DerivedPolicies:: @@ -274,42 +279,6 @@ namespace boost { namespace python { return false; } } - - static void - base_append(Container& container, PyObject* v) - { - extract elem(v); - // try if elem is an exact Element - if (elem.check()) - { - DerivedPolicies::append(container, elem()); - } - else - { - // try to convert elem to Element - extract elem(v); - if (elem.check()) - { - DerivedPolicies::append(container, elem()); - } - else - { - PyErr_SetString(PyExc_TypeError, - "Attempting to append an invalid type"); - throw_error_already_set(); - } - } - } - - static void - base_extend(Container& container, PyObject* v) - { - std::vector temp; - handle<> l_(borrowed(v)); - object l(l_); - container_utils::extend_container(temp, l); - DerivedPolicies::extend(container, temp.begin(), temp.end()); - } }; }} // namespace boost::python diff --git a/include/boost/python/suite/indexing/vector_indexing_suite.hpp b/include/boost/python/suite/indexing/vector_indexing_suite.hpp index a21d8be3..f23d55ac 100644 --- a/include/boost/python/suite/indexing/vector_indexing_suite.hpp +++ b/include/boost/python/suite/indexing/vector_indexing_suite.hpp @@ -8,6 +8,7 @@ # define VECTOR_INDEXING_SUITE_JDG20036_HPP # include +# include # include namespace boost { namespace python { @@ -50,13 +51,23 @@ namespace boost { namespace python { { public: - typedef typename Container::value_type element_type; + typedef typename Container::value_type data_type; typedef typename Container::value_type key_type; typedef typename Container::size_type index_type; typedef typename Container::size_type size_type; typedef typename Container::difference_type difference_type; - static element_type& + template + static void + extension_def(Class& cl) + { + cl + .def("append", &base_append) + .def("extend", &base_extend) + ; + } + + static data_type& get_item(Container& container, index_type i) { return container[i]; @@ -69,14 +80,14 @@ namespace boost { namespace python { } static void - set_item(Container& container, index_type i, element_type const& v) + set_item(Container& container, index_type i, data_type const& v) { container[i] = v; } static void set_slice(Container& container, index_type from, - index_type to, element_type const& v) + index_type to, data_type const& v) { container.erase(container.begin()+from, container.begin()+to); container.insert(container.begin()+from, v); @@ -128,6 +139,12 @@ namespace boost { namespace python { return container.size(); } + static bool + compare_index(index_type a, index_type b) + { + return a < b; + } + static index_type convert_index(Container& container, PyObject* i_) { @@ -158,7 +175,7 @@ namespace boost { namespace python { } static void - append(Container& container, element_type const& v) + append(Container& container, data_type const& v) { container.push_back(v); } @@ -169,6 +186,44 @@ namespace boost { namespace python { { container.insert(container.end(), first, last); } + + private: + + static void + base_append(Container& container, PyObject* v) + { + extract elem(v); + // try if elem is an exact Data + if (elem.check()) + { + DerivedPolicies::append(container, elem()); + } + else + { + // try to convert elem to data_type + extract elem(v); + if (elem.check()) + { + DerivedPolicies::append(container, elem()); + } + else + { + PyErr_SetString(PyExc_TypeError, + "Attempting to append an invalid type"); + throw_error_already_set(); + } + } + } + + static void + base_extend(Container& container, PyObject* v) + { + std::vector temp; + handle<> l_(borrowed(v)); + object l(l_); + container_utils::extend_container(temp, l); + DerivedPolicies::extend(container, temp.begin(), temp.end()); + } }; }} // namespace boost::python