1 // (C) Copyright Joel de Guzman 2003.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef MAP_INDEXING_SUITE_JDG20038_HPP
7 # define MAP_INDEXING_SUITE_JDG20038_HPP
9 # include <boost/python/suite/indexing/indexing_suite.hpp>
10 # include <boost/python/iterator.hpp>
11 # include <boost/python/call_method.hpp>
12 # include <boost/python/tuple.hpp>
14 namespace boost { namespace python {
16 // Forward declaration
17 template <class Container, bool NoProxy, class DerivedPolicies>
18 class map_indexing_suite;
22 template <class Container, bool NoProxy>
23 class final_map_derived_policies
24 : public map_indexing_suite<Container,
25 NoProxy, final_map_derived_policies<Container, NoProxy> > {};
28 // The map_indexing_suite class is a predefined indexing_suite derived
29 // class for wrapping std::map (and std::map like) classes. It provides
30 // all the policies required by the indexing_suite (see indexing_suite).
37 // class_<std::map<std::string, X> >("XMap")
38 // .def(map_indexing_suite<std::map<std::string, X> >())
41 // By default indexed elements are returned by proxy. This can be
42 // disabled by supplying *true* in the NoProxy template parameter.
48 = detail::final_map_derived_policies<Container, NoProxy> >
49 class map_indexing_suite
50 : public indexing_suite<
55 , typename Container::value_type::second_type
56 , typename Container::key_type
57 , typename Container::key_type
62 typedef typename Container::value_type value_type;
63 typedef typename Container::value_type::second_type data_type;
64 typedef typename Container::key_type key_type;
65 typedef typename Container::key_type index_type;
66 typedef typename Container::size_type size_type;
67 typedef typename Container::difference_type difference_type;
69 template <class Class>
71 extension_def(Class& cl)
73 // Wrap the map's element (value_type)
74 std::string elem_name = "map_indexing_suite_";
75 object class_name(cl.attr("__name__"));
76 extract<std::string> class_name_extractor(class_name);
77 elem_name += class_name_extractor();
78 elem_name += "_entry";
80 typedef typename mpl::if_<
81 mpl::and_<is_class<data_type>, mpl::bool_<!NoProxy> >
82 , return_internal_reference<>
83 , default_call_policies
84 >::type get_data_return_policy;
86 class_<value_type>(elem_name.c_str())
87 .def("__repr__", &DerivedPolicies::print_elem)
88 .def("data", &DerivedPolicies::get_data, get_data_return_policy())
89 .def("key", &DerivedPolicies::get_key)
94 print_elem(typename Container::value_type const& e)
96 return "(%s, %s)" % python::make_tuple(e.first, e.second);
101 mpl::and_<is_class<data_type>, mpl::bool_<!NoProxy> >
105 get_data(typename Container::value_type& e)
110 static typename Container::key_type
111 get_key(typename Container::value_type& e)
117 get_item(Container& container, index_type i_)
119 typename Container::iterator i = container.find(i_);
120 if (i == container.end())
122 PyErr_SetString(PyExc_KeyError, "Invalid key");
123 throw_error_already_set();
129 set_item(Container& container, index_type i, data_type const& v)
135 delete_item(Container& container, index_type i)
141 size(Container& container)
143 return container.size();
147 contains(Container& container, key_type const& key)
149 return container.find(key) != container.end();
153 compare_index(Container& container, index_type a, index_type b)
155 return container.key_comp()(a, b);
159 convert_index(Container& /*container*/, PyObject* i_)
161 extract<key_type const&> i(i_);
168 extract<key_type> i(i_);
173 PyErr_SetString(PyExc_TypeError, "Invalid index type");
174 throw_error_already_set();
179 }} // namespace boost::python
181 #endif // MAP_INDEXING_SUITE_JDG20038_HPP