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