From 2dc87d7485876d8a7a45d8136014695863e186de Mon Sep 17 00:00:00 2001 From: Daniel Vollmer Date: Sat, 17 Sep 2016 10:26:54 +0200 Subject: [PATCH] Add std::bad_cast to std_except.i This exception occurs when dynamic_cast fails. Fixes #783. --- Examples/test-suite/csharp/li_std_except_runme.cs | 1 + Examples/test-suite/li_std_except.i | 1 + Lib/csharp/std_except.i | 1 + Lib/d/std_except.i | 1 + Lib/go/std_except.i | 1 + Lib/java/std_except.i | 1 + Lib/lua/std_except.i | 1 + Lib/std/std_except.i | 4 ++++ Lib/std_except.i | 1 + 9 files changed, 12 insertions(+) diff --git a/Examples/test-suite/csharp/li_std_except_runme.cs b/Examples/test-suite/csharp/li_std_except_runme.cs index 86ab44892..c5ff26b6c 100644 --- a/Examples/test-suite/csharp/li_std_except_runme.cs +++ b/Examples/test-suite/csharp/li_std_except_runme.cs @@ -7,6 +7,7 @@ public class li_std_except_runme { public static void Main() { Test test = new Test(); + try { test.throw_bad_cast(); throw new Exception("throw_bad_cast failed"); } catch (InvalidCastException) {} try { test.throw_bad_exception(); throw new Exception("throw_bad_exception failed"); } catch (ApplicationException) {} try { test.throw_domain_error(); throw new Exception("throw_domain_error failed"); } catch (ApplicationException) {} try { test.throw_exception(); throw new Exception("throw_exception failed"); } catch (ApplicationException) {} diff --git a/Examples/test-suite/li_std_except.i b/Examples/test-suite/li_std_except.i index fc886dca7..b79d36bc1 100644 --- a/Examples/test-suite/li_std_except.i +++ b/Examples/test-suite/li_std_except.i @@ -24,6 +24,7 @@ int foo3() throw(E1) { return 0; } int foo4() throw(E2) { return 0; } // all the STL exceptions... + void throw_bad_cast() throw(std::bad_cast) { throw std::bad_cast(); } void throw_bad_exception() throw(std::bad_exception) { throw std::bad_exception(); } void throw_domain_error() throw(std::domain_error) { throw std::domain_error("oops"); } void throw_exception() throw(std::exception) { throw std::exception(); } diff --git a/Lib/csharp/std_except.i b/Lib/csharp/std_except.i index 27eb84bc2..1f6b2c483 100644 --- a/Lib/csharp/std_except.i +++ b/Lib/csharp/std_except.i @@ -16,6 +16,7 @@ namespace std struct exception {}; } +%typemap(throws, canthrow=1) std::bad_cast "SWIG_CSharpSetPendingException(SWIG_CSharpInvalidCastException, $1.what());\n return $null;" %typemap(throws, canthrow=1) std::bad_exception "SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, $1.what());\n return $null;" %typemap(throws, canthrow=1) std::domain_error "SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, $1.what());\n return $null;" %typemap(throws, canthrow=1) std::exception "SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, $1.what());\n return $null;" diff --git a/Lib/d/std_except.i b/Lib/d/std_except.i index 2b557e5fc..4a0ce6d69 100644 --- a/Lib/d/std_except.i +++ b/Lib/d/std_except.i @@ -16,6 +16,7 @@ namespace std struct exception {}; } +%typemap(throws, canthrow=1) std::bad_cast "SWIG_DSetPendingException(SWIG_DException, $1.what());\n return $null;" %typemap(throws, canthrow=1) std::bad_exception "SWIG_DSetPendingException(SWIG_DException, $1.what());\n return $null;" %typemap(throws, canthrow=1) std::domain_error "SWIG_DSetPendingException(SWIG_DException, $1.what());\n return $null;" %typemap(throws, canthrow=1) std::exception "SWIG_DSetPendingException(SWIG_DException, $1.what());\n return $null;" diff --git a/Lib/go/std_except.i b/Lib/go/std_except.i index 789a335f7..9439120e6 100644 --- a/Lib/go/std_except.i +++ b/Lib/go/std_except.i @@ -16,6 +16,7 @@ namespace std struct exception {}; } +%typemap(throws) std::bad_cast %{_swig_gopanic($1.what());%} %typemap(throws) std::bad_exception %{_swig_gopanic($1.what());%} %typemap(throws) std::domain_error %{_swig_gopanic($1.what());%} %typemap(throws) std::exception %{_swig_gopanic($1.what());%} diff --git a/Lib/java/std_except.i b/Lib/java/std_except.i index 9e23d50e6..b89ee617f 100644 --- a/Lib/java/std_except.i +++ b/Lib/java/std_except.i @@ -16,6 +16,7 @@ namespace std struct exception {}; } +%typemap(throws) std::bad_cast "SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, $1.what());\n return $null;" %typemap(throws) std::bad_exception "SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, $1.what());\n return $null;" %typemap(throws) std::domain_error "SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, $1.what());\n return $null;" %typemap(throws) std::exception "SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, $1.what());\n return $null;" diff --git a/Lib/lua/std_except.i b/Lib/lua/std_except.i index 160828723..fd63663af 100644 --- a/Lib/lua/std_except.i +++ b/Lib/lua/std_except.i @@ -27,6 +27,7 @@ namespace std // normally objects which are thrown are returned to the interpreter as errors // (which potentially may have problems if they are not copied) // therefore all classes based upon std::exception are converted to their strings & returned as errors +%typemap(throws) std::bad_cast "SWIG_exception(SWIG_TypeError, $1.what());" %typemap(throws) std::bad_exception "SWIG_exception(SWIG_RuntimeError, $1.what());" %typemap(throws) std::domain_error "SWIG_exception(SWIG_ValueError, $1.what());" %typemap(throws) std::exception "SWIG_exception(SWIG_SystemError, $1.what());" diff --git a/Lib/std/std_except.i b/Lib/std/std_except.i index 75b8d0fd6..c8820c2c5 100644 --- a/Lib/std/std_except.i +++ b/Lib/std/std_except.i @@ -15,6 +15,10 @@ namespace std { virtual const char* what() const throw(); }; + struct bad_cast : exception + { + }; + struct bad_exception : exception { }; diff --git a/Lib/std_except.i b/Lib/std_except.i index a4a7a85ac..d070d1b9c 100644 --- a/Lib/std_except.i +++ b/Lib/std_except.i @@ -40,6 +40,7 @@ %enddef namespace std { + %std_exception_map(bad_cast, SWIG_TypeError); %std_exception_map(bad_exception, SWIG_SystemError); %std_exception_map(domain_error, SWIG_ValueError); %std_exception_map(exception, SWIG_SystemError);