fix doc example typo
[boost.git] / boost / python / handle.hpp
blob461a219ad89a2513d404b71e2f003edea77e6120
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 HANDLE_DWA200269_HPP
6 # define HANDLE_DWA200269_HPP
8 # include <boost/python/detail/prefix.hpp>
10 # include <boost/python/cast.hpp>
11 # include <boost/python/errors.hpp>
12 # include <boost/python/borrowed.hpp>
13 # include <boost/python/handle_fwd.hpp>
14 # include <boost/python/refcount.hpp>
15 # include <boost/python/tag.hpp>
16 # include <boost/python/detail/raw_pyobject.hpp>
18 namespace boost { namespace python {
20 template <class T> struct null_ok;
22 template <class T>
23 inline null_ok<T>* allow_null(T* p)
25 return (null_ok<T>*)p;
28 namespace detail
30 template <class T>
31 inline T* manage_ptr(detail::borrowed<null_ok<T> >* p, int)
33 return python::xincref((T*)p);
36 template <class T>
37 inline T* manage_ptr(null_ok<detail::borrowed<T> >* p, int)
39 return python::xincref((T*)p);
42 template <class T>
43 inline T* manage_ptr(detail::borrowed<T>* p, long)
45 return python::incref(expect_non_null((T*)p));
48 template <class T>
49 inline T* manage_ptr(null_ok<T>* p, long)
51 return (T*)p;
54 template <class T>
55 inline T* manage_ptr(T* p, ...)
57 return expect_non_null(p);
61 template <class T>
62 class handle
64 typedef T* (handle::* bool_type )() const;
66 public: // types
67 typedef T element_type;
69 public: // member functions
70 handle();
71 ~handle();
73 template <class Y>
74 explicit handle(Y* p)
75 : m_p(
76 python::upcast<T>(
77 detail::manage_ptr(p, 0)
83 handle& operator=(handle const& r)
85 python::xdecref(m_p);
86 m_p = python::xincref(r.m_p);
87 return *this;
90 #if !defined(BOOST_MSVC) || (BOOST_MSVC >= 1300)
92 template<typename Y>
93 handle& operator=(handle<Y> const & r) // never throws
95 python::xdecref(m_p);
96 m_p = python::xincref(python::upcast<T>(r.get()));
97 return *this;
100 #endif
102 template <typename Y>
103 handle(handle<Y> const& r)
104 : m_p(python::xincref(python::upcast<T>(r.get())))
108 handle(handle const& r)
109 : m_p(python::xincref(r.m_p))
113 T* operator-> () const;
114 T& operator* () const;
115 T* get() const;
116 T* release();
117 void reset();
119 operator bool_type() const // never throws
121 return m_p ? &handle<T>::get : 0;
123 bool operator! () const; // never throws
125 public: // implementation details -- do not touch
126 // Defining this in the class body suppresses a VC7 link failure
127 inline handle(detail::borrowed_reference x)
128 : m_p(
129 python::incref(
130 downcast<T>((PyObject*)x)
135 private: // data members
136 T* m_p;
139 #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
140 } // namespace python
141 #endif
143 template<class T> inline T * get_pointer(python::handle<T> const & p)
145 return p.get();
148 #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
149 namespace python {
150 #else
152 // We don't want get_pointer above to hide the others
153 using boost::get_pointer;
155 #endif
157 typedef handle<PyTypeObject> type_handle;
160 // Compile-time introspection
162 # ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
163 template<typename T>
164 class is_handle
166 public:
167 BOOST_STATIC_CONSTANT(bool, value = false);
170 template<typename T>
171 class is_handle<handle<T> >
173 public:
174 BOOST_STATIC_CONSTANT(bool, value = true);
176 # else
177 namespace detail
179 typedef char (&yes_handle_t)[1];
180 typedef char (&no_handle_t)[2];
182 no_handle_t is_handle_test(...);
184 template<typename T>
185 yes_handle_t is_handle_test(boost::type< handle<T> >);
188 template<typename T>
189 class is_handle
191 public:
192 BOOST_STATIC_CONSTANT(
193 bool, value = (
194 sizeof(detail::is_handle_test(boost::type<T>()))
195 == sizeof(detail::yes_handle_t)));
197 # endif
200 // implementations
202 template <class T>
203 inline handle<T>::handle()
204 : m_p(0)
208 template <class T>
209 inline handle<T>::~handle()
211 python::xdecref(m_p);
214 template <class T>
215 inline T* handle<T>::operator->() const
217 return m_p;
220 template <class T>
221 inline T& handle<T>::operator*() const
223 return *m_p;
226 template <class T>
227 inline T* handle<T>::get() const
229 return m_p;
232 template <class T>
233 inline bool handle<T>::operator!() const
235 return m_p == 0;
238 template <class T>
239 inline T* handle<T>::release()
241 T* result = m_p;
242 m_p = 0;
243 return result;
246 template <class T>
247 inline void handle<T>::reset()
249 python::xdecref(m_p);
250 m_p = 0;
253 // Because get_managed_object must return a non-null PyObject*, we
254 // return Py_None if the handle is null.
255 template <class T>
256 inline PyObject* get_managed_object(handle<T> const& h, tag_t)
258 return h.get() ? python::upcast<PyObject>(h.get()) : Py_None;
261 }} // namespace boost::python
264 #endif // HANDLE_DWA200269_HPP