|
| 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 TRANSLATE_EXCEPTION_DWA2002810_HPP |
| 7 | +# define TRANSLATE_EXCEPTION_DWA2002810_HPP |
| 8 | + |
| 9 | +# include <boost/function/function0.hpp> |
| 10 | +# include <boost/call_traits.hpp> |
| 11 | + |
| 12 | +namespace boost { namespace python { namespace detail { |
| 13 | + |
| 14 | +struct exception_handler; |
| 15 | + |
| 16 | +// A ternary function object used to translate C++ exceptions of type |
| 17 | +// ExceptionType into Python exceptions by invoking an object of type |
| 18 | +// Translate. Typically the translate function will be curried with |
| 19 | +// boost::bind(). |
| 20 | +template <class ExceptionType, class Translate> |
| 21 | +struct translate_exception |
| 22 | +{ |
| 23 | + typedef typename add_reference< |
| 24 | + typename add_const<ExceptionType>::type |
| 25 | + >::type exception_cref; |
| 26 | + |
| 27 | + inline bool operator()( |
| 28 | + exception_handler const& handler |
| 29 | + , function0<void> const& f |
| 30 | + , typename call_traits<Translate>::param_type translate) const |
| 31 | + { |
| 32 | + try |
| 33 | + { |
| 34 | + return handler(f); |
| 35 | + } |
| 36 | + catch(exception_cref e) |
| 37 | + { |
| 38 | + translate(e); |
| 39 | + return true; |
| 40 | + } |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | +}}} // namespace boost::python::detail |
| 45 | + |
| 46 | +#endif // TRANSLATE_EXCEPTION_DWA2002810_HPP |
0 commit comments