PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/typemaps/exception.swg

#
Unknown | 86 lines | 72 code | 14 blank | 0 comment | 0 complexity | ebbb9783c142432eb2e731fb5ebd6ad2 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * exceptions.swg
  3. *
  4. * This SWIG library file provides language independent exception handling
  5. * ----------------------------------------------------------------------------- */
  6. %include <typemaps/swigmacros.swg>
  7. /* macros for error manipulation */
  8. #define %nullref_fmt() "invalid null reference "
  9. #define %varfail_fmt(_type,_name) "in variable '"`_name`"' of type '"`_type`"'"
  10. #ifndef %argfail_fmt
  11. #define %argfail_fmt(_type,_name,_argn) "in method '" `_name` "', argument " `_argn`" of type '" `_type`"'"
  12. #endif
  13. #define %outfail_fmt(_type) "in output value of type '"_type"'"
  14. #ifndef %argnullref_fmt
  15. #define %argnullref_fmt(_type,_name,_argn) %nullref_fmt() %argfail_fmt(_type, _name, _argn)
  16. #endif
  17. #define %varnullref_fmt(_type,_name) %nullref_fmt() %varfail_fmt(_type, _name)
  18. #define %outnullref_fmt(_type) %nullref_fmt() %outfail_fmt(_type)
  19. /* setting an error */
  20. #define %error(code,msg...) SWIG_Error(code, msg)
  21. #define %type_error(msg...) SWIG_Error(SWIG_TypeError, msg)
  22. %insert("runtime") {
  23. %define_as(SWIG_exception_fail(code, msg), %block(%error(code, msg); SWIG_fail))
  24. %define_as(SWIG_contract_assert(expr, msg), if (!(expr)) { %error(SWIG_RuntimeError, msg); SWIG_fail; } else)
  25. }
  26. #ifdef __cplusplus
  27. /*
  28. You can use the SWIG_CATCH_STDEXCEPT macro with the %exception
  29. directive as follows:
  30. %exception {
  31. try {
  32. $action
  33. }
  34. catch (my_except& e) {
  35. ...
  36. }
  37. SWIG_CATCH_STDEXCEPT // catch std::exception
  38. catch (...) {
  39. SWIG_exception_fail(SWIG_UnknownError, "Unknown exception");
  40. }
  41. }
  42. */
  43. %{
  44. #include <stdexcept>
  45. %}
  46. %define SWIG_CATCH_STDEXCEPT
  47. /* catching std::exception */
  48. catch (std::invalid_argument& e) {
  49. SWIG_exception_fail(SWIG_ValueError, e.what() );
  50. } catch (std::domain_error& e) {
  51. SWIG_exception_fail(SWIG_ValueError, e.what() );
  52. } catch (std::overflow_error& e) {
  53. SWIG_exception_fail(SWIG_OverflowError, e.what() );
  54. } catch (std::out_of_range& e) {
  55. SWIG_exception_fail(SWIG_IndexError, e.what() );
  56. } catch (std::length_error& e) {
  57. SWIG_exception_fail(SWIG_IndexError, e.what() );
  58. } catch (std::runtime_error& e) {
  59. SWIG_exception_fail(SWIG_RuntimeError, e.what() );
  60. } catch (std::exception& e) {
  61. SWIG_exception_fail(SWIG_SystemError, e.what() );
  62. }
  63. %enddef
  64. %define SWIG_CATCH_UNKNOWN
  65. catch (std::exception& e) {
  66. SWIG_exception_fail(SWIG_SystemError, e.what() );
  67. }
  68. catch (...) {
  69. SWIG_exception_fail(SWIG_UnknownError, "unknown exception");
  70. }
  71. %enddef
  72. #endif /* __cplusplus */