[29] | 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 SCOPE_DWA2002724_HPP |
---|
| 6 | # define SCOPE_DWA2002724_HPP |
---|
| 7 | |
---|
| 8 | # include <boost/python/detail/prefix.hpp> |
---|
| 9 | # include <boost/python/object.hpp> |
---|
| 10 | # include <boost/python/refcount.hpp> |
---|
| 11 | # include <boost/utility.hpp> |
---|
| 12 | |
---|
| 13 | namespace boost { namespace python { |
---|
| 14 | |
---|
| 15 | namespace detail |
---|
| 16 | { |
---|
| 17 | // Making this a namespace-scope variable to avoid Cygwin issues. |
---|
| 18 | // Use a PyObject* to avoid problems with static destruction after Py_Finalize |
---|
| 19 | extern BOOST_PYTHON_DECL PyObject* current_scope; |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | class scope |
---|
| 23 | : public object |
---|
| 24 | { |
---|
| 25 | public: |
---|
| 26 | inline scope(scope const&); |
---|
| 27 | inline scope(object const&); |
---|
| 28 | inline scope(); |
---|
| 29 | inline ~scope(); |
---|
| 30 | |
---|
| 31 | private: // data members |
---|
| 32 | PyObject* m_previous_scope; |
---|
| 33 | |
---|
| 34 | private: // unimplemented functions |
---|
| 35 | void operator=(scope const&); |
---|
| 36 | }; |
---|
| 37 | |
---|
| 38 | inline scope::scope(object const& new_scope) |
---|
| 39 | : object(new_scope) |
---|
| 40 | , m_previous_scope(detail::current_scope) |
---|
| 41 | { |
---|
| 42 | detail::current_scope = python::incref(new_scope.ptr()); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | inline scope::scope() |
---|
| 46 | : object(detail::borrowed_reference( |
---|
| 47 | detail::current_scope ? detail::current_scope : Py_None |
---|
| 48 | )) |
---|
| 49 | , m_previous_scope(python::xincref(detail::current_scope)) |
---|
| 50 | { |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | inline scope::~scope() |
---|
| 54 | { |
---|
| 55 | python::xdecref(detail::current_scope); |
---|
| 56 | detail::current_scope = m_previous_scope; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | namespace converter |
---|
| 60 | { |
---|
| 61 | template <> |
---|
| 62 | struct object_manager_traits<scope> |
---|
| 63 | : object_manager_traits<object> |
---|
| 64 | { |
---|
| 65 | }; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | // Placing this after the specialization above suppresses a CWPro8.3 bug |
---|
| 69 | inline scope::scope(scope const& new_scope) |
---|
| 70 | : object(new_scope) |
---|
| 71 | , m_previous_scope(detail::current_scope) |
---|
| 72 | { |
---|
| 73 | detail::current_scope = python::incref(new_scope.ptr()); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | }} // namespace boost::python |
---|
| 77 | |
---|
| 78 | #endif // SCOPE_DWA2002724_HPP |
---|