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;
23 inline null_ok<T>* allow_null(T* p)
25 return (null_ok<T>*)p;
31 inline T* manage_ptr(detail::borrowed<null_ok<T> >* p, int)
33 return python::xincref((T*)p);
37 inline T* manage_ptr(null_ok<detail::borrowed<T> >* p, int)
39 return python::xincref((T*)p);
43 inline T* manage_ptr(detail::borrowed<T>* p, long)
45 return python::incref(expect_non_null((T*)p));
49 inline T* manage_ptr(null_ok<T>* p, long)
55 inline T* manage_ptr(T* p, ...)
57 return expect_non_null(p);
64 typedef T* (handle::* bool_type )() const;
67 typedef T element_type;
69 public: // member functions
77 detail::manage_ptr(p, 0)
83 handle& operator=(handle const& r)
86 m_p = python::xincref(r.m_p);
91 handle& operator=(handle<Y> const & r) // never throws
94 m_p = python::xincref(python::upcast<T>(r.get()));
99 handle(handle<Y> const& r)
100 : m_p(python::xincref(python::upcast<T>(r.get())))
104 handle(handle const& r)
105 : m_p(python::xincref(r.m_p))
109 T* operator-> () const;
110 T& operator* () const;
115 operator bool_type() const // never throws
117 return m_p ? &handle<T>::get : 0;
119 bool operator! () const; // never throws
121 public: // implementation details -- do not touch
122 // Defining this in the class body suppresses a VC7 link failure
123 inline handle(detail::borrowed_reference x)
126 downcast<T>((PyObject*)x)
131 private: // data members
135 #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
136 } // namespace python
139 template<class T> inline T * get_pointer(python::handle<T> const & p)
144 #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
148 // We don't want get_pointer above to hide the others
149 using boost::get_pointer;
153 typedef handle<PyTypeObject> type_handle;
156 // Compile-time introspection
162 BOOST_STATIC_CONSTANT(bool, value = false);
166 class is_handle<handle<T> >
169 BOOST_STATIC_CONSTANT(bool, value = true);
176 inline handle<T>::handle()
182 inline handle<T>::~handle()
184 python::xdecref(m_p);
188 inline T* handle<T>::operator->() const
194 inline T& handle<T>::operator*() const
200 inline T* handle<T>::get() const
206 inline bool handle<T>::operator!() const
212 inline T* handle<T>::release()
220 inline void handle<T>::reset()
222 python::xdecref(m_p);
226 // Because get_managed_object must return a non-null PyObject*, we
227 // return Py_None if the handle is null.
229 inline PyObject* get_managed_object(handle<T> const& h, tag_t)
231 return h.get() ? python::upcast<PyObject>(h.get()) : Py_None;
234 }} // namespace boost::python
237 #endif // HANDLE_DWA200269_HPP