object_items.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_ITEMS_DWA2002615_HPP
  6. # define OBJECT_ITEMS_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. namespace boost { namespace python { namespace api {
  12. struct const_item_policies
  13. {
  14. typedef object key_type;
  15. static object get(object const& target, object const& key);
  16. };
  17. struct item_policies : const_item_policies
  18. {
  19. static object const& set(object const& target, object const& key, object const& value);
  20. static void del(object const& target, object const& key);
  21. };
  22. //
  23. // implementation
  24. //
  25. template <class U>
  26. inline object_item
  27. object_operators<U>::operator[](object_cref key)
  28. {
  29. object_cref2 x = *static_cast<U*>(this);
  30. return object_item(x, key);
  31. }
  32. template <class U>
  33. inline const_object_item
  34. object_operators<U>::operator[](object_cref key) const
  35. {
  36. object_cref2 x = *static_cast<U const*>(this);
  37. return const_object_item(x, key);
  38. }
  39. # if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
  40. template <class U>
  41. template <class T>
  42. inline const_object_item
  43. object_operators<U>::operator[](T const& key) const
  44. {
  45. return (*this)[object(key)];
  46. }
  47. template <class U>
  48. template <class T>
  49. inline object_item
  50. object_operators<U>::operator[](T const& key)
  51. {
  52. return (*this)[object(key)];
  53. }
  54. # endif
  55. inline object const_item_policies::get(object const& target, object const& key)
  56. {
  57. return getitem(target, key);
  58. }
  59. inline object const& item_policies::set(
  60. object const& target
  61. , object const& key
  62. , object const& value)
  63. {
  64. setitem(target, key, value);
  65. return value;
  66. }
  67. inline void item_policies::del(
  68. object const& target
  69. , object const& key)
  70. {
  71. delitem(target, key);
  72. }
  73. }}} // namespace boost::python::api
  74. #endif // OBJECT_ITEMS_DWA2002615_HPP