translate_exception.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 TRANSLATE_EXCEPTION_TDS20091020_HPP
  6. # define TRANSLATE_EXCEPTION_TDS20091020_HPP
  7. # include <boost/python/detail/exception_handler.hpp>
  8. # include <boost/python/detail/type_traits.hpp>
  9. # include <boost/call_traits.hpp>
  10. # include <boost/function/function0.hpp>
  11. namespace boost { namespace python { namespace detail {
  12. // A ternary function object used to translate C++ exceptions of type
  13. // ExceptionType into Python exceptions by invoking an object of type
  14. // Translate. Typically the translate function will be curried with
  15. // boost::bind().
  16. template <class ExceptionType, class Translate>
  17. struct translate_exception
  18. {
  19. // workaround for broken gcc that ships with SuSE 9.0 and SuSE 9.1
  20. # if defined(__linux__) && defined(__GNUC__) \
  21. && BOOST_WORKAROUND(__GNUC__, == 3) \
  22. && BOOST_WORKAROUND(__GNUC_MINOR__, == 3) \
  23. && (BOOST_WORKAROUND(__GNUC_PATCHLEVEL__, == 1) \
  24. || BOOST_WORKAROUND(__GNUC_PATCHLEVEL__, == 3))
  25. typedef typename remove_reference<
  26. typename add_const<ExceptionType>::type
  27. >::type exception_non_ref;
  28. # else
  29. typedef typename add_lvalue_reference<
  30. typename add_const<ExceptionType>::type
  31. >::type exception_cref;
  32. # endif
  33. inline bool operator()(
  34. exception_handler const& handler
  35. , function0<void> const& f
  36. , typename call_traits<Translate>::param_type translate) const
  37. {
  38. try
  39. {
  40. return handler(f);
  41. }
  42. // workaround for broken gcc that ships with SuSE 9.0 and SuSE 9.1
  43. # if defined(__linux__) && defined(__GNUC__) \
  44. && BOOST_WORKAROUND(__GNUC__, == 3) \
  45. && BOOST_WORKAROUND(__GNUC_MINOR__, == 3) \
  46. && (BOOST_WORKAROUND(__GNUC_PATCHLEVEL__, == 1) \
  47. || BOOST_WORKAROUND(__GNUC_PATCHLEVEL__, == 3))
  48. catch(exception_non_ref& e)
  49. # else
  50. catch(exception_cref e)
  51. # endif
  52. {
  53. translate(e);
  54. return true;
  55. }
  56. }
  57. };
  58. }}} // namespace boost::python::detail
  59. #endif // TRANSLATE_EXCEPTION_DWA2002810_HPP