implicit.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 IMPLICIT_DWA2002326_HPP
  6. # define IMPLICIT_DWA2002326_HPP
  7. # include <boost/python/converter/rvalue_from_python_data.hpp>
  8. # include <boost/python/converter/registrations.hpp>
  9. # include <boost/python/converter/registered.hpp>
  10. # include <boost/python/extract.hpp>
  11. namespace boost { namespace python { namespace converter {
  12. template <class Source, class Target>
  13. struct implicit
  14. {
  15. static void* convertible(PyObject* obj)
  16. {
  17. // Find a converter which can produce a Source instance from
  18. // obj. The user has told us that Source can be converted to
  19. // Target, and instantiating construct() below, ensures that
  20. // at compile-time.
  21. return implicit_rvalue_convertible_from_python(obj, registered<Source>::converters)
  22. ? obj : 0;
  23. }
  24. static void construct(PyObject* obj, rvalue_from_python_stage1_data* data)
  25. {
  26. void* storage = ((rvalue_from_python_storage<Target>*)data)->storage.bytes;
  27. arg_from_python<Source> get_source(obj);
  28. bool convertible = get_source.convertible();
  29. BOOST_VERIFY(convertible);
  30. new (storage) Target(get_source());
  31. // record successful construction
  32. data->convertible = storage;
  33. }
  34. };
  35. }}} // namespace boost::python::converter
  36. #endif // IMPLICIT_DWA2002326_HPP