|
| 1 | +// Copyright David Abrahams 2002. Permission to copy, use, |
| 2 | +// modify, sell and distribute this software is granted provided this |
| 3 | +// copyright notice appears in all copies. This software is provided |
| 4 | +// "as is" without express or implied warranty, and with no claim as |
| 5 | +// to its suitability for any purpose. |
| 6 | +#ifndef IMPLICIT_DWA2002326_HPP |
| 7 | +# define IMPLICIT_DWA2002326_HPP |
| 8 | +# include <boost/python/converter/from_python_data.hpp> |
| 9 | +# include <boost/python/converter/from_python_stage1_data.hpp> |
| 10 | +# include <boost/python/converter/registrations.hpp> |
| 11 | + |
| 12 | +namespace boost { namespace python { namespace converter { |
| 13 | + |
| 14 | +template <class Source, class Target> |
| 15 | +struct implicit |
| 16 | +{ |
| 17 | + static void* convertible(PyObject* obj) |
| 18 | + { |
| 19 | + // Find a converter registration which can produce a Source |
| 20 | + // instance from obj |
| 21 | + return const_cast<rvalue_from_python_registration*>( |
| 22 | + find_chain(obj, rvalue_from_python_chain<Source>::value)); |
| 23 | + } |
| 24 | + |
| 25 | + static void construct(PyObject* obj, rvalue_stage1_data* data) |
| 26 | + { |
| 27 | + // This is the registration we got from the convertible step |
| 28 | + rvalue_from_python_registration const* registration |
| 29 | + = static_cast<rvalue_from_python_registration*>(data->convertible); |
| 30 | + |
| 31 | + // Call the convertible function again |
| 32 | + rvalue_data<Source> intermediate_data(registration->convertible(obj)); |
| 33 | + |
| 34 | + // Use the result to construct the source type if the first |
| 35 | + // converter was an rvalue converter. |
| 36 | + if (registration->construct != 0) |
| 37 | + registration->construct(obj, &intermediate_data.stage1); |
| 38 | + |
| 39 | + void* storage = ((rvalue_base_data<Target>*)data)->storage.bytes; |
| 40 | + new (storage) Target(*(Source*)intermediate_data.storage.bytes); |
| 41 | + |
| 42 | + // record successful construction |
| 43 | + data->convertible = storage; |
| 44 | + |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +}}} // namespace boost::python::converter |
| 49 | + |
| 50 | +#endif // IMPLICIT_DWA2002326_HPP |
0 commit comments