list.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #ifndef LIST_DWA2002627_HPP
  6. # define LIST_DWA2002627_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/python/object.hpp>
  9. # include <boost/python/converter/pytype_object_mgr_traits.hpp>
  10. # include <boost/python/ssize_t.hpp>
  11. namespace boost { namespace python {
  12. namespace detail
  13. {
  14. struct BOOST_PYTHON_DECL list_base : object
  15. {
  16. void append(object_cref); // append object to end
  17. ssize_t count(object_cref value) const; // return number of occurrences of value
  18. void extend(object_cref sequence); // extend list by appending sequence elements
  19. long index(object_cref value) const; // return index of first occurrence of value
  20. void insert(ssize_t index, object_cref); // insert object before index
  21. void insert(object const& index, object_cref);
  22. object pop(); // remove and return item at index (default last)
  23. object pop(ssize_t index);
  24. object pop(object const& index);
  25. void remove(object_cref value); // remove first occurrence of value
  26. void reverse(); // reverse *IN PLACE*
  27. void sort(); // sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1
  28. #if PY_VERSION_HEX >= 0x03000000
  29. void sort(args_proxy const &args,
  30. kwds_proxy const &kwds);
  31. #else
  32. void sort(object_cref cmpfunc);
  33. #endif
  34. protected:
  35. list_base(); // new list
  36. explicit list_base(object_cref sequence); // new list initialized from sequence's items
  37. BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(list_base, object)
  38. private:
  39. static detail::new_non_null_reference call(object const&);
  40. };
  41. }
  42. class list : public detail::list_base
  43. {
  44. typedef detail::list_base base;
  45. public:
  46. list() {} // new list
  47. template <class T>
  48. explicit list(T const& sequence)
  49. : base(object(sequence))
  50. {
  51. }
  52. template <class T>
  53. void append(T const& x)
  54. {
  55. base::append(object(x));
  56. }
  57. template <class T>
  58. ssize_t count(T const& value) const
  59. {
  60. return base::count(object(value));
  61. }
  62. template <class T>
  63. void extend(T const& x)
  64. {
  65. base::extend(object(x));
  66. }
  67. template <class T>
  68. long index(T const& x) const
  69. {
  70. return base::index(object(x));
  71. }
  72. template <class T>
  73. void insert(ssize_t index, T const& x) // insert object before index
  74. {
  75. base::insert(index, object(x));
  76. }
  77. template <class T>
  78. void insert(object const& index, T const& x) // insert object before index
  79. {
  80. base::insert(index, object(x));
  81. }
  82. object pop() { return base::pop(); }
  83. object pop(ssize_t index) { return base::pop(index); }
  84. template <class T>
  85. object pop(T const& index)
  86. {
  87. return base::pop(object(index));
  88. }
  89. template <class T>
  90. void remove(T const& value)
  91. {
  92. base::remove(object(value));
  93. }
  94. #if PY_VERSION_HEX <= 0x03000000
  95. void sort() { base::sort(); }
  96. template <class T>
  97. void sort(T const& value)
  98. {
  99. base::sort(object(value));
  100. }
  101. #endif
  102. public: // implementation detail -- for internal use only
  103. BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(list, base)
  104. };
  105. //
  106. // Converter Specializations
  107. //
  108. namespace converter
  109. {
  110. template <>
  111. struct object_manager_traits<list>
  112. : pytype_object_manager_traits<&PyList_Type,list>
  113. {
  114. };
  115. }
  116. }} // namespace boost::python
  117. #endif // LIST_DWA2002627_HPP