translate_exception.hpp 2.2 KB

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