PageRenderTime 8ms CodeModel.GetById 4ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/csharp_exceptions.i

#
Swig | 241 lines | 216 code | 25 blank | 0 comment | 0 complexity | 3f9b6f2adf76b70c4e807876c3f25da8 MD5 | raw file
  1%module csharp_exceptions
  2
  3%include <exception.i>
  4
  5%inline %{
  6  class Ex {
  7    const char *message;
  8  public:
  9    Ex(const char *msg) : message(msg) {}
 10    const char *what() { return message; }
 11  };
 12%}
 13
 14%exception ThrowByValue() {
 15  try {
 16    $action
 17  } catch(Ex e) {
 18    SWIG_exception(SWIG_DivisionByZero, e.what());
 19  }
 20}
 21
 22%exception ThrowByReference() {
 23  try {
 24    $action
 25  } catch(Ex &e) {
 26    SWIG_exception(SWIG_DivisionByZero, e.what());
 27  }
 28}
 29
 30%csnothrowexception NoThrowException() {
 31  try {
 32    $action
 33  } catch(Ex) {
 34    // swallowed
 35  }
 36}
 37
 38%inline %{
 39
 40#if defined(_MSC_VER)
 41  #pragma warning(disable: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
 42#endif
 43
 44// %exception tests
 45void ThrowByValue()                                     { throw Ex("ThrowByValue"); }
 46void ThrowByReference()                                 { throw Ex("ThrowByReference"); }
 47// %csnothrowexception
 48void NoThrowException()                                 { throw Ex("NoThrowException"); }
 49// exception specifications
 50void ExceptionSpecificationValue() throw(Ex)            { throw Ex("ExceptionSpecificationValue"); }
 51void ExceptionSpecificationReference() throw(Ex&)       { throw Ex("ExceptionSpecificationReference"); }
 52void ExceptionSpecificationString() throw(const char *) { throw "ExceptionSpecificationString"; }
 53void ExceptionSpecificationInteger() throw(int)         { throw 20; }
 54%}
 55
 56// test exceptions in the default typemaps
 57
 58// null reference exceptions
 59%inline %{
 60void NullReference(Ex& e) {}
 61void NullValue(Ex e) {}
 62%}
 63
 64// enums
 65%inline %{
 66enum TestEnum {TestEnumItem};
 67void ExceptionSpecificationEnumValue() throw(TestEnum) { throw TestEnumItem; }
 68void ExceptionSpecificationEnumReference() throw(TestEnum&) { throw TestEnumItem; }
 69%}
 70
 71// std::string
 72%include <std_string.i>
 73%inline %{
 74void ExceptionSpecificationStdStringValue() throw(std::string) { throw std::string("ExceptionSpecificationStdStringValue"); }
 75void ExceptionSpecificationStdStringReference() throw(const std::string&) { throw std::string("ExceptionSpecificationStdStringReference"); }
 76void NullStdStringValue(std::string s) {}
 77void NullStdStringReference(std::string &s) {}
 78%}
 79
 80// Memory leak check (The C++ exception stack was never unwound in the original approach to throwing exceptions from unmanaged code)
 81%exception MemoryLeakCheck() {
 82  Counter destructor_should_be_called;
 83  try {
 84    $action
 85  } catch(Ex e) {
 86    SWIG_exception(SWIG_DivisionByZero, e.what());
 87  }
 88}
 89
 90%inline %{
 91struct Counter {
 92  static int count;
 93  Counter() { count++; }
 94  ~Counter() { count--; }
 95};
 96int Counter::count = 0;
 97
 98void MemoryLeakCheck() {
 99  throw Ex("testing memory leaks when throwing exceptions");
100}
101%}
102
103// test exception pending in the csconstruct typemap
104%inline %{
105struct constructor {
106  constructor(std::string s) {}
107  constructor() throw(int) { throw 10; }
108};
109
110#if defined(_MSC_VER)
111  #pragma warning(default: 4290) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
112#endif
113%}
114
115// test exception pending in the csout typemaps
116%typemap(out, canthrow=1) unsigned short ushorttest %{
117  $result = $1;
118  if ($result == 100) {
119    SWIG_CSharpSetPendingException(SWIG_CSharpIndexOutOfRangeException, "don't like 100");
120    return $null;
121  }
122%}
123%inline %{
124unsigned short ushorttest() { return 100; }
125%}
126
127// test exception pending in the csvarout/csvarin typemaps and canthrow attribute in unmanaged code typemaps
128%typemap(check, canthrow=1) int numberin, int InOutStruct::staticnumberin %{
129  if ($1 < 0) {
130    SWIG_CSharpSetPendingException(SWIG_CSharpIndexOutOfRangeException, "too small");
131    return $null;
132  }
133%}
134%typemap(out, canthrow=1) int numberout, int InOutStruct::staticnumberout %{
135  $result = $1;
136  if ($result > 10) {
137    SWIG_CSharpSetPendingException(SWIG_CSharpIndexOutOfRangeException, "too big");
138    return $null;
139  }
140%}
141%inline %{
142  int numberin;
143  int numberout;
144  struct InOutStruct {
145    int numberin;
146    int numberout;
147    static int staticnumberin;
148    static int staticnumberout;
149  };
150  int InOutStruct::staticnumberin;
151  int InOutStruct::staticnumberout;
152%}
153
154// test SWIG_exception macro - it must return from unmanaged code without executing any further unmanaged code
155%typemap(check, canthrow=1) int macrotest {
156  if ($1 < 0) {
157    SWIG_exception(SWIG_IndexError, "testing SWIG_exception macro");
158  }
159}
160%inline %{
161  bool exception_macro_run_flag = false;
162  void exceptionmacrotest(int macrotest) {
163    exception_macro_run_flag = true;
164  }
165%}
166
167// test all the types of exceptions
168%typemap(check, canthrow=1) UnmanagedExceptions {
169  switch($1) {
170    case UnmanagedApplicationException:          SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException,        "msg"); return $null; break;
171    case UnmanagedArithmeticException:           SWIG_CSharpSetPendingException(SWIG_CSharpArithmeticException,         "msg"); return $null; break;
172    case UnmanagedDivideByZeroException:         SWIG_CSharpSetPendingException(SWIG_CSharpDivideByZeroException,       "msg"); return $null; break;
173    case UnmanagedIndexOutOfRangeException:      SWIG_CSharpSetPendingException(SWIG_CSharpIndexOutOfRangeException,    "msg"); return $null; break;
174    case UnmanagedInvalidCastException:          SWIG_CSharpSetPendingException(SWIG_CSharpInvalidCastException,        "msg"); return $null; break;
175    case UnmanagedInvalidOperationException:     SWIG_CSharpSetPendingException(SWIG_CSharpInvalidOperationException,   "msg"); return $null; break;
176    case UnmanagedIOException:                   SWIG_CSharpSetPendingException(SWIG_CSharpIOException,                 "msg"); return $null; break;
177    case UnmanagedNullReferenceException:        SWIG_CSharpSetPendingException(SWIG_CSharpNullReferenceException,      "msg"); return $null; break;
178    case UnmanagedOutOfMemoryException:          SWIG_CSharpSetPendingException(SWIG_CSharpOutOfMemoryException,        "msg"); return $null; break;
179    case UnmanagedOverflowException:             SWIG_CSharpSetPendingException(SWIG_CSharpOverflowException,           "msg"); return $null; break;
180    case UnmanagedSystemException:               SWIG_CSharpSetPendingException(SWIG_CSharpSystemException,             "msg"); return $null; break;
181    case UnmanagedArgumentException:             SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentException,           "msg", "parm"); return $null; break;
182    case UnmanagedArgumentNullException:         SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException,       "msg", "parm"); return $null; break;
183    case UnmanagedArgumentOutOfRangeException:   SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, "msg", "parm"); return $null; break;
184  }
185}
186%inline %{
187enum UnmanagedExceptions {
188  UnmanagedApplicationException,
189  UnmanagedArithmeticException,
190  UnmanagedDivideByZeroException,
191  UnmanagedIndexOutOfRangeException,
192  UnmanagedInvalidCastException,
193  UnmanagedInvalidOperationException,
194  UnmanagedIOException,
195  UnmanagedNullReferenceException,
196  UnmanagedOutOfMemoryException,
197  UnmanagedOverflowException,
198  UnmanagedSystemException,
199  UnmanagedArgumentException,
200  UnmanagedArgumentNullException,
201  UnmanagedArgumentOutOfRangeException,
202};
203
204void check_exception(UnmanagedExceptions e) {
205}
206%}
207
208// exceptions in multiple threads test
209%exception ThrowsClass::ThrowException(long long input) {
210  try {
211    $action
212  } catch (long long d) {
213    char message[64];
214    sprintf(message, "caught:%lld", d);
215    SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, message, "input");
216  }
217}
218%inline %{
219struct ThrowsClass {
220  double dub;
221  ThrowsClass(double d) : dub(d) {}
222  long long ThrowException(long long input) {
223    throw input;
224    return input;
225  }
226};
227%}
228
229// test inner exceptions
230%exception InnerExceptionTest() {
231  try {
232    $action
233  } catch(Ex &e) {
234    SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, e.what());
235    SWIG_CSharpSetPendingException(SWIG_CSharpInvalidOperationException, "My OuterException message");
236  }
237}
238
239%inline %{
240void InnerExceptionTest() { throw Ex("My InnerException message"); }
241%}