mirror of
https://github.com/boostorg/python.git
synced 2026-01-22 17:32:55 +00:00
added custom converter test for map indexing suite
[SVN r34359]
This commit is contained in:
@@ -26,6 +26,62 @@ std::string x_value(X const& x)
|
||||
return "gotya " + x.s;
|
||||
}
|
||||
|
||||
struct A
|
||||
{
|
||||
int value;
|
||||
A() : value(0){};
|
||||
A(int v) : value(v) {};
|
||||
};
|
||||
|
||||
bool operator==(const A& v1, const A& v2)
|
||||
{
|
||||
return (v1.value == v2.value);
|
||||
}
|
||||
|
||||
struct B
|
||||
{
|
||||
A a;
|
||||
};
|
||||
|
||||
// Converter from A to python int
|
||||
struct AToPython
|
||||
{
|
||||
static PyObject* convert(const A& s)
|
||||
{
|
||||
return boost::python::incref(boost::python::object((int)s.value).ptr());
|
||||
}
|
||||
};
|
||||
|
||||
// Conversion from python int to A
|
||||
struct AFromPython
|
||||
{
|
||||
AFromPython()
|
||||
{
|
||||
boost::python::converter::registry::push_back(
|
||||
&convertible,
|
||||
&construct,
|
||||
boost::python::type_id< A >());
|
||||
}
|
||||
|
||||
static void* convertible(PyObject* obj_ptr)
|
||||
{
|
||||
if (!PyInt_Check(obj_ptr)) return 0;
|
||||
return obj_ptr;
|
||||
}
|
||||
|
||||
static void construct(
|
||||
PyObject* obj_ptr,
|
||||
boost::python::converter::rvalue_from_python_stage1_data* data)
|
||||
{
|
||||
void* storage = (
|
||||
(boost::python::converter::rvalue_from_python_storage< A >*)
|
||||
data)-> storage.bytes;
|
||||
|
||||
new (storage) A((int)PyInt_AsLong(obj_ptr));
|
||||
data->convertible = storage;
|
||||
}
|
||||
};
|
||||
|
||||
BOOST_PYTHON_MODULE(map_indexing_suite_ext)
|
||||
{
|
||||
class_<X>("X")
|
||||
@@ -58,6 +114,18 @@ BOOST_PYTHON_MODULE(map_indexing_suite_ext)
|
||||
class_<std::map<std::string, boost::shared_ptr<X> > >("TestMap")
|
||||
.def(map_indexing_suite<std::map<std::string, boost::shared_ptr<X> >, true>())
|
||||
;
|
||||
|
||||
to_python_converter< A , AToPython >();
|
||||
AFromPython();
|
||||
|
||||
class_< std::map<int, A> >("AMap")
|
||||
.def(map_indexing_suite<std::map<int, A>, true >())
|
||||
;
|
||||
|
||||
class_< B >("B")
|
||||
.add_property("a", make_getter(&B::a, return_value_policy<return_by_value>()),
|
||||
make_setter(&B::a, return_value_policy<return_by_value>()))
|
||||
;
|
||||
}
|
||||
|
||||
#include "module_tail.cpp"
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
>>> assert "map_indexing_suite_IntMap_entry" in dir()
|
||||
>>> assert "map_indexing_suite_TestMap_entry" in dir()
|
||||
>>> assert "map_indexing_suite_XMap_entry" in dir()
|
||||
>>> assert "map_indexing_suite_AMap_entry" in dir()
|
||||
>>> x = X('hi')
|
||||
>>> x
|
||||
hi
|
||||
@@ -201,6 +202,18 @@ kiwi
|
||||
... dom = el.data()
|
||||
joel kimpo
|
||||
|
||||
#####################################################################
|
||||
# Test custom converter...
|
||||
#####################################################################
|
||||
|
||||
>>> am = AMap()
|
||||
>>> am[3] = 4
|
||||
>>> am[3]
|
||||
4
|
||||
>>> for i in am:
|
||||
... i.data()
|
||||
4
|
||||
|
||||
#####################################################################
|
||||
# END....
|
||||
#####################################################################
|
||||
|
||||
Reference in New Issue
Block a user