diff options
author | Swati Sharma <itawswati@gmail.com> | 2012-08-10 18:04:06 +0000 |
---|---|---|
committer | Swati Sharma <itawswati@gmail.com> | 2012-08-10 18:04:06 +0000 |
commit | 6caba80c61ed000b1eee59a217c434bafd7feef8 (patch) | |
tree | 7e1f4673551062b08527a133300951dd585f18c9 | |
parent | 7591aa904b77647b16954d38f39034bc5da6a50d (diff) | |
download | swig-6caba80c61ed000b1eee59a217c434bafd7feef8.tar.gz |
Added Exception Handling support for Objective-C.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2012-objc@13588 626c5289-ae23-0410-ae9c-e8d60b6d4f22
-rw-r--r-- | Lib/exception.i | 40 | ||||
-rw-r--r-- | Lib/objc/objc.swg | 27 | ||||
-rwxr-xr-x | Lib/objc/objcruntime.swg | 42 | ||||
-rw-r--r-- | Lib/objc/std_except.i | 30 |
4 files changed, 139 insertions, 0 deletions
diff --git a/Lib/exception.i b/Lib/exception.i index 8d8df33dd..539d2d536 100644 --- a/Lib/exception.i +++ b/Lib/exception.i @@ -200,6 +200,46 @@ SWIGINTERN void SWIG_CSharpException(int code, const char *msg) { { SWIG_CSharpException(code, msg); return $null; } #endif // SWIGCSHARP +#ifdef SWIGOBJECTIVEC +%{ +SWIGINTERN void SWIG_ObjcException(int code, const char *msg) { + SWIG_ObjcExceptionCodes exception_code = SWIG_ObjcUnknownError; + switch(code) { + case SWIG_MemoryError: + exception_code = SWIG_ObjcOutOfMemoryError; + break; + case SWIG_IOError: + exception_code = SWIG_ObjcIOException; + break; + case SWIG_SystemError: + case SWIG_RuntimeError: + exception_code = SWIG_ObjcRuntimeException; + break; + case SWIG_OverflowError: + case SWIG_IndexError: + exception_code = SWIG_ObjcIndexOutOfBoundsException; + break; + case SWIG_DivisionByZero: + exception_code = SWIG_ObjcArithmeticException; + break; + case SWIG_SyntaxError: + case SWIG_ValueError: + case SWIG_TypeError: + exception_code = SWIG_ObjcIllegalArgumentException; + break; + case SWIG_UnknownError: + default: + exception_code = SWIG_ObjcUnknownError; + break; + } + SWIG_ObjcThrowException(exception_code, msg); +} +%} + +#define SWIG_exception(code, msg)\ +{ SWIG_ObjcException(code, msg); return $null; } +#endif // SWIGOBJECTIVEC + #ifdef SWIGLUA %{ diff --git a/Lib/objc/objc.swg b/Lib/objc/objc.swg index efba52e45..f244f39b2 100644 --- a/Lib/objc/objc.swg +++ b/Lib/objc/objc.swg @@ -593,6 +593,33 @@ /* const pointers */ %apply SWIGTYPE * { SWIGTYPE *const } +/* Exception handling */ + +%typemap(throws) int, + long, + short, + unsigned int, + unsigned long, + unsigned short +%{ char error_msg[256]; + sprintf(error_msg, "C++ $1_type exception thrown, value: %d", $1); + SWIG_ObjcThrowException(SWIG_ObjcRuntimeException, error_msg); + return $null; %} + +%typemap(throws) SWIGTYPE, SWIGTYPE &, SWIGTYPE *, SWIGTYPE [], SWIGTYPE [ANY] +%{ (void)$1; + SWIG_ObjcThrowException(SWIG_ObjcRuntimeException, "C++ $1_type exception thrown"); + return $null; %} + +%typemap(throws) char * +%{ SWIG_ObjcThrowException(SWIG_ObjcRuntimeException, $1); + return $null; %} + + +#define %objcexception(exceptionclasses) %feature("except",throws=exceptionclasses) +#define %noobjcexception %feature("except","0",throws="") +#define %clearobjcexception %feature("except","",throws="") + /* Additional typemaps */ // Typemaps containing ObjectiveC code used when generating ObjectiveC proxy classes. %include <objcproxycode.swg> diff --git a/Lib/objc/objcruntime.swg b/Lib/objc/objcruntime.swg index a2e8fb385..91feb2d70 100755 --- a/Lib/objc/objcruntime.swg +++ b/Lib/objc/objcruntime.swg @@ -15,3 +15,45 @@ #define SWIG_contract_assert(nullreturn, expr, msg) if (!(expr)) { /*throw exception*/ return nullreturn; } else %} + +%insert(runtime) %{ +/* Support for throwing Objc exceptions */ +typedef enum { + SWIG_ObjcOutOfMemoryError = 1, + SWIG_ObjcIOException, + SWIG_ObjcRuntimeException, + SWIG_ObjcIndexOutOfBoundsException, + SWIG_ObjcArithmeticException, + SWIG_ObjcIllegalArgumentException, + SWIG_ObjcNullPointerException, + SWIG_ObjcDirectorPureVirtual, + SWIG_ObjcUnknownError +} SWIG_ObjcExceptionCodes; + +typedef struct { + SWIG_ObjcExceptionCodes code; + const char *Objc_exception; +} SWIG_ObjcExceptions_t; +%} + +%insert(runtime) { +static void SWIGUNUSED SWIG_ObjcThrowException(SWIG_ObjcExceptionCodes code, const char *msg) { + static const SWIG_ObjcExceptions_t Objc_exceptions[] = { + { SWIG_ObjcOutOfMemoryError, "OutOfMemoryError" }, + { SWIG_ObjcIOException, "IOException" }, + { SWIG_ObjcRuntimeException, "RuntimeException" }, + { SWIG_ObjcIndexOutOfBoundsException, "IndexOutOfBoundsException" }, + { SWIG_ObjcArithmeticException, "ArithmeticException" }, + { SWIG_ObjcIllegalArgumentException, "IllegalArgumentException" }, + { SWIG_ObjcNullPointerException, "NullPointerException" }, + { SWIG_ObjcDirectorPureVirtual, "RuntimeException" }, + { SWIG_ObjcUnknownError, "UnknownError" }, + { (SWIG_ObjcExceptionCodes)0, "UnknownError" } + }; + const SWIG_ObjcExceptions_t *except_ptr = Objc_exceptions; + while (except_ptr->code != code && except_ptr->code) + except_ptr++; +} +} + + diff --git a/Lib/objc/std_except.i b/Lib/objc/std_except.i new file mode 100644 index 000000000..54bfe4794 --- /dev/null +++ b/Lib/objc/std_except.i @@ -0,0 +1,30 @@ +/* ----------------------------------------------------------------------------- + * std_except.i + * + * Typemaps used by the STL wrappers that throw exceptions. + * These typemaps are used when methods are declared with an STL exception specification, such as + * size_t at() const throw (std::out_of_range); + * ----------------------------------------------------------------------------- */ + +%{ +#include <stdexcept> +%} + +namespace std +{ + %ignore exception; + struct exception {}; +} + +%typemap(throws) std::bad_exception "SWIG_ObjcThrowException(SWIG_ObjcRuntimeException, $1.what());\n return $null;" +%typemap(throws) std::domain_error "SWIG_ObjcThrowException(SWIG_ObjcRuntimeException, $1.what());\n return $null;" +%typemap(throws) std::exception "SWIG_ObjcThrowException(SWIG_ObjcRuntimeException, $1.what());\n return $null;" +%typemap(throws) std::invalid_argument "SWIG_ObjcThrowException(SWIG_ObjcIllegalArgumentException, $1.what());\n return $null;" +%typemap(throws) std::length_error "SWIG_ObjcThrowException(SWIG_ObjcIndexOutOfBoundsException, $1.what());\n return $null;" +%typemap(throws) std::logic_error "SWIG_ObjcThrowException(SWIG_ObjcRuntimeException, $1.what());\n return $null;" +%typemap(throws) std::out_of_range "SWIG_ObjcThrowException(SWIG_ObjcIndexOutOfBoundsException, $1.what());\n return $null;" +%typemap(throws) std::overflow_error "SWIG_ObjcThrowException(SWIG_ObjcArithmeticException, $1.what());\n return $null;" +%typemap(throws) std::range_error "SWIG_ObjcThrowException(SWIG_ObjcIndexOutOfBoundsException, $1.what());\n return $null;" +%typemap(throws) std::runtime_error "SWIG_ObjcThrowException(SWIG_ObjcRuntimeException, $1.what());\n return $null;" +%typemap(throws) std::underflow_error "SWIG_ObjcThrowException(SWIG_ObjcArithmeticException, $1.what());\n return $null;" + |