• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 # include <boost/python/detail/prefix.hpp>
9 
10 # include <boost/python/proxy.hpp>
11 # include <boost/python/object_core.hpp>
12 # include <boost/python/object_protocol.hpp>
13 
14 namespace boost { namespace python { namespace api {
15 
16 struct const_attribute_policies
17 {
18     typedef char const* key_type;
19     static object get(object const& target, char const* key);
20     static object get(object const& target, object const& key);
21 };
22 
23 struct attribute_policies : const_attribute_policies
24 {
25     static object const& set(object const& target, char const* key, object const& value);
26     static void del(object const&target, char const* key);
27 };
28 
29 struct const_objattribute_policies
30 {
31     typedef object const key_type;
32     static object get(object const& target, object const& key);
33 };
34 
35 struct objattribute_policies : const_objattribute_policies
36 {
37     static object const& set(object const& target, object const& key, object const& value);
38     static void del(object const&target, object const& key);
39 };
40 
41 //
42 // implementation
43 //
44 template <class U>
attr(char const * name)45 inline object_attribute object_operators<U>::attr(char const* name)
46 {
47     object_cref2 x = *static_cast<U*>(this);
48     return object_attribute(x, name);
49 }
50 
51 template <class U>
attr(char const * name) const52 inline const_object_attribute object_operators<U>::attr(char const* name) const
53 {
54     object_cref2 x = *static_cast<U const*>(this);
55     return const_object_attribute(x, name);
56 }
57 
58 template <class U>
attr(object const & name)59 inline object_objattribute object_operators<U>::attr(object const& name)
60 {
61     object_cref2 x = *static_cast<U*>(this);
62     return object_objattribute(x, name);
63 }
64 
65 template <class U>
attr(object const & name) const66 inline const_object_objattribute object_operators<U>::attr(object const& name) const
67 {
68     object_cref2 x = *static_cast<U const*>(this);
69     return const_object_objattribute(x, name);
70 }
71 
get(object const & target,char const * key)72 inline object const_attribute_policies::get(object const& target, char const* key)
73 {
74     return python::getattr(target, key);
75 }
76 
get(object const & target,object const & key)77 inline object const_objattribute_policies::get(object const& target, object const& key)
78 {
79     return python::getattr(target, key);
80 }
81 
set(object const & target,char const * key,object const & value)82 inline object const& attribute_policies::set(
83     object const& target
84     , char const* key
85     , object const& value)
86 {
87     python::setattr(target, key, value);
88     return value;
89 }
90 
set(object const & target,object const & key,object const & value)91 inline object const& objattribute_policies::set(
92     object const& target
93     , object const& key
94     , object const& value)
95 {
96     python::setattr(target, key, value);
97     return value;
98 }
99 
del(object const & target,char const * key)100 inline void attribute_policies::del(
101     object const& target
102     , char const* key)
103 {
104     python::delattr(target, key);
105 }
106 
del(object const & target,object const & key)107 inline void objattribute_policies::del(
108     object const& target
109     , object const& key)
110 {
111     python::delattr(target, key);
112 }
113 
114 }}} // namespace boost::python::api
115 
116 #endif // OBJECT_ATTRIBUTES_DWA2002615_HPP
117