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