pyobject_type.hpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 PYOBJECT_TYPE_DWA2002720_HPP
  6. # define PYOBJECT_TYPE_DWA2002720_HPP
  7. # include <boost/python/cast.hpp>
  8. namespace boost { namespace python { namespace converter {
  9. BOOST_PYTHON_DECL inline
  10. PyObject* checked_downcast_impl(PyObject *obj, PyTypeObject *type)
  11. {
  12. return (PyType_IsSubtype(Py_TYPE(obj), type) ? obj : NULL);
  13. }
  14. // Used as a base class for specializations which need to provide
  15. // Python type checking capability.
  16. template <class Object, PyTypeObject* pytype>
  17. struct pyobject_type
  18. {
  19. static bool check(PyObject* x)
  20. {
  21. return ::PyObject_IsInstance(x, (PyObject*)pytype);
  22. }
  23. static Object* checked_downcast(PyObject* x)
  24. {
  25. return python::downcast<Object>(
  26. (checked_downcast_impl)(x, pytype)
  27. );
  28. }
  29. #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
  30. static PyTypeObject const* get_pytype() { return pytype; }
  31. #endif
  32. };
  33. }}} // namespace boost::python::converter
  34. #endif // PYOBJECT_TYPE_DWA2002720_HPP