object_slices.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 OBJECT_SLICES_DWA2002615_HPP
  6. # define OBJECT_SLICES_DWA2002615_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/python/proxy.hpp>
  9. # include <boost/python/object_core.hpp>
  10. # include <boost/python/object_protocol.hpp>
  11. # include <boost/python/handle.hpp>
  12. # include <utility>
  13. namespace boost { namespace python { namespace api {
  14. struct const_slice_policies
  15. {
  16. typedef std::pair<handle<>, handle<> > key_type;
  17. static object get(object const& target, key_type const& key);
  18. };
  19. struct slice_policies : const_slice_policies
  20. {
  21. static object const& set(object const& target, key_type const& key, object const& value);
  22. static void del(object const& target, key_type const& key);
  23. };
  24. template <class T, class U>
  25. inline slice_policies::key_type slice_key(T x, U y)
  26. {
  27. return slice_policies::key_type(handle<>(x), handle<>(y));
  28. }
  29. //
  30. // implementation
  31. //
  32. template <class U>
  33. object_slice
  34. object_operators<U>::slice(object_cref start, object_cref finish)
  35. {
  36. object_cref2 x = *static_cast<U*>(this);
  37. return object_slice(x, api::slice_key(borrowed(start.ptr()), borrowed(finish.ptr())));
  38. }
  39. template <class U>
  40. const_object_slice
  41. object_operators<U>::slice(object_cref start, object_cref finish) const
  42. {
  43. object_cref2 x = *static_cast<U const*>(this);
  44. return const_object_slice(x, api::slice_key(borrowed(start.ptr()), borrowed(finish.ptr())));
  45. }
  46. template <class U>
  47. object_slice
  48. object_operators<U>::slice(slice_nil, object_cref finish)
  49. {
  50. object_cref2 x = *static_cast<U*>(this);
  51. return object_slice(x, api::slice_key(allow_null((PyObject*)0), borrowed(finish.ptr())));
  52. }
  53. template <class U>
  54. const_object_slice
  55. object_operators<U>::slice(slice_nil, object_cref finish) const
  56. {
  57. object_cref2 x = *static_cast<U const*>(this);
  58. return const_object_slice(x, api::slice_key(allow_null((PyObject*)0), borrowed(finish.ptr())));
  59. }
  60. template <class U>
  61. object_slice
  62. object_operators<U>::slice(slice_nil, slice_nil)
  63. {
  64. object_cref2 x = *static_cast<U*>(this);
  65. return object_slice(x, api::slice_key(allow_null((PyObject*)0), allow_null((PyObject*)0)));
  66. }
  67. template <class U>
  68. const_object_slice
  69. object_operators<U>::slice(slice_nil, slice_nil) const
  70. {
  71. object_cref2 x = *static_cast<U const*>(this);
  72. return const_object_slice(x, api::slice_key(allow_null((PyObject*)0), allow_null((PyObject*)0)));
  73. }
  74. template <class U>
  75. object_slice
  76. object_operators<U>::slice(object_cref start, slice_nil)
  77. {
  78. object_cref2 x = *static_cast<U*>(this);
  79. return object_slice(x, api::slice_key(borrowed(start.ptr()), allow_null((PyObject*)0)));
  80. }
  81. template <class U>
  82. const_object_slice
  83. object_operators<U>::slice(object_cref start, slice_nil) const
  84. {
  85. object_cref2 x = *static_cast<U const*>(this);
  86. return const_object_slice(x, api::slice_key(borrowed(start.ptr()), allow_null((PyObject*)0)));
  87. }
  88. template <class U>
  89. template <class T, class V>
  90. inline const_object_slice
  91. object_operators<U>::slice(T const& start, V const& end) const
  92. {
  93. return this->slice(
  94. typename slice_bound<T>::type(start)
  95. , typename slice_bound<V>::type(end));
  96. }
  97. template <class U>
  98. template <class T, class V>
  99. inline object_slice
  100. object_operators<U>::slice(T const& start, V const& end)
  101. {
  102. return this->slice(
  103. typename slice_bound<T>::type(start)
  104. , typename slice_bound<V>::type(end));
  105. }
  106. inline object const_slice_policies::get(object const& target, key_type const& key)
  107. {
  108. return getslice(target, key.first, key.second);
  109. }
  110. inline object const& slice_policies::set(
  111. object const& target
  112. , key_type const& key
  113. , object const& value)
  114. {
  115. setslice(target, key.first, key.second, value);
  116. return value;
  117. }
  118. inline void slice_policies::del(
  119. object const& target
  120. , key_type const& key)
  121. {
  122. delslice(target, key.first, key.second);
  123. }
  124. }}} // namespace boost::python::api
  125. #endif // OBJECT_SLICES_DWA2002615_HPP