|
12 | 12 |
|
13 | 13 | namespace boost { namespace python { |
14 | 14 |
|
| 15 | +// Put this in an inner namespace so that the generalized operators won't take over |
| 16 | +namespace api |
| 17 | +{ |
| 18 | + |
15 | 19 | // This file contains the definition of the object class and enough to |
16 | 20 | // construct/copy it, but not enough to do operations like |
17 | 21 | // attribute/item access or addition. |
18 | 22 |
|
19 | | -template <class Policies> class proxy; |
20 | | -namespace detail |
21 | | -{ |
| 23 | + template <class Policies> class proxy; |
| 24 | + |
22 | 25 | struct const_attribute_policies; |
23 | 26 | struct attribute_policies; |
24 | 27 | struct const_item_policies; |
25 | 28 | struct item_policies; |
26 | | -} |
27 | | -typedef proxy<detail::const_attribute_policies> const_object_attribute; |
28 | | -typedef proxy<detail::attribute_policies> object_attribute; |
29 | | -typedef proxy<detail::const_item_policies> const_object_item; |
30 | | -typedef proxy<detail::item_policies> object_item; |
31 | 29 |
|
32 | | -class object |
33 | | -{ |
| 30 | + typedef proxy<const_attribute_policies> const_object_attribute; |
| 31 | + typedef proxy<attribute_policies> object_attribute; |
| 32 | + typedef proxy<const_item_policies> const_object_item; |
| 33 | + typedef proxy<item_policies> object_item; |
| 34 | + |
| 35 | + // A way to turn a conrete type T into a type dependent on U. This |
| 36 | + // keeps conforming compilers from complaining about returning an |
| 37 | + // incomplete T from a template member function (which must be |
| 38 | + // defined in the class body to keep MSVC happy). |
| 39 | + template <class T, class U> |
| 40 | + struct dependent |
| 41 | + { |
| 42 | + typedef T type; |
| 43 | + }; |
| 44 | + |
| 45 | + class object |
| 46 | + { |
34 | 47 | # if !defined(BOOST_MSVC) || BOOST_MSVC > 1200 |
35 | | - typedef object const& self_cref; |
| 48 | + typedef object const& self_cref; |
36 | 49 | # else |
37 | | - typedef object self_cref; |
| 50 | + typedef object self_cref; |
38 | 51 | # endif |
39 | | - public: |
| 52 | + public: |
40 | 53 | # ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING |
41 | | - // copy constructor without NULL checking, for efficiency |
42 | | - object(object const&); |
| 54 | + // copy constructor without NULL checking, for efficiency |
| 55 | + object(object const&); |
43 | 56 | # endif |
44 | 57 |
|
45 | | - // explicit conversion from any C++ object to Python |
46 | | - template <class T> |
47 | | - explicit object(T const& x) |
48 | | - : m_ptr( |
49 | | - python::borrowed( |
50 | | - python::allow_null( // null check is already done |
51 | | - converter::arg_to_python<T>(x).get()) |
52 | | - ) |
53 | | - ) |
54 | | - { |
55 | | - } |
56 | | - |
57 | | - // capture this case explicitly to handle string |
58 | | - // literals. Otherwise, they get deduced as char[N]const& above |
59 | | - // and conversion fails at runtime. |
60 | | - explicit object(char const* x) |
61 | | - : m_ptr( |
62 | | - python::borrowed( |
63 | | - python::allow_null( // null check is already done |
64 | | - converter::arg_to_python<char const*>(x).get()) |
65 | | - ) |
66 | | - ) |
67 | | - { |
| 58 | + // explicit conversion from any C++ object to Python |
| 59 | + template <class T> |
| 60 | + explicit object(T const& x) |
| 61 | + : m_ptr( |
| 62 | + python::borrowed( |
| 63 | + python::allow_null( // null check is already done |
| 64 | + converter::arg_to_python<T>(x).get()) |
| 65 | + ) |
| 66 | + ) |
| 67 | + { |
| 68 | + } |
| 69 | + |
| 70 | + // capture this case explicitly to handle string |
| 71 | + // literals. Otherwise, they get deduced as char[N]const& above |
| 72 | + // and conversion fails at runtime. |
| 73 | + explicit object(char const* x) |
| 74 | + : m_ptr( |
| 75 | + python::borrowed( |
| 76 | + python::allow_null( // null check is already done |
| 77 | + converter::arg_to_python<char const*>(x).get()) |
| 78 | + ) |
| 79 | + ) |
| 80 | + { |
68 | 81 |
|
69 | | - } |
| 82 | + } |
70 | 83 |
|
71 | | - // Throw error_already_set() if the handle is null. |
72 | | - explicit object(handle<> const&); |
| 84 | + // Throw error_already_set() if the handle is null. |
| 85 | + explicit object(handle<> const&); |
73 | 86 |
|
74 | | - // Attribute access via x._("attribute_name") |
75 | | - const_object_attribute _(char const*) const; |
76 | | - object_attribute _(char const*); |
| 87 | + // Attribute access via x.attr("attribute_name") |
| 88 | + const_object_attribute attr(char const*) const; |
| 89 | + object_attribute attr(char const*); |
77 | 90 |
|
78 | | - object operator()() const |
79 | | - { |
80 | | - return object(call<handle<> >(m_ptr.get())); |
81 | | - } |
| 91 | + object operator()() const |
| 92 | + { |
| 93 | + return object(call<handle<> >(m_ptr.get())); |
| 94 | + } |
82 | 95 |
|
83 | 96 | # ifndef BOOST_PYTHON_GENERATE_CODE |
84 | 97 | # include <boost/python/preprocessed/object_call.hpp> |
85 | 98 | # endif |
86 | 99 |
|
87 | 100 | # define BOOST_PYTHON_OBJECT_CALL(nargs,ignored) \ |
88 | | - template <BOOST_PP_ENUM_PARAMS(nargs, class A)> \ |
89 | | - object operator()(BOOST_PYTHON_ENUM_PARAMS2(nargs, (A,const& a))) const \ |
90 | | - { \ |
91 | | - return object(call<handle<> >(&**this, BOOST_PP_ENUM_PARAMS(nargs, a))); \ |
92 | | - } |
93 | | - |
94 | | - BOOST_PYTHON_REPEAT_ARITY_2ND(BOOST_PYTHON_OBJECT_CALL, ignored) |
95 | | - |
96 | | - // truth value testing |
97 | | - typedef handle<> (object::*bool_type); |
98 | | - operator bool_type() const; |
99 | | - bool operator!() const; // needed for vc6 |
100 | | - |
101 | | - // item access |
102 | | - const_object_item operator[](self_cref) const; |
103 | | - object_item operator[](self_cref); |
| 101 | + template <BOOST_PP_ENUM_PARAMS(nargs, class A)> \ |
| 102 | + object operator()(BOOST_PYTHON_ENUM_PARAMS2(nargs, (A,const& a))) const \ |
| 103 | + { \ |
| 104 | + return object(call<handle<> >(&**this, BOOST_PP_ENUM_PARAMS(nargs, a))); \ |
| 105 | + } |
| 106 | + |
| 107 | + BOOST_PYTHON_REPEAT_ARITY_2ND(BOOST_PYTHON_OBJECT_CALL, ignored) |
| 108 | + |
| 109 | + // truth value testing |
| 110 | + typedef handle<> (object::*bool_type); |
| 111 | + operator bool_type() const; |
| 112 | + bool operator!() const; // needed for vc6 |
| 113 | + |
| 114 | + // item access |
| 115 | + const_object_item operator[](self_cref) const; |
| 116 | + object_item operator[](self_cref); |
104 | 117 |
|
105 | | - template <class T> |
106 | | - const_object_item operator[](T const& key) const |
107 | | - { |
108 | | - return (*this)[object(key)]; |
109 | | - } |
| 118 | + template <class T> |
| 119 | +# if BOOST_MSVC != 1300 |
| 120 | + typename dependent<const_object_item,T>::type |
| 121 | +# else |
| 122 | + const_object_item |
| 123 | +# endif |
| 124 | + operator[](T const& key) const |
| 125 | + { |
| 126 | + return (*this)[object(key)]; |
| 127 | + } |
110 | 128 |
|
111 | | - template <class T> |
112 | | - object_item operator[](T const& key) |
113 | | - { |
114 | | - return (*this)[object(key)]; |
115 | | - } |
| 129 | + template <class T> |
| 130 | +# if BOOST_MSVC != 1300 |
| 131 | + typename dependent<object_item,T>::type |
| 132 | +# else |
| 133 | + object_item |
| 134 | +# endif |
| 135 | + operator[](T const& key) |
| 136 | + { |
| 137 | + return (*this)[object(key)]; |
| 138 | + } |
116 | 139 |
|
117 | | - // Underlying object access |
118 | | - PyObject* operator->() const; |
119 | | - PyObject& operator*() const; |
120 | | - |
121 | | - public: // implementation detail -- for internal use only |
122 | | - object(null_ok<detail::borrowed<PyObject> >*); |
123 | | - object(detail::borrowed<null_ok<PyObject> >*); |
124 | | - object(detail::borrowed<PyObject>*); |
125 | | - class new_pyobject_reference; |
126 | | - object(new_pyobject_reference*); |
| 140 | + // Underlying object access |
| 141 | + PyObject* operator->() const; |
| 142 | + PyObject& operator*() const; |
| 143 | + |
| 144 | + public: // implementation detail -- for internal use only |
| 145 | + object(null_ok<detail::borrowed<PyObject> >*); |
| 146 | + object(detail::borrowed<null_ok<PyObject> >*); |
| 147 | + object(detail::borrowed<PyObject>*); |
| 148 | + class new_pyobject_reference; |
| 149 | + object(new_pyobject_reference*); |
127 | 150 |
|
128 | | - private: |
129 | | - handle<> m_ptr; |
130 | | -}; |
| 151 | + private: |
| 152 | + handle<> m_ptr; |
| 153 | + }; |
| 154 | +} |
| 155 | +using api::object; |
131 | 156 |
|
132 | 157 | // |
133 | 158 | // Converter Specializations |
|
0 commit comments