object_slices.hpp 4.0 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 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. # if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
  89. template <class U>
  90. template <class T, class V>
  91. inline const_object_slice
  92. object_operators<U>::slice(T const& start, V const& end) const
  93. {
  94. return this->slice(
  95. typename slice_bound<T>::type(start)
  96. , typename slice_bound<V>::type(end));
  97. }
  98. template <class U>
  99. template <class T, class V>
  100. inline object_slice
  101. object_operators<U>::slice(T const& start, V const& end)
  102. {
  103. return this->slice(
  104. typename slice_bound<T>::type(start)
  105. , typename slice_bound<V>::type(end));
  106. }
  107. # endif
  108. inline object const_slice_policies::get(object const& target, key_type const& key)
  109. {
  110. return getslice(target, key.first, key.second);
  111. }
  112. inline object const& slice_policies::set(
  113. object const& target
  114. , key_type const& key
  115. , object const& value)
  116. {
  117. setslice(target, key.first, key.second, value);
  118. return value;
  119. }
  120. inline void slice_policies::del(
  121. object const& target
  122. , key_type const& key)
  123. {
  124. delslice(target, key.first, key.second);
  125. }
  126. }}} // namespace boost::python::api
  127. #endif // OBJECT_SLICES_DWA2002615_HPP