map_indexing_suite.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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)
  5. #ifndef MAP_INDEXING_SUITE_JDG20038_HPP
  6. # define MAP_INDEXING_SUITE_JDG20038_HPP
  7. # include <boost/python/suite/indexing/indexing_suite.hpp>
  8. # include <boost/python/iterator.hpp>
  9. # include <boost/python/call_method.hpp>
  10. # include <boost/python/tuple.hpp>
  11. namespace boost { namespace python {
  12. // Forward declaration
  13. template <class Container, bool NoProxy, class DerivedPolicies>
  14. class map_indexing_suite;
  15. namespace detail
  16. {
  17. template <class Container, bool NoProxy>
  18. class final_map_derived_policies
  19. : public map_indexing_suite<Container,
  20. NoProxy, final_map_derived_policies<Container, NoProxy> > {};
  21. }
  22. // The map_indexing_suite class is a predefined indexing_suite derived
  23. // class for wrapping std::map (and std::map like) classes. It provides
  24. // all the policies required by the indexing_suite (see indexing_suite).
  25. // Example usage:
  26. //
  27. // class X {...};
  28. //
  29. // ...
  30. //
  31. // class_<std::map<std::string, X> >("XMap")
  32. // .def(map_indexing_suite<std::map<std::string, X> >())
  33. // ;
  34. //
  35. // By default indexed elements are returned by proxy. This can be
  36. // disabled by supplying *true* in the NoProxy template parameter.
  37. //
  38. template <
  39. class Container,
  40. bool NoProxy = false,
  41. class DerivedPolicies
  42. = detail::final_map_derived_policies<Container, NoProxy> >
  43. class map_indexing_suite
  44. : public indexing_suite<
  45. Container
  46. , DerivedPolicies
  47. , NoProxy
  48. , true
  49. , typename Container::value_type::second_type
  50. , typename Container::key_type
  51. , typename Container::key_type
  52. >
  53. {
  54. public:
  55. typedef typename Container::value_type value_type;
  56. typedef typename Container::value_type::second_type data_type;
  57. typedef typename Container::key_type key_type;
  58. typedef typename Container::key_type index_type;
  59. typedef typename Container::size_type size_type;
  60. typedef typename Container::difference_type difference_type;
  61. template <class Class>
  62. static void
  63. extension_def(Class& cl)
  64. {
  65. // Wrap the map's element (value_type)
  66. std::string elem_name = "map_indexing_suite_";
  67. object class_name(cl.attr("__name__"));
  68. extract<std::string> class_name_extractor(class_name);
  69. elem_name += class_name_extractor();
  70. elem_name += "_entry";
  71. typedef typename mpl::if_<
  72. mpl::and_<is_class<data_type>, mpl::bool_<!NoProxy> >
  73. , return_internal_reference<>
  74. , default_call_policies
  75. >::type get_data_return_policy;
  76. class_<value_type>(elem_name.c_str())
  77. .def("__repr__", &DerivedPolicies::print_elem)
  78. .def("data", &DerivedPolicies::get_data, get_data_return_policy())
  79. .def("key", &DerivedPolicies::get_key)
  80. ;
  81. }
  82. static object
  83. print_elem(typename Container::value_type const& e)
  84. {
  85. return "(%s, %s)" % python::make_tuple(e.first, e.second);
  86. }
  87. static
  88. typename mpl::if_<
  89. mpl::and_<is_class<data_type>, mpl::bool_<!NoProxy> >
  90. , data_type&
  91. , data_type
  92. >::type
  93. get_data(typename Container::value_type& e)
  94. {
  95. return e.second;
  96. }
  97. static typename Container::key_type
  98. get_key(typename Container::value_type& e)
  99. {
  100. return e.first;
  101. }
  102. static data_type&
  103. get_item(Container& container, index_type i_)
  104. {
  105. typename Container::iterator i = container.find(i_);
  106. if (i == container.end())
  107. {
  108. PyErr_SetString(PyExc_KeyError, "Invalid key");
  109. throw_error_already_set();
  110. }
  111. return i->second;
  112. }
  113. static void
  114. set_item(Container& container, index_type i, data_type const& v)
  115. {
  116. container[i] = v;
  117. }
  118. static void
  119. delete_item(Container& container, index_type i)
  120. {
  121. container.erase(i);
  122. }
  123. static size_t
  124. size(Container& container)
  125. {
  126. return container.size();
  127. }
  128. static bool
  129. contains(Container& container, key_type const& key)
  130. {
  131. return container.find(key) != container.end();
  132. }
  133. static bool
  134. compare_index(Container& container, index_type a, index_type b)
  135. {
  136. return container.key_comp()(a, b);
  137. }
  138. static index_type
  139. convert_index(Container& /*container*/, PyObject* i_)
  140. {
  141. extract<key_type const&> i(i_);
  142. if (i.check())
  143. {
  144. return i();
  145. }
  146. else
  147. {
  148. extract<key_type> i(i_);
  149. if (i.check())
  150. return i();
  151. }
  152. PyErr_SetString(PyExc_TypeError, "Invalid index type");
  153. throw_error_already_set();
  154. return index_type();
  155. }
  156. };
  157. }} // namespace boost::python
  158. #endif // MAP_INDEXING_SUITE_JDG20038_HPP