exception_translator.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #ifndef BOOST_STATECHART_EXCEPTION_TRANSLATOR_HPP_INCLUDED
  2. #define BOOST_STATECHART_EXCEPTION_TRANSLATOR_HPP_INCLUDED
  3. //////////////////////////////////////////////////////////////////////////////
  4. // Copyright 2002-2006 Andreas Huber Doenni
  5. // Distributed under the Boost Software License, Version 1.0. (See accompany-
  6. // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //////////////////////////////////////////////////////////////////////////////
  8. #include <boost/statechart/event.hpp>
  9. #include <boost/statechart/result.hpp>
  10. namespace boost
  11. {
  12. namespace statechart
  13. {
  14. //////////////////////////////////////////////////////////////////////////////
  15. class exception_thrown : public event< exception_thrown > {};
  16. //////////////////////////////////////////////////////////////////////////////
  17. template< class ExceptionEvent = exception_thrown >
  18. class exception_translator
  19. {
  20. public:
  21. //////////////////////////////////////////////////////////////////////////
  22. // The following declarations should be private.
  23. // They are only public because many compilers lack template friends.
  24. //////////////////////////////////////////////////////////////////////////
  25. template< class Action, class ExceptionEventHandler >
  26. result operator()( Action action, ExceptionEventHandler eventHandler )
  27. {
  28. try
  29. {
  30. return action();
  31. }
  32. catch ( ... )
  33. {
  34. return eventHandler( ExceptionEvent() );
  35. }
  36. }
  37. };
  38. } // namespace statechart
  39. } // namespace boost
  40. #endif