object_attributes.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_ATTRIBUTES_DWA2002615_HPP
  6. # define OBJECT_ATTRIBUTES_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_attribute_policies
  13. {
  14. typedef char const* key_type;
  15. static object get(object const& target, char const* key);
  16. static object get(object const& target, object const& key);
  17. };
  18. struct attribute_policies : const_attribute_policies
  19. {
  20. static object const& set(object const& target, char const* key, object const& value);
  21. static void del(object const&target, char const* key);
  22. };
  23. struct const_objattribute_policies
  24. {
  25. typedef object const key_type;
  26. static object get(object const& target, object const& key);
  27. };
  28. struct objattribute_policies : const_objattribute_policies
  29. {
  30. static object const& set(object const& target, object const& key, object const& value);
  31. static void del(object const&target, object const& key);
  32. };
  33. //
  34. // implementation
  35. //
  36. template <class U>
  37. inline object_attribute object_operators<U>::attr(char const* name)
  38. {
  39. object_cref2 x = *static_cast<U*>(this);
  40. return object_attribute(x, name);
  41. }
  42. template <class U>
  43. inline const_object_attribute object_operators<U>::attr(char const* name) const
  44. {
  45. object_cref2 x = *static_cast<U const*>(this);
  46. return const_object_attribute(x, name);
  47. }
  48. template <class U>
  49. inline object_objattribute object_operators<U>::attr(object const& name)
  50. {
  51. object_cref2 x = *static_cast<U*>(this);
  52. return object_objattribute(x, name);
  53. }
  54. template <class U>
  55. inline const_object_objattribute object_operators<U>::attr(object const& name) const
  56. {
  57. object_cref2 x = *static_cast<U const*>(this);
  58. return const_object_objattribute(x, name);
  59. }
  60. inline object const_attribute_policies::get(object const& target, char const* key)
  61. {
  62. return python::getattr(target, key);
  63. }
  64. inline object const_objattribute_policies::get(object const& target, object const& key)
  65. {
  66. return python::getattr(target, key);
  67. }
  68. inline object const& attribute_policies::set(
  69. object const& target
  70. , char const* key
  71. , object const& value)
  72. {
  73. python::setattr(target, key, value);
  74. return value;
  75. }
  76. inline object const& objattribute_policies::set(
  77. object const& target
  78. , object const& key
  79. , object const& value)
  80. {
  81. python::setattr(target, key, value);
  82. return value;
  83. }
  84. inline void attribute_policies::del(
  85. object const& target
  86. , char const* key)
  87. {
  88. python::delattr(target, key);
  89. }
  90. inline void objattribute_policies::del(
  91. object const& target
  92. , object const& key)
  93. {
  94. python::delattr(target, key);
  95. }
  96. }}} // namespace boost::python::api
  97. #endif // OBJECT_ATTRIBUTES_DWA2002615_HPP