2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-31 20:32:16 +00:00

Preparing for std::map suite

[SVN r19566]
This commit is contained in:
Joel de Guzman
2003-08-12 21:21:47 +00:00
parent 8ca32bb494
commit fa70ddc2c5
4 changed files with 105 additions and 79 deletions

View File

@@ -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<element_t const&> x(elem);
// try if elem is an exact element_t type
extract<data_type const&> 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<element_t> x(elem);
// try to convert elem to data_type type
extract<data_type> 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();
}
}

View File

@@ -32,7 +32,9 @@ namespace boost { namespace python { namespace detail {
template <class Index>
bool operator()(PyObject* prox, Index i) const
{
return extract<Proxy&>(prox)().get_index() < i;
typedef typename Proxy::policies_type policies_type;
return policies_type::
compare_index(extract<Proxy&>(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<Element&> elem(v);
// try if elem is an exact Element
extract<Data&> 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<Element> elem(v);
// try to convert elem to Data
extract<Data> 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<Element> temp;
std::vector<Data> temp;
for (int i = 0; i < l.attr("__len__")(); i++)
{
object elem(l[i]);
extract<Element const&> x(elem);
// try if elem is an exact Element type
extract<Data const&> 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<Element> x(elem);
// try to convert elem to Data type
extract<Data> 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

View File

@@ -10,7 +10,6 @@
# include <boost/python/class.hpp>
# include <boost/python/register_ptr_to_python.hpp>
# include <boost/python/suite/indexing/detail/indexing_suite_detail.hpp>
# include <boost/python/suite/indexing/container_utils.hpp>
# include <boost/python/return_internal_reference.hpp>
# include <boost/python/iterator.hpp>
# include <boost/mpl/or.hpp>
@@ -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 <class Iter>
@@ -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_<NoProxy>
, mpl::not_<is_class<Element> > >
, mpl::not_<is_class<Data> > >
no_proxy;
typedef detail::container_element<Container, Index, DerivedPolicies>
@@ -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 <class Class>
static void
extension_def(Class& cl)
{
// no more extensions
}
private:
static object
base_get_item(back_reference<Container&> container, PyObject* i)
{
@@ -205,8 +210,8 @@ namespace boost { namespace python {
}
else
{
extract<Element&> elem(v);
// try if elem is an exact Element
extract<Data&> 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<Element> elem(v);
// try to convert elem to Data
extract<Data> 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<Element&> elem(v);
// try if elem is an exact Element
if (elem.check())
{
DerivedPolicies::append(container, elem());
}
else
{
// try to convert elem to Element
extract<Element> 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<Element> temp;
handle<> l_(borrowed(v));
object l(l_);
container_utils::extend_container(temp, l);
DerivedPolicies::extend(container, temp.begin(), temp.end());
}
};
}} // namespace boost::python

View File

@@ -8,6 +8,7 @@
# define VECTOR_INDEXING_SUITE_JDG20036_HPP
# include <boost/python/suite/indexing/indexing_suite.hpp>
# include <boost/python/suite/indexing/container_utils.hpp>
# include <boost/python/iterator.hpp>
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 <class Class>
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<data_type&> 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<data_type> 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<data_type> temp;
handle<> l_(borrowed(v));
object l(l_);
container_utils::extend_container(temp, l);
DerivedPolicies::extend(container, temp.begin(), temp.end());
}
};
}} // namespace boost::python