Skip to content

Commit 187506c

Browse files
committed
added map value type (std::pair) wrapper to map_indexing_suite.hpp
[SVN r19616]
1 parent 145c6d1 commit 187506c

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

include/boost/python/suite/indexing/map_indexing_suite.hpp

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
# include <boost/python/suite/indexing/indexing_suite.hpp>
1111
# include <boost/python/iterator.hpp>
12+
# include <boost/python/call_method.hpp>
13+
# include <boost/python/tuple.hpp>
1214

1315
namespace boost { namespace python {
1416

@@ -55,25 +57,60 @@ namespace boost { namespace python {
5557
, typename Container::key_type
5658
, typename Container::key_type
5759
>
58-
{
60+
{
5961
public:
6062

6163
typedef typename Container::mapped_type data_type;
6264
typedef typename Container::key_type key_type;
6365
typedef typename Container::key_type index_type;
6466
typedef typename Container::size_type size_type;
6567
typedef typename Container::difference_type difference_type;
66-
68+
6769
template <class Class>
6870
static void
6971
extension_def(Class& cl)
7072
{
71-
// cl
72-
// .def("append", &base_append)
73-
// .def("extend", &base_extend)
74-
// ;
73+
// Wrap the map's element (value_type)
74+
std::string elem_name = "map_indexing_suite_";
75+
elem_name += cl.ptr()->ob_type->tp_name; // the class name
76+
elem_name += "_entry";
77+
78+
typedef typename mpl::if_<
79+
is_class<typename Container::mapped_type>
80+
, return_internal_reference<>
81+
, default_call_policies
82+
>::type get_data_return_policy;
83+
84+
class_<typename Container::value_type>(elem_name.c_str())
85+
.def("__repr__", &DerivedPolicies::print_elem)
86+
.def("data", &DerivedPolicies::get_data, get_data_return_policy())
87+
.def("key", &DerivedPolicies::get_key)
88+
;
7589
}
7690

91+
static object
92+
print_elem(typename Container::value_type const& e)
93+
{
94+
return "(%s, %s)" % make_tuple(e.first, e.second);
95+
}
96+
97+
static
98+
typename mpl::if_<
99+
is_class<typename Container::mapped_type>
100+
, typename Container::mapped_type&
101+
, typename Container::mapped_type
102+
>::type
103+
get_data(typename Container::value_type& e)
104+
{
105+
return e.second;
106+
}
107+
108+
static typename Container::key_type
109+
get_key(typename Container::value_type& e)
110+
{
111+
return e.first;
112+
}
113+
77114
static data_type&
78115
get_item(Container& container, index_type i_)
79116
{

0 commit comments

Comments
 (0)