std_except.i 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* -----------------------------------------------------------------------------
  2. * std_except.i
  3. *
  4. * SWIG library file with typemaps to handle and throw STD exceptions in a
  5. * language and STL independent way, i.e., the target language doesn't
  6. * require to support STL but only the 'exception.i' mechanism.
  7. *
  8. * These typemaps are used when methods are declared with an STD
  9. * exception specification, such as
  10. *
  11. * size_t at() const throw (std::out_of_range);
  12. *
  13. * The typemaps here are based on the language independent
  14. * 'exception.i' library. If that is working in your target language,
  15. * this file will work.
  16. *
  17. * If the target language doesn't implement a robust 'exception.i'
  18. * mechanism, or you prefer other ways to map the STD exceptions, write
  19. * a new std_except.i file in the target library directory.
  20. * ----------------------------------------------------------------------------- */
  21. #if defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGGUILE) || defined(SWIGUTL) || defined(SWIGD)
  22. #error "This version of std_except.i should not be used"
  23. #endif
  24. %{
  25. #include <stdexcept>
  26. %}
  27. %include <exception.i>
  28. %define %std_exception_map(Exception, Code)
  29. %typemap(throws,noblock=1) Exception {
  30. SWIG_exception(Code, $1.what());
  31. }
  32. %ignore Exception;
  33. struct Exception {
  34. };
  35. %enddef
  36. namespace std {
  37. %std_exception_map(bad_exception, SWIG_SystemError);
  38. %std_exception_map(domain_error, SWIG_ValueError);
  39. %std_exception_map(exception, SWIG_SystemError);
  40. %std_exception_map(invalid_argument, SWIG_ValueError);
  41. %std_exception_map(length_error, SWIG_IndexError);
  42. %std_exception_map(logic_error, SWIG_RuntimeError);
  43. %std_exception_map(out_of_range, SWIG_IndexError);
  44. %std_exception_map(overflow_error, SWIG_OverflowError);
  45. %std_exception_map(range_error, SWIG_OverflowError);
  46. %std_exception_map(runtime_error, SWIG_RuntimeError);
  47. %std_exception_map(underflow_error, SWIG_OverflowError);
  48. }