~ubuntu-branches/ubuntu/hardy/swig1.3/hardy

« back to all changes in this revision

Viewing changes to Examples/test-suite/csharp_exceptions.i

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-12-05 01:16:04 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051205011604-ygx904it6413k3go
Tags: 1.3.27-1ubuntu1
Resynchronise with Debian again, for the new subversion packages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
// %exception tests
 
40
void ThrowByValue()                                     { throw Ex("ThrowByValue"); }
 
41
void ThrowByReference()                                 { throw Ex("ThrowByReference"); }
 
42
// %csnothrowexception
 
43
void NoThrowException()                                 { throw Ex("NoThrowException"); }
 
44
// exception specifications
 
45
void ExceptionSpecificationValue() throw(Ex)            { throw Ex("ExceptionSpecificationValue"); }
 
46
void ExceptionSpecificationReference() throw(Ex&)       { throw Ex("ExceptionSpecificationReference"); }
 
47
void ExceptionSpecificationString() throw(const char *) { throw "ExceptionSpecificationString"; }
 
48
void ExceptionSpecificationInteger() throw(int)         { throw 20; }
 
49
%}
 
50
 
 
51
// test exceptions in the default typemaps
 
52
 
 
53
// null reference exceptions
 
54
%inline %{
 
55
void NullReference(Ex& e) {}
 
56
void NullValue(Ex e) {}
 
57
%}
 
58
 
 
59
// enums
 
60
%inline %{
 
61
enum TestEnum {TestEnumItem};
 
62
void ExceptionSpecificationEnumValue() throw(TestEnum) { throw TestEnumItem; }
 
63
void ExceptionSpecificationEnumReference() throw(TestEnum&) { throw TestEnumItem; }
 
64
%}
 
65
 
 
66
// std::string
 
67
%include "std_string.i"
 
68
%inline %{
 
69
void ExceptionSpecificationStdStringValue() throw(std::string) { throw std::string("ExceptionSpecificationStdStringValue"); }
 
70
void ExceptionSpecificationStdStringReference() throw(const std::string&) { throw std::string("ExceptionSpecificationStdStringReference"); }
 
71
void NullStdStringValue(std::string s) {}
 
72
void NullStdStringReference(std::string &s) {}
 
73
%}
 
74
 
 
75
// Memory leak check (The C++ exception stack was never unwound in the original approach to throwing exceptions from unmanaged code)
 
76
%exception MemoryLeakCheck() {
 
77
  Counter destructor_should_be_called;
 
78
  try {
 
79
    $action
 
80
  } catch(Ex e) {
 
81
    SWIG_exception(SWIG_DivisionByZero, e.what());
 
82
  }
 
83
}
 
84
 
 
85
%inline %{
 
86
struct Counter {
 
87
  static int count;
 
88
  Counter() { count++; }
 
89
  ~Counter() { count--; }
 
90
};
 
91
int Counter::count = 0;
 
92
 
 
93
void MemoryLeakCheck() {
 
94
  throw Ex("testing memory leaks when throwing exceptions");
 
95
}
 
96
%}
 
97
 
 
98
// test exception pending in the csconstruct typemap
 
99
%inline %{
 
100
struct constructor {
 
101
  constructor(std::string s) {}
 
102
  constructor() throw(int) { throw 10; }
 
103
};
 
104
%}
 
105
 
 
106
// test exception pending in the csout typemaps
 
107
%typemap(out, canthrow=1) unsigned short ushorttest %{
 
108
  $result = $1;
 
109
  if ($result == 100) {
 
110
    SWIG_CSharpSetPendingException(SWIG_CSharpIndexOutOfRangeException, "don't like 100");
 
111
    return $null;
 
112
  }
 
113
%}
 
114
%inline %{
 
115
unsigned short ushorttest() { return 100; }
 
116
%}
 
117
 
 
118
// test exception pending in the csvarout/csvarin typemaps and canthrow attribute in unmanaged code typemaps
 
119
%typemap(check, canthrow=1) int numberin, int InOutStruct::staticnumberin %{
 
120
  if ($1 < 0) {
 
121
    SWIG_CSharpSetPendingException(SWIG_CSharpIndexOutOfRangeException, "too small");
 
122
    return $null;
 
123
  }
 
124
%}
 
125
%typemap(out, canthrow=1) int numberout, int InOutStruct::staticnumberout %{
 
126
  $result = $1;
 
127
  if ($result > 10) {
 
128
    SWIG_CSharpSetPendingException(SWIG_CSharpIndexOutOfRangeException, "too big");
 
129
    return $null;
 
130
  }
 
131
%}
 
132
%inline %{
 
133
  int numberin;
 
134
  int numberout;
 
135
  struct InOutStruct {
 
136
    int numberin;
 
137
    int numberout;
 
138
    static int staticnumberin;
 
139
    static int staticnumberout;
 
140
  };
 
141
  int InOutStruct::staticnumberin;
 
142
  int InOutStruct::staticnumberout;
 
143
%}
 
144
 
 
145
// test SWIG_exception macro - it must return from unmanaged code without executing any further unmanaged code
 
146
%typemap(check, canthrow=1) int macrotest {
 
147
  if ($1 < 0) {
 
148
    SWIG_exception(SWIG_IndexError, "testing SWIG_exception macro");
 
149
  }
 
150
}
 
151
%inline %{
 
152
  bool exception_macro_run_flag = false;
 
153
  void exceptionmacrotest(int macrotest) {
 
154
    exception_macro_run_flag = true;
 
155
  }
 
156
%}
 
157
 
 
158
// test all the types of exceptions
 
159
%typemap(check, canthrow=1) UnmanagedExceptions {
 
160
  switch($1) {
 
161
    case UnmanagedApplicationException:          SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException,        "msg"); return $null; break;
 
162
    case UnmanagedArithmeticException:           SWIG_CSharpSetPendingException(SWIG_CSharpArithmeticException,         "msg"); return $null; break;
 
163
    case UnmanagedDivideByZeroException:         SWIG_CSharpSetPendingException(SWIG_CSharpDivideByZeroException,       "msg"); return $null; break;
 
164
    case UnmanagedIndexOutOfRangeException:      SWIG_CSharpSetPendingException(SWIG_CSharpIndexOutOfRangeException,    "msg"); return $null; break;
 
165
    case UnmanagedInvalidOperationException:     SWIG_CSharpSetPendingException(SWIG_CSharpInvalidOperationException,   "msg"); return $null; break;
 
166
    case UnmanagedIOException:                   SWIG_CSharpSetPendingException(SWIG_CSharpIOException,                 "msg"); return $null; break;
 
167
    case UnmanagedNullReferenceException:        SWIG_CSharpSetPendingException(SWIG_CSharpNullReferenceException,      "msg"); return $null; break;
 
168
    case UnmanagedOutOfMemoryException:          SWIG_CSharpSetPendingException(SWIG_CSharpOutOfMemoryException,        "msg"); return $null; break;
 
169
    case UnmanagedOverflowException:             SWIG_CSharpSetPendingException(SWIG_CSharpOverflowException,           "msg"); return $null; break;
 
170
    case UnmanagedSystemException:               SWIG_CSharpSetPendingException(SWIG_CSharpSystemException,             "msg"); return $null; break;
 
171
    case UnmanagedArgumentException:             SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentException,           "msg", "parm"); return $null; break;
 
172
    case UnmanagedArgumentNullException:         SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException,       "msg", "parm"); return $null; break;
 
173
    case UnmanagedArgumentOutOfRangeException:   SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, "msg", "parm"); return $null; break;
 
174
  }
 
175
}
 
176
%inline %{
 
177
enum UnmanagedExceptions {
 
178
  UnmanagedApplicationException,
 
179
  UnmanagedArithmeticException,
 
180
  UnmanagedDivideByZeroException,
 
181
  UnmanagedIndexOutOfRangeException,
 
182
  UnmanagedInvalidOperationException,
 
183
  UnmanagedIOException,
 
184
  UnmanagedNullReferenceException,
 
185
  UnmanagedOutOfMemoryException,
 
186
  UnmanagedOverflowException,
 
187
  UnmanagedSystemException,
 
188
  UnmanagedArgumentException,
 
189
  UnmanagedArgumentNullException,
 
190
  UnmanagedArgumentOutOfRangeException,
 
191
};
 
192
 
 
193
void check_exception(UnmanagedExceptions e) {
 
194
}
 
195
%}
 
196
 
 
197
// exceptions in multiple threads test
 
198
%exception ThrowsClass::ThrowException(long long input) {
 
199
  try {
 
200
    $action
 
201
  } catch (long long d) {
 
202
    char message[64];
 
203
    sprintf(message, "caught:%lld", d);
 
204
    SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, message, "input");
 
205
  }
 
206
}
 
207
%inline %{
 
208
struct ThrowsClass {
 
209
  double dub;
 
210
  ThrowsClass(double d) : dub(d) {}
 
211
  long long ThrowException(long long input) {
 
212
    throw input;
 
213
    return input;
 
214
  }
 
215
};
 
216
%}