iterator.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright David Abrahams 2002.
  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. #include <boost/python/module.hpp>
  6. #include <boost/python/def.hpp>
  7. #include <boost/python/class.hpp>
  8. #include <boost/python/return_internal_reference.hpp>
  9. #include <boost/python/copy_non_const_reference.hpp>
  10. #include <boost/python/return_value_policy.hpp>
  11. #include <boost/python/iterator.hpp>
  12. #include <list>
  13. #include <utility>
  14. #include <iterator>
  15. #include <algorithm>
  16. using namespace boost::python;
  17. typedef std::list<int> list_int;
  18. typedef std::list<list_int> list_list;
  19. void push_back(list_int& x, int y)
  20. {
  21. x.push_back(y);
  22. }
  23. void push_list_back(list_list& x, list_int const& y)
  24. {
  25. x.push_back(y);
  26. }
  27. int back(list_int& x)
  28. {
  29. return x.back();
  30. }
  31. typedef std::pair<list_int::iterator,list_int::iterator> list_range;
  32. struct list_range2 : list_range
  33. {
  34. list_int::iterator& begin() { return this->first; }
  35. list_int::iterator& end() { return this->second; }
  36. };
  37. list_range range(list_int& x)
  38. {
  39. return list_range(x.begin(), x.end());
  40. }
  41. struct two_lists
  42. {
  43. two_lists()
  44. {
  45. int primes[] = { 2, 3, 5, 7, 11, 13 };
  46. std::copy(primes, primes + sizeof(primes)/sizeof(*primes), std::back_inserter(one));
  47. int evens[] = { 2, 4, 6, 8, 10, 12 };
  48. std::copy(evens, evens + sizeof(evens)/sizeof(*evens), std::back_inserter(two));
  49. }
  50. struct two_start
  51. {
  52. typedef list_int::iterator result_type;
  53. result_type operator()(two_lists& ll) const { return ll.two.begin(); }
  54. };
  55. friend struct two_start;
  56. list_int::iterator one_begin() { return one.begin(); }
  57. list_int::iterator two_begin() { return two.begin(); }
  58. list_int::iterator one_end() { return one.end(); }
  59. list_int::iterator two_end() { return two.end(); }
  60. private:
  61. list_int one;
  62. list_int two;
  63. };
  64. BOOST_PYTHON_MODULE(iterator_ext)
  65. {
  66. using boost::python::iterator; // gcc 2.96 bug workaround
  67. def("range", &::range);
  68. class_<list_int>("list_int")
  69. .def("push_back", push_back)
  70. .def("back", back)
  71. .def("__iter__", iterator<list_int>())
  72. ;
  73. class_<list_range>("list_range")
  74. // We can specify data members
  75. .def("__iter__"
  76. , range(&list_range::first, &list_range::second))
  77. ;
  78. // No runtime tests for this one yet
  79. class_<list_range2>("list_range2")
  80. // We can specify member functions returning a non-const reference
  81. .def("__iter__", range(&list_range2::begin, &list_range2::end))
  82. ;
  83. class_<two_lists>("two_lists")
  84. // We can spcify member functions
  85. .add_property(
  86. "primes"
  87. , range(&two_lists::one_begin, &two_lists::one_end))
  88. // Prove that we can explicitly specify call policies
  89. .add_property(
  90. "evens"
  91. , range<return_value_policy<copy_non_const_reference> >(
  92. &two_lists::two_begin, &two_lists::two_end))
  93. // Prove that we can specify call policies and target
  94. .add_property(
  95. "twosies"
  96. , range<return_value_policy<copy_non_const_reference>, two_lists>(
  97. // And we can use adaptable function objects when
  98. // partial specialization is available.
  99. # ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  100. two_lists::two_start()
  101. # else
  102. &two_lists::two_begin
  103. # endif
  104. , &two_lists::two_end))
  105. ;
  106. class_<list_list>("list_list")
  107. .def("push_back", push_list_back)
  108. .def("__iter__", iterator<list_list,return_internal_reference<> >())
  109. ;
  110. }
  111. #include "module_tail.cpp"