diff --git a/include/boost/python/suite/indexing/container_utils.hpp b/include/boost/python/suite/indexing/container_utils.hpp deleted file mode 100644 index 2370fa8d..00000000 --- a/include/boost/python/suite/indexing/container_utils.hpp +++ /dev/null @@ -1,53 +0,0 @@ - -// (C) Copyright Joel de Guzman 2003. -// Permission to copy, use, modify, sell and distribute this software -// is granted provided this copyright notice appears in all copies. This -// software is provided "as is" without express or implied warranty, and -// with no claim as to its suitability for any purpose. - -#ifndef PY_CONTAINER_UTILS_JDG20038_HPP -# define PY_CONTAINER_UTILS_JDG20038_HPP - -# include -# include -# include - -namespace boost { namespace python { namespace container_utils { - - template - void - extend_container(Container& container, object l) - { - 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 data_type type - if (x.check()) - { - container.push_back(x()); - } - else - { - // try to convert elem to data_type type - extract x(elem); - if (x.check()) - { - container.push_back(x()); - } - else - { - PyErr_SetString(PyExc_TypeError, "Incompatible Data Type"); - throw_error_already_set(); - } - } - } - } - -}}} // namespace boost::python::container_utils - -#endif diff --git a/include/boost/python/suite/indexing/detail/indexing_suite_detail.hpp b/include/boost/python/suite/indexing/detail/indexing_suite_detail.hpp deleted file mode 100644 index 8217d83d..00000000 --- a/include/boost/python/suite/indexing/detail/indexing_suite_detail.hpp +++ /dev/null @@ -1,743 +0,0 @@ -// (C) Copyright Joel de Guzman 2003. -// Permission to copy, use, modify, sell and distribute this software -// is granted provided this copyright notice appears in all copies. This -// software is provided "as is" without express or implied warranty, and -// with no claim as to its suitability for any purpose. - -#ifndef INDEXING_SUITE_DETAIL_JDG20036_HPP -# define INDEXING_SUITE_DETAIL_JDG20036_HPP - -# include -# include -# include -# include -# include -# include -#include - -namespace boost { namespace python { namespace detail { - -#if defined(NDEBUG) -#define BOOST_PYTHON_INDEXING_CHECK_INVARIANT -#else -#define BOOST_PYTHON_INDEXING_CHECK_INVARIANT check_invariant() -#endif - - template - struct compare_proxy_index - { - // This functor compares a proxy and an index. - // This is used by proxy_group::first_proxy to - // get first proxy with index i. - - template - bool operator()(PyObject* prox, Index i) const - { - typedef typename Proxy::policies_type policies_type; - Proxy& proxy = extract(prox)(); - return policies_type:: - compare_index(proxy.get_container(), proxy.get_index(), i); - } - }; - - // The proxy_group class holds a vector of container element - // proxies. First, what is a container element proxy? A container - // element proxy acts like a smart pointer holding a reference to - // a container and an index (see container_element, for details). - // - // The proxies are held in a vector always sorted by its index. - // Various functions manage the addition, removal and searching - // of proxies from the vector. - // - template - class proxy_group - { - public: - - typedef typename std::vector::const_iterator const_iterator; - typedef typename std::vector::iterator iterator; - typedef typename Proxy::index_type index_type; - typedef typename Proxy::policies_type policies_type; - - iterator - first_proxy(index_type i) - { - // Return the first proxy with index <= i - return boost::detail::lower_bound( - proxies.begin(), proxies.end(), - i, compare_proxy_index()); - } - - void - remove(Proxy& proxy) - { - // Remove a proxy - for (iterator iter = first_proxy(proxy.get_index()); - iter != proxies.end(); ++iter) - { - if (&extract(*iter)() == &proxy) - { - proxies.erase(iter); - break; - } - } - BOOST_PYTHON_INDEXING_CHECK_INVARIANT; - } - - void - add(PyObject* prox) - { - BOOST_PYTHON_INDEXING_CHECK_INVARIANT; - // Add a proxy - proxies.insert( - first_proxy(extract(prox)().get_index()), prox); - BOOST_PYTHON_INDEXING_CHECK_INVARIANT; - } - - void - erase(index_type i, mpl::false_) - { - BOOST_PYTHON_INDEXING_CHECK_INVARIANT; - // Erase the proxy with index i - replace(i, i+1, 0); - BOOST_PYTHON_INDEXING_CHECK_INVARIANT; - } - - void - erase(index_type i, mpl::true_) - { - BOOST_PYTHON_INDEXING_CHECK_INVARIANT; - // Erase the proxy with index i - - iterator iter = first_proxy(i); - extract p(*iter); - - if (iter != proxies.end() && p().get_index() == i) - { - extract p(*iter); - p().detach(); - proxies.erase(iter); - } - BOOST_PYTHON_INDEXING_CHECK_INVARIANT; - } - - void - erase(index_type from, index_type to) - { - // note: this cannot be called when container is not sliceable - - BOOST_PYTHON_INDEXING_CHECK_INVARIANT; - // Erase all proxies with indexes from..to - replace(from, to, 0); - BOOST_PYTHON_INDEXING_CHECK_INVARIANT; - } - - void - replace( - index_type from, - index_type to, - typename std::vector::size_type len) - { - // note: this cannot be called when container is not sliceable - - BOOST_PYTHON_INDEXING_CHECK_INVARIANT; - // Erase all proxies with indexes from..to. - // Adjust the displaced indexes such that the - // final effect is that we have inserted *len* - // number of proxies in the vacated region. This - // procedure involves adjusting the indexes of - // the proxies. - - iterator left = first_proxy(from); - iterator right = proxies.end(); // we'll adjust this later - - for (iterator iter = left; iter != right; ++iter) - { - if (extract(*iter)().get_index() > to) - { - right = iter; // adjust right - break; - } - extract p(*iter); - p().detach(); - } - - typename std::vector::size_type - offset = left-proxies.begin(); - proxies.erase(left, right); - right = proxies.begin()+offset; - - while (right != proxies.end()) - { - typedef typename Proxy::container_type::difference_type difference_type; - extract p(*right); - p().set_index( - extract(*right)().get_index() - - (difference_type(to) - from - len) - ); - - ++right; - } - BOOST_PYTHON_INDEXING_CHECK_INVARIANT; - } - - PyObject* - find(index_type i) - { - BOOST_PYTHON_INDEXING_CHECK_INVARIANT; - // Find the proxy with *exact* index i. - // Return 0 (null) if no proxy with the - // given index is found. - iterator iter = first_proxy(i); - if (iter != proxies.end() - && extract(*iter)().get_index() == i) - { - BOOST_PYTHON_INDEXING_CHECK_INVARIANT; - return *iter; - } - BOOST_PYTHON_INDEXING_CHECK_INVARIANT; - return 0; - } - - typename std::vector::size_type - size() const - { - BOOST_PYTHON_INDEXING_CHECK_INVARIANT; - // How many proxies are there so far? - return proxies.size(); - } - - private: - -#if !defined(NDEBUG) - void - check_invariant() const - { - for (const_iterator i = proxies.begin(); i != proxies.end(); ++i) - { - if ((*i)->ob_refcnt <= 0) - { - PyErr_SetString(PyExc_RuntimeError, - "Invariant: Proxy vector in an inconsistent state"); - throw_error_already_set(); - } - - if (i+1 != proxies.end()) - { - if (extract(*(i+1))().get_index() == - extract(*(i))().get_index()) - { - PyErr_SetString(PyExc_RuntimeError, - "Invariant: Proxy vector in an inconsistent state (duplicate proxy)"); - throw_error_already_set(); - } - } - } - } -#endif - - std::vector proxies; - }; - - // proxy_links holds a map of Container pointers (keys) - // with proxy_group(s) (data). Various functions manage - // the addition, removal and searching of proxies from - // the map. - // - template - class proxy_links - { - public: - - typedef std::map > links_t; - typedef typename Proxy::index_type index_type; - - void - remove(Proxy& proxy) - { - // Remove a proxy. - typename links_t::iterator r = links.find(&proxy.get_container()); - if (r != links.end()) - { - r->second.remove(proxy); - if (r->second.size() == 0) - links.erase(r); - } - } - - void - add(PyObject* prox, Container& container) - { - // Add a proxy - links[&container].add(prox); - } - - template - void erase(Container& container, index_type i, NoSlice no_slice) - { - // Erase the proxy with index i - typename links_t::iterator r = links.find(&container); - if (r != links.end()) - { - r->second.erase(i, no_slice); - if (r->second.size() == 0) - links.erase(r); - } - } - - void - erase(Container& container, index_type from, index_type to) - { - // Erase all proxies with indexes from..to - typename links_t::iterator r = links.find(&container); - if (r != links.end()) - { - r->second.erase(from, to); - if (r->second.size() == 0) - links.erase(r); - } - } - - void - replace( - Container& container, - index_type from, index_type to, index_type len) - { - // Erase all proxies with indexes from..to. - // Adjust the displaced indexes such that the - // final effect is that we have inserted *len* - // number of proxies in the vacated region. This - // procedure involves adjusting the indexes of - // the proxies. - - typename links_t::iterator r = links.find(&container); - if (r != links.end()) - { - r->second.replace(from, to, len); - if (r->second.size() == 0) - links.erase(r); - } - } - - PyObject* - find(Container& container, index_type i) - { - // Find the proxy with *exact* index i. - // Return 0 (null) if no proxy with the given - // index is found. - typename links_t::iterator r = links.find(&container); - if (r != links.end()) - return r->second.find(i); - return 0; - } - - private: - - links_t links; - }; - - // container_element is our container proxy class. - // This class acts like a smart pointer to a container - // element. The class holds an index and a reference to - // a container. Dereferencing the smart pointer will - // retrieve the nth (index) element from the container. - // - // A container_element can also be detached from the - // container. In such a detached state, the container_element - // holds a copy of the nth (index) element, which it - // returns when dereferenced. - // - template - class container_element - { - public: - - typedef Index index_type; - typedef Container container_type; - typedef typename Policies::data_type element_type; - typedef Policies policies_type; - typedef container_element self_t; - typedef proxy_group links_type; - - container_element(object container, Index index) - : ptr() - , container(container) - , index(index) - { - } - - container_element(container_element const& ce) - : ptr(ce.ptr.get() == 0 ? 0 : new element_type(*ce.ptr.get())) - , container(ce.container) - , index(ce.index) - { - } - - ~container_element() - { - if (!is_detached()) - get_links().remove(*this); - } - - element_type& operator*() const - { - if (is_detached()) - return *get_pointer(ptr); - return Policies::get_item(get_container(), index); - } - - element_type* get() const - { - if (is_detached()) - return get_pointer(ptr); - return &Policies::get_item(get_container(), index); - } - - void - detach() - { - if (!is_detached()) - { - ptr.reset( - new element_type( - Policies::get_item(get_container(), index))); - container = object(); // free container. reset it to None - } - } - - bool - is_detached() const - { - return get_pointer(ptr) != 0; - } - - Container& - get_container() const - { - return extract(container)(); - } - - Index - get_index() const - { - return index; - } - - void - set_index(Index i) - { - index = i; - } - - static proxy_links& - get_links() - { - // All container_element(s) maintain links to - // its container in a global map (see proxy_links). - // This global "links" map is a singleton. - - static proxy_links links; - return links; // singleton - } - - private: - - container_element& operator=(container_element const& ce); - - scoped_ptr ptr; - object container; - Index index; - }; - - template < - class Container - , class DerivedPolicies - , class ContainerElement - , class Index - > - struct no_proxy_helper - { - static void - register_container_element() - { - } - - static object - base_get_item_(back_reference const& container, PyObject* i) - { - return object( - DerivedPolicies::get_item( - container.get(), DerivedPolicies:: - convert_index(container.get(), i))); - } - - static void - base_replace_indexes( - Container& container, Index from, - Index to, Index n) - { - } - - template - static void - base_erase_index( - Container& container, Index i, NoSlice no_slice) - { - } - - static void - base_erase_indexes(Container& container, Index from, Index to) - { - } - }; - - template < - class Container - , class DerivedPolicies - , class ContainerElement - , class Index - > - struct proxy_helper - { - static void - register_container_element() - { - register_ptr_to_python(); - } - - static object - base_get_item_(back_reference const& container, PyObject* i) - { - // Proxy - Index idx = DerivedPolicies::convert_index(container.get(), i); - - if (PyObject* shared = - ContainerElement::get_links().find(container.get(), idx)) - { - handle<> h(python::borrowed(shared)); - return object(h); - } - else - { - object prox(ContainerElement(container.source(), idx)); - ContainerElement:: - get_links().add(prox.ptr(), container.get()); - return prox; - } - } - - static void - base_replace_indexes( - Container& container, Index from, - Index to, Index n) - { - ContainerElement::get_links().replace(container, from, to, n); - } - - template - static void - base_erase_index( - Container& container, Index i, NoSlice no_slice) - { - ContainerElement::get_links().erase(container, i, no_slice); - } - - static void - base_erase_indexes( - Container& container, Index from, Index to) - { - ContainerElement::get_links().erase(container, from, to); - } - }; - - template < - class Container - , class DerivedPolicies - , class ProxyHandler - , class Data - , class Index - > - struct slice_helper - { - static object - base_get_slice(Container& container, PySliceObject* slice) - { - Index from, to; - base_get_slice_data(container, slice, from, to); - return DerivedPolicies::get_slice(container, from, to); - } - - static void - base_get_slice_data( - Container& container, PySliceObject* slice, Index& from, Index& to) - { - if (Py_None != slice->step) { - PyErr_SetString( PyExc_IndexError, "slice step size not supported."); - throw_error_already_set(); - } - - Index min_index = DerivedPolicies::get_min_index(container); - Index max_index = DerivedPolicies::get_max_index(container); - - - if (Py_None == slice->start) { - from = min_index; - } - else { - from = extract( slice->start); - if (from < 0) // Negative slice index - from += max_index; - if (from < 0) // Clip lower bounds to zero - from = 0; - if (from > max_index) // Clip upper bounds to max_index. - from = max_index; - - } - - if (Py_None == slice->stop) { - to = DerivedPolicies::get_max_index(container); - } - else { - to = extract( slice->stop); - if (to < 0) - to += max_index; - if (to < 0) - to = 0; - if (to > max_index) - to = max_index; - } - - } - - static void - base_set_slice(Container& container, PySliceObject* slice, PyObject* v) - { - Index from, to; - base_get_slice_data(container, slice, from, to); - - extract elem(v); - // try if elem is an exact Data - if (elem.check()) - { - ProxyHandler::base_replace_indexes(container, from, to, 1); - DerivedPolicies::set_slice(container, from, to, elem()); - } - else - { - // try to convert elem to Data - extract elem(v); - if (elem.check()) - { - ProxyHandler::base_replace_indexes(container, from, to, 1); - DerivedPolicies::set_slice(container, from, to, elem()); - } - else - { - // Otherwise, it must be a list or some container - handle<> l_(python::borrowed(v)); - object l(l_); - - 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 Data type - if (x.check()) - { - temp.push_back(x()); - } - else - { - // try to convert elem to Data type - extract x(elem); - if (x.check()) - { - temp.push_back(x()); - } - else - { - PyErr_SetString(PyExc_TypeError, - "Invalid sequence element"); - throw_error_already_set(); - } - } - } - - ProxyHandler::base_replace_indexes(container, from, to, - temp.end()-temp.begin()); - DerivedPolicies::set_slice(container, from, to, - temp.begin(), temp.end()); - } - } - } - - static void - base_delete_slice(Container& container, PySliceObject* slice) - { - Index from, to; - base_get_slice_data(container, slice, from, to); - ProxyHandler::base_erase_indexes(container, from, to); - DerivedPolicies::delete_slice(container, from, to); - } - }; - - template < - class Container - , class DerivedPolicies - , class ProxyHandler - , class Data - , class Index - > - struct no_slice_helper - { - static void - slicing_not_suported() - { - PyErr_SetString(PyExc_RuntimeError, "Slicing not supported"); - throw_error_already_set(); - } - - static object - base_get_slice(Container& container, PySliceObject* slice) - { - slicing_not_suported(); - return object(); - } - - static void - base_set_slice(Container& container, PySliceObject* slice, PyObject* v) - { - slicing_not_suported(); - } - - static void - base_delete_slice(Container& container, PySliceObject* slice) - { - slicing_not_suported(); - } - }; - -#ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP -}} // namespace python::detail -#endif - - template - inline typename Policies::data_type* - get_pointer( - python::detail::container_element const& p) - { - // Get the pointer of a container_element smart pointer - return p.get(); - } - -#ifndef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP - // Don't hide these other get_pointer overloads - using boost::python::get_pointer; - using boost::get_pointer; -}} // namespace python::detail -#endif - -} // namespace boost - -#endif // INDEXING_SUITE_DETAIL_JDG20036_HPP diff --git a/include/boost/python/suite/indexing/indexing_suite.hpp b/include/boost/python/suite/indexing/indexing_suite.hpp deleted file mode 100644 index 1e392b40..00000000 --- a/include/boost/python/suite/indexing/indexing_suite.hpp +++ /dev/null @@ -1,294 +0,0 @@ -// (C) Copyright Joel de Guzman 2003. -// Permission to copy, use, modify, sell and distribute this software -// is granted provided this copyright notice appears in all copies. This -// software is provided "as is" without express or implied warranty, and -// with no claim as to its suitability for any purpose. - -#ifndef INDEXING_SUITE_JDG20036_HPP -# define INDEXING_SUITE_JDG20036_HPP - -# include -# include -# include -# include -# include -# include -# include -# include - -namespace boost { namespace python { - - // indexing_suite class. This class is the protocol class for - // the management of C++ containers intended to be integrated - // to Python. The objective is make a C++ container look and - // feel and behave exactly as we'd expect a Python container. - // By default indexed elements are returned by proxy. This can be - // disabled by supplying *true* in the NoProxy template parameter. - // - // Derived classes provide the hooks needed by the indexing_suite - // to do its job: - // - // 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, data_type const& v); - // - // static void - // set_slice( - // Container& container, index_type from, - // index_type to, data_type const& v - // ); - // - // template - // static void - // set_slice(Container& container, index_type from, - // index_type to, Iter first, Iter last - // ); - // - // static void - // delete_item(Container& container, index_type i); - // - // static void - // delete_slice(Container& container, index_type from, index_type to); - // - // static size_t - // size(Container& container); - // - // template - // static bool - // contains(Container& container, T const& val); - // - // static index_type - // convert_index(Container& container, PyObject* i); - // - // static index_type - // adjust_index(index_type current, index_type from, - // index_type to, size_type len - // ); - // - // Most of these policies are self explanatory. convert_index and - // adjust_index, however, deserves some explanation. - // - // convert_index converts an Python index into a C++ index that the - // container can handle. For instance, negative indexes in Python, by - // convention, indexes from the right (e.g. C[-1] indexes the rightmost - // element in C). convert_index should handle the necessary conversion - // for the C++ container (e.g. convert -1 to C.size()-1). convert_index - // should also be able to convert the type of the index (A dynamic Python - // type) to the actual type that the C++ container expects. - // - // When a container expands or contracts, held indexes to its elements - // must be adjusted to follow the movement of data. For instance, if - // we erase 3 elements, starting from index 0 from a 5 element vector, - // what used to be at index 4 will now be at index 1: - // - // [a][b][c][d][e] ---> [d][e] - // ^ ^ - // 4 1 - // - // adjust_index takes care of the adjustment. Given a current index, - // the function should return the adjusted index when data in the - // container at index from..to is replaced by *len* elements. - // - - template < - class Container - , class DerivedPolicies - , bool NoProxy = false - , bool NoSlice = false - , class Data = typename Container::value_type - , class Index = typename Container::size_type - , class Key = typename Container::value_type - > - class indexing_suite - : public def_visitor< - indexing_suite< - Container - , DerivedPolicies - , NoProxy - , NoSlice - , Data - , Index - , Key - > > - { - private: - - typedef mpl::or_< - mpl::bool_ - , mpl::not_ > > - no_proxy; - - typedef detail::container_element - container_element_t; - -#if BOOST_WORKAROUND(BOOST_MSVC, == 1200) - struct return_policy : return_internal_reference<> {}; -#else - typedef return_internal_reference<> return_policy; -#endif - - typedef typename mpl::if_< - no_proxy - , iterator - , iterator >::type - def_iterator; - - typedef typename mpl::if_< - no_proxy - , detail::no_proxy_helper< - Container - , DerivedPolicies - , container_element_t - , Index> - , detail::proxy_helper< - Container - , DerivedPolicies - , container_element_t - , Index> >::type - proxy_handler; - - typedef typename mpl::if_< - mpl::bool_ - , detail::no_slice_helper< - Container - , DerivedPolicies - , proxy_handler - , Data - , Index> - , detail::slice_helper< - Container - , DerivedPolicies - , proxy_handler - , Data - , Index> >::type - slice_handler; - - public: - - template - void visit(Class& cl) const - { - // Hook into the class_ generic visitation .def function - proxy_handler::register_container_element(); - - cl - .def("__len__", base_size) - .def("__setitem__", &base_set_item) - .def("__delitem__", &base_delete_item) - .def("__getitem__", &base_get_item) - .def("__contains__", &base_contains) - .def("__iter__", def_iterator()) - ; - - DerivedPolicies::extension_def(cl); - } - - template - static void - extension_def(Class& cl) - { - // default. - // no more extensions - } - - private: - - static object - base_get_item(back_reference container, PyObject* i) - { - if (PySlice_Check(i)) - return slice_handler::base_get_slice( - container.get(), reinterpret_cast(i)); - - return proxy_handler::base_get_item_(container, i); - } - - static void - base_set_item(Container& container, PyObject* i, PyObject* v) - { - if (PySlice_Check(i)) - { - slice_handler::base_set_slice(container, - reinterpret_cast(i), v); - } - else - { - extract elem(v); - // try if elem is an exact Data - if (elem.check()) - { - DerivedPolicies:: - set_item(container, - DerivedPolicies:: - convert_index(container, i), elem()); - } - else - { - // try to convert elem to Data - extract elem(v); - if (elem.check()) - { - DerivedPolicies:: - set_item(container, - DerivedPolicies:: - convert_index(container, i), elem()); - } - else - { - PyErr_SetString(PyExc_TypeError, "Invalid assignment"); - throw_error_already_set(); - } - } - } - } - - static void - base_delete_item(Container& container, PyObject* i) - { - if (PySlice_Check(i)) - { - slice_handler::base_delete_slice( - container, reinterpret_cast(i)); - return; - } - - Index index = DerivedPolicies::convert_index(container, i); - proxy_handler::base_erase_index(container, index, mpl::bool_()); - DerivedPolicies::delete_item(container, index); - } - - static size_t - base_size(Container& container) - { - return DerivedPolicies::size(container); - } - - static bool - base_contains(Container& container, PyObject* key) - { - extract x(key); - // try if key is an exact Key type - if (x.check()) - { - return DerivedPolicies::contains(container, x()); - } - else - { - // try to convert key to Key type - extract x(key); - if (x.check()) - return DerivedPolicies::contains(container, x()); - else - return false; - } - } - }; - -}} // namespace boost::python - -#endif // INDEXING_SUITE_JDG20036_HPP diff --git a/include/boost/python/suite/indexing/map_indexing_suite.hpp b/include/boost/python/suite/indexing/map_indexing_suite.hpp deleted file mode 100644 index acdb664f..00000000 --- a/include/boost/python/suite/indexing/map_indexing_suite.hpp +++ /dev/null @@ -1,180 +0,0 @@ -// (C) Copyright Joel de Guzman 2003. -// Permission to copy, use, modify, sell and distribute this software -// is granted provided this copyright notice appears in all copies. This -// software is provided "as is" without express or implied warranty, and -// with no claim as to its suitability for any purpose. - -#ifndef MAP_INDEXING_SUITE_JDG20038_HPP -# define MAP_INDEXING_SUITE_JDG20038_HPP - -# include -# include -# include -# include - -namespace boost { namespace python { - - // Forward declaration - template - class map_indexing_suite; - - namespace detail - { - template - class final_map_derived_policies - : public map_indexing_suite > {}; - } - - // The map_indexing_suite class is a predefined indexing_suite derived - // class for wrapping std::vector (and std::vector like) classes. It provides - // all the policies required by the indexing_suite (see indexing_suite). - // Example usage: - // - // class X {...}; - // - // ... - // - // class_ >("XMap") - // .def(map_indexing_suite >()) - // ; - // - // By default indexed elements are returned by proxy. This can be - // disabled by supplying *true* in the NoProxy template parameter. - // - template < - class Container, - bool NoProxy = false, - class DerivedPolicies - = detail::final_map_derived_policies > - class map_indexing_suite - : public indexing_suite< - Container - , DerivedPolicies - , NoProxy - , true - , typename Container::value_type::second_type - , typename Container::key_type - , typename Container::key_type - > - { - public: - - typedef typename Container::value_type value_type; - typedef typename Container::value_type::second_type data_type; - typedef typename Container::key_type key_type; - typedef typename Container::key_type index_type; - typedef typename Container::size_type size_type; - typedef typename Container::difference_type difference_type; - - template - static void - extension_def(Class& cl) - { - // Wrap the map's element (value_type) - std::string elem_name = "map_indexing_suite_"; - elem_name += cl.ptr()->ob_type->tp_name; // the class name - elem_name += "_entry"; - - typedef typename mpl::if_< - is_class - , return_internal_reference<> - , default_call_policies - >::type get_data_return_policy; - - class_(elem_name.c_str()) - .def("__repr__", &DerivedPolicies::print_elem) - .def("data", &DerivedPolicies::get_data, get_data_return_policy()) - .def("key", &DerivedPolicies::get_key) - ; - } - - static object - print_elem(typename Container::value_type const& e) - { - return "(%s, %s)" % make_tuple(e.first, e.second); - } - - static - typename mpl::if_< - is_class - , data_type& - , data_type - >::type - get_data(typename Container::value_type& e) - { - return e.second; - } - - static typename Container::key_type - get_key(typename Container::value_type& e) - { - return e.first; - } - - static data_type& - get_item(Container& container, index_type i_) - { - typename Container::iterator i = container.find(i_); - if (i == container.end()) - { - PyErr_SetString(PyExc_KeyError, "Invalid key"); - throw_error_already_set(); - } - return i->second; - } - - static void - set_item(Container& container, index_type i, data_type const& v) - { - container[i] = v; - } - - static void - delete_item(Container& container, index_type i) - { - container.erase(i); - } - - static size_t - size(Container& container) - { - return container.size(); - } - - static bool - contains(Container& container, key_type const& key) - { - return container.find(key) != container.end(); - } - - static bool - compare_index(Container& container, index_type a, index_type b) - { - return container.key_comp()(a, b); - } - - static index_type - convert_index(Container& container, PyObject* i_) - { - extract i(i_); - if (i.check()) - { - return i(); - } - else - { - extract i(i_); - if (i.check()) - return i(); - } - - PyErr_SetString(PyExc_TypeError, "Invalid index type"); - throw_error_already_set(); - return index_type(); - } - }; - -}} // namespace boost::python - -#endif // MAP_INDEXING_SUITE_JDG20038_HPP diff --git a/include/boost/python/suite/indexing/vector_indexing_suite.hpp b/include/boost/python/suite/indexing/vector_indexing_suite.hpp deleted file mode 100644 index 19abe2ff..00000000 --- a/include/boost/python/suite/indexing/vector_indexing_suite.hpp +++ /dev/null @@ -1,243 +0,0 @@ -// (C) Copyright Joel de Guzman 2003. -// Permission to copy, use, modify, sell and distribute this software -// is granted provided this copyright notice appears in all copies. This -// software is provided "as is" without express or implied warranty, and -// with no claim as to its suitability for any purpose. - -#ifndef VECTOR_INDEXING_SUITE_JDG20036_HPP -# define VECTOR_INDEXING_SUITE_JDG20036_HPP - -# include -# include -# include - -namespace boost { namespace python { - - // Forward declaration - template - class vector_indexing_suite; - - namespace detail - { - template - class final_vector_derived_policies - : public vector_indexing_suite > {}; - } - - // The vector_indexing_suite class is a predefined indexing_suite derived - // class for wrapping std::vector (and std::vector like) classes. It provides - // all the policies required by the indexing_suite (see indexing_suite). - // Example usage: - // - // class X {...}; - // - // ... - // - // class_ >("XVec") - // .def(vector_indexing_suite >()) - // ; - // - // By default indexed elements are returned by proxy. This can be - // disabled by supplying *true* in the NoProxy template parameter. - // - template < - class Container, - bool NoProxy = false, - class DerivedPolicies - = detail::final_vector_derived_policies > - class vector_indexing_suite - : public indexing_suite - { - public: - - 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; - - template - static void - extension_def(Class& cl) - { - cl - .def("append", &base_append) - .def("extend", &base_extend) - ; - } - - static - typename mpl::if_< - is_class - , data_type& - , data_type - >::type - get_item(Container& container, index_type i) - { - return container[i]; - } - - static object - get_slice(Container& container, index_type from, index_type to) - { - if (from > to) - return object(Container()); - return object(Container(container.begin()+from, container.begin()+to)); - } - - static void - 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, data_type const& v) - { - if (from > to) { - return; - } - else { - container.erase(container.begin()+from, container.begin()+to); - container.insert(container.begin()+from, v); - } - } - - template - static void - set_slice(Container& container, index_type from, - index_type to, Iter first, Iter last) - { - if (from > to) { - container.insert(container.begin()+from, first, last); - } - else { - container.erase(container.begin()+from, container.begin()+to); - container.insert(container.begin()+from, first, last); - } - } - - static void - delete_item(Container& container, index_type i) - { - container.erase(container.begin()+i); - } - - static void - delete_slice(Container& container, index_type from, index_type to) - { - if (from > to) { - // A null-op. - return; - } - container.erase(container.begin()+from, container.begin()+to); - } - - static size_t - size(Container& container) - { - return container.size(); - } - - static bool - contains(Container& container, key_type const& key) - { - return std::find(container.begin(), container.end(), key) - != container.end(); - } - - static index_type - get_min_index(Container& container) - { - return 0; - } - - static index_type - get_max_index(Container& container) - { - return container.size(); - } - - static bool - compare_index(Container& container, index_type a, index_type b) - { - return a < b; - } - - static index_type - convert_index(Container& container, PyObject* i_) - { - extract i(i_); - if (i.check()) - { - long index = i(); - if (index < 0) - index += DerivedPolicies::size(container); - if (index >= long(container.size()) || index < 0) - { - PyErr_SetString(PyExc_IndexError, "Index out of range"); - throw_error_already_set(); - } - return index; - } - - PyErr_SetString(PyExc_TypeError, "Invalid index type"); - throw_error_already_set(); - return index_type(); - } - - static void - append(Container& container, data_type const& v) - { - container.push_back(v); - } - - template - static void - extend(Container& container, Iter first, Iter last) - { - container.insert(container.end(), first, last); - } - - private: - - static void - base_append(Container& container, object 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, object v) - { - std::vector temp; - container_utils::extend_container(temp, v); - DerivedPolicies::extend(container, temp.begin(), temp.end()); - } - }; - -}} // namespace boost::python - -#endif // VECTOR_INDEXING_SUITE_JDG20036_HPP