object_items.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. template <class U>
  40. template <class T>
  41. inline const_object_item
  42. object_operators<U>::operator[](T const& key) const
  43. {
  44. return (*this)[object(key)];
  45. }
  46. template <class U>
  47. template <class T>
  48. inline object_item
  49. object_operators<U>::operator[](T const& key)
  50. {
  51. return (*this)[object(key)];
  52. }
  53. inline object const_item_policies::get(object const& target, object const& key)
  54. {
  55. return getitem(target, key);
  56. }
  57. inline object const& item_policies::set(
  58. object const& target
  59. , object const& key
  60. , object const& value)
  61. {
  62. setitem(target, key, value);
  63. return value;
  64. }
  65. inline void item_policies::del(
  66. object const& target
  67. , object const& key)
  68. {
  69. delitem(target, key);
  70. }
  71. }}} // namespace boost::python::api
  72. #endif // OBJECT_ITEMS_DWA2002615_HPP