refcount.hpp 960 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 REFCOUNT_DWA2002615_HPP
  6. # define REFCOUNT_DWA2002615_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/python/cast.hpp>
  9. namespace boost { namespace python {
  10. template <class T>
  11. inline T* incref(T* p)
  12. {
  13. Py_INCREF(python::upcast<PyObject>(p));
  14. return p;
  15. }
  16. template <class T>
  17. inline T* xincref(T* p)
  18. {
  19. Py_XINCREF(python::upcast<PyObject>(p));
  20. return p;
  21. }
  22. template <class T>
  23. inline void decref(T* p)
  24. {
  25. assert( Py_REFCNT(python::upcast<PyObject>(p)) > 0 );
  26. Py_DECREF(python::upcast<PyObject>(p));
  27. }
  28. template <class T>
  29. inline void xdecref(T* p)
  30. {
  31. assert( !p || Py_REFCNT(python::upcast<PyObject>(p)) > 0 );
  32. Py_XDECREF(python::upcast<PyObject>(p));
  33. }
  34. }} // namespace boost::python
  35. #endif // REFCOUNT_DWA2002615_HPP