scope.hpp 1.8 KB

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