shared_ptr_to_python.hpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright David Abrahams 2003.
  2. // Copyright Stefan Seefeld 2016.
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef boost_python_converter_shared_ptr_to_python_hpp_
  7. #define boost_python_converter_shared_ptr_to_python_hpp_
  8. #include <boost/python/refcount.hpp>
  9. #include <boost/python/converter/shared_ptr_deleter.hpp>
  10. #include <boost/shared_ptr.hpp>
  11. #include <boost/get_pointer.hpp>
  12. namespace boost { namespace python { namespace converter {
  13. template <class T>
  14. PyObject* shared_ptr_to_python(shared_ptr<T> const& x)
  15. {
  16. if (!x)
  17. return python::detail::none();
  18. else if (shared_ptr_deleter* d = boost::get_deleter<shared_ptr_deleter>(x))
  19. return incref( get_pointer( d->owner ) );
  20. else
  21. return converter::registered<shared_ptr<T> const&>::converters.to_python(&x);
  22. }
  23. #if !defined(BOOST_NO_CXX11_SMART_PTR)
  24. template <class T>
  25. PyObject* shared_ptr_to_python(std::shared_ptr<T> const& x)
  26. {
  27. if (!x)
  28. return python::detail::none();
  29. else if (shared_ptr_deleter* d = std::get_deleter<shared_ptr_deleter>(x))
  30. return incref(get_pointer(d->owner));
  31. else
  32. return converter::registered<std::shared_ptr<T> const&>::converters.to_python(&x);
  33. }
  34. #endif
  35. }}} // namespace boost::python::converter
  36. #endif