import source from 1.3.40
[external/swig.git] / Lib / csharp / csharphead.swg
1 /* -----------------------------------------------------------------------------
2  * See the LICENSE file for information on copyright, usage and redistribution
3  * of SWIG, and the README file for authors - http://www.swig.org/release.html.
4  *
5  * csharphead.swg
6  *
7  * Support code for exceptions if the SWIG_CSHARP_NO_EXCEPTION_HELPER is not defined
8  * Support code for strings if the SWIG_CSHARP_NO_STRING_HELPER is not defined
9  * ----------------------------------------------------------------------------- */
10
11 %insert(runtime) %{
12 #include <stdlib.h>
13 #include <string.h>
14 #include <stdio.h>
15 %}
16
17 #if !defined(SWIG_CSHARP_NO_EXCEPTION_HELPER)
18 %insert(runtime) %{
19 /* Support for throwing C# exceptions from C/C++. There are two types: 
20  * Exceptions that take a message and ArgumentExceptions that take a message and a parameter name. */
21 typedef enum {
22   SWIG_CSharpApplicationException,
23   SWIG_CSharpArithmeticException,
24   SWIG_CSharpDivideByZeroException,
25   SWIG_CSharpIndexOutOfRangeException,
26   SWIG_CSharpInvalidCastException,
27   SWIG_CSharpInvalidOperationException,
28   SWIG_CSharpIOException,
29   SWIG_CSharpNullReferenceException,
30   SWIG_CSharpOutOfMemoryException,
31   SWIG_CSharpOverflowException,
32   SWIG_CSharpSystemException
33 } SWIG_CSharpExceptionCodes;
34
35 typedef enum {
36   SWIG_CSharpArgumentException,
37   SWIG_CSharpArgumentNullException,
38   SWIG_CSharpArgumentOutOfRangeException
39 } SWIG_CSharpExceptionArgumentCodes;
40
41 typedef void (SWIGSTDCALL* SWIG_CSharpExceptionCallback_t)(const char *);
42 typedef void (SWIGSTDCALL* SWIG_CSharpExceptionArgumentCallback_t)(const char *, const char *);
43
44 typedef struct {
45   SWIG_CSharpExceptionCodes code;
46   SWIG_CSharpExceptionCallback_t callback;
47 } SWIG_CSharpException_t;
48
49 typedef struct {
50   SWIG_CSharpExceptionArgumentCodes code;
51   SWIG_CSharpExceptionArgumentCallback_t callback;
52 } SWIG_CSharpExceptionArgument_t;
53
54 static SWIG_CSharpException_t SWIG_csharp_exceptions[] = {
55   { SWIG_CSharpApplicationException, NULL },
56   { SWIG_CSharpArithmeticException, NULL },
57   { SWIG_CSharpDivideByZeroException, NULL },
58   { SWIG_CSharpIndexOutOfRangeException, NULL },
59   { SWIG_CSharpInvalidCastException, NULL },
60   { SWIG_CSharpInvalidOperationException, NULL },
61   { SWIG_CSharpIOException, NULL },
62   { SWIG_CSharpNullReferenceException, NULL },
63   { SWIG_CSharpOutOfMemoryException, NULL },
64   { SWIG_CSharpOverflowException, NULL },
65   { SWIG_CSharpSystemException, NULL }
66 };
67
68 static SWIG_CSharpExceptionArgument_t SWIG_csharp_exceptions_argument[] = {
69   { SWIG_CSharpArgumentException, NULL },
70   { SWIG_CSharpArgumentNullException, NULL },
71   { SWIG_CSharpArgumentOutOfRangeException, NULL }
72 };
73
74 static void SWIGUNUSED SWIG_CSharpSetPendingException(SWIG_CSharpExceptionCodes code, const char *msg) {
75   SWIG_CSharpExceptionCallback_t callback = SWIG_csharp_exceptions[SWIG_CSharpApplicationException].callback;
76   if ((size_t)code < sizeof(SWIG_csharp_exceptions)/sizeof(SWIG_CSharpException_t)) {
77     callback = SWIG_csharp_exceptions[code].callback;
78   }
79   callback(msg);
80 }
81
82 static void SWIGUNUSED SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpExceptionArgumentCodes code, const char *msg, const char *param_name) {
83   SWIG_CSharpExceptionArgumentCallback_t callback = SWIG_csharp_exceptions_argument[SWIG_CSharpArgumentException].callback;
84   if ((size_t)code < sizeof(SWIG_csharp_exceptions_argument)/sizeof(SWIG_CSharpExceptionArgument_t)) {
85     callback = SWIG_csharp_exceptions_argument[code].callback;
86   }
87   callback(msg, param_name);
88 }
89 %}
90
91 %insert(runtime) %{
92 #ifdef __cplusplus
93 extern "C" 
94 #endif
95 SWIGEXPORT void SWIGSTDCALL SWIGRegisterExceptionCallbacks_$module(
96                                                 SWIG_CSharpExceptionCallback_t applicationCallback,
97                                                 SWIG_CSharpExceptionCallback_t arithmeticCallback,
98                                                 SWIG_CSharpExceptionCallback_t divideByZeroCallback, 
99                                                 SWIG_CSharpExceptionCallback_t indexOutOfRangeCallback, 
100                                                 SWIG_CSharpExceptionCallback_t invalidCastCallback,
101                                                 SWIG_CSharpExceptionCallback_t invalidOperationCallback,
102                                                 SWIG_CSharpExceptionCallback_t ioCallback,
103                                                 SWIG_CSharpExceptionCallback_t nullReferenceCallback,
104                                                 SWIG_CSharpExceptionCallback_t outOfMemoryCallback, 
105                                                 SWIG_CSharpExceptionCallback_t overflowCallback, 
106                                                 SWIG_CSharpExceptionCallback_t systemCallback) {
107   SWIG_csharp_exceptions[SWIG_CSharpApplicationException].callback = applicationCallback;
108   SWIG_csharp_exceptions[SWIG_CSharpArithmeticException].callback = arithmeticCallback;
109   SWIG_csharp_exceptions[SWIG_CSharpDivideByZeroException].callback = divideByZeroCallback;
110   SWIG_csharp_exceptions[SWIG_CSharpIndexOutOfRangeException].callback = indexOutOfRangeCallback;
111   SWIG_csharp_exceptions[SWIG_CSharpInvalidCastException].callback = invalidCastCallback;
112   SWIG_csharp_exceptions[SWIG_CSharpInvalidOperationException].callback = invalidOperationCallback;
113   SWIG_csharp_exceptions[SWIG_CSharpIOException].callback = ioCallback;
114   SWIG_csharp_exceptions[SWIG_CSharpNullReferenceException].callback = nullReferenceCallback;
115   SWIG_csharp_exceptions[SWIG_CSharpOutOfMemoryException].callback = outOfMemoryCallback;
116   SWIG_csharp_exceptions[SWIG_CSharpOverflowException].callback = overflowCallback;
117   SWIG_csharp_exceptions[SWIG_CSharpSystemException].callback = systemCallback;
118 }
119
120 #ifdef __cplusplus
121 extern "C" 
122 #endif
123 SWIGEXPORT void SWIGSTDCALL SWIGRegisterExceptionArgumentCallbacks_$module(
124                                                 SWIG_CSharpExceptionArgumentCallback_t argumentCallback,
125                                                 SWIG_CSharpExceptionArgumentCallback_t argumentNullCallback,
126                                                 SWIG_CSharpExceptionArgumentCallback_t argumentOutOfRangeCallback) {
127   SWIG_csharp_exceptions_argument[SWIG_CSharpArgumentException].callback = argumentCallback;
128   SWIG_csharp_exceptions_argument[SWIG_CSharpArgumentNullException].callback = argumentNullCallback;
129   SWIG_csharp_exceptions_argument[SWIG_CSharpArgumentOutOfRangeException].callback = argumentOutOfRangeCallback;
130 }
131 %}
132
133 %pragma(csharp) imclasscode=%{
134   protected class SWIGExceptionHelper {
135
136     public delegate void ExceptionDelegate(string message);
137     public delegate void ExceptionArgumentDelegate(string message, string paramName);
138
139     static ExceptionDelegate applicationDelegate = new ExceptionDelegate(SetPendingApplicationException);
140     static ExceptionDelegate arithmeticDelegate = new ExceptionDelegate(SetPendingArithmeticException);
141     static ExceptionDelegate divideByZeroDelegate = new ExceptionDelegate(SetPendingDivideByZeroException);
142     static ExceptionDelegate indexOutOfRangeDelegate = new ExceptionDelegate(SetPendingIndexOutOfRangeException);
143     static ExceptionDelegate invalidCastDelegate = new ExceptionDelegate(SetPendingInvalidCastException);
144     static ExceptionDelegate invalidOperationDelegate = new ExceptionDelegate(SetPendingInvalidOperationException);
145     static ExceptionDelegate ioDelegate = new ExceptionDelegate(SetPendingIOException);
146     static ExceptionDelegate nullReferenceDelegate = new ExceptionDelegate(SetPendingNullReferenceException);
147     static ExceptionDelegate outOfMemoryDelegate = new ExceptionDelegate(SetPendingOutOfMemoryException);
148     static ExceptionDelegate overflowDelegate = new ExceptionDelegate(SetPendingOverflowException);
149     static ExceptionDelegate systemDelegate = new ExceptionDelegate(SetPendingSystemException);
150
151     static ExceptionArgumentDelegate argumentDelegate = new ExceptionArgumentDelegate(SetPendingArgumentException);
152     static ExceptionArgumentDelegate argumentNullDelegate = new ExceptionArgumentDelegate(SetPendingArgumentNullException);
153     static ExceptionArgumentDelegate argumentOutOfRangeDelegate = new ExceptionArgumentDelegate(SetPendingArgumentOutOfRangeException);
154
155     [DllImport("$dllimport", EntryPoint="SWIGRegisterExceptionCallbacks_$module")]
156     public static extern void SWIGRegisterExceptionCallbacks_$module(
157                                 ExceptionDelegate applicationDelegate,
158                                 ExceptionDelegate arithmeticDelegate,
159                                 ExceptionDelegate divideByZeroDelegate, 
160                                 ExceptionDelegate indexOutOfRangeDelegate, 
161                                 ExceptionDelegate invalidCastDelegate,
162                                 ExceptionDelegate invalidOperationDelegate,
163                                 ExceptionDelegate ioDelegate,
164                                 ExceptionDelegate nullReferenceDelegate,
165                                 ExceptionDelegate outOfMemoryDelegate, 
166                                 ExceptionDelegate overflowDelegate, 
167                                 ExceptionDelegate systemExceptionDelegate);
168
169     [DllImport("$dllimport", EntryPoint="SWIGRegisterExceptionArgumentCallbacks_$module")]
170     public static extern void SWIGRegisterExceptionCallbacksArgument_$module(
171                                 ExceptionArgumentDelegate argumentDelegate,
172                                 ExceptionArgumentDelegate argumentNullDelegate,
173                                 ExceptionArgumentDelegate argumentOutOfRangeDelegate);
174
175     static void SetPendingApplicationException(string message) {
176       SWIGPendingException.Set(new System.ApplicationException(message, SWIGPendingException.Retrieve()));
177     }
178     static void SetPendingArithmeticException(string message) {
179       SWIGPendingException.Set(new System.ArithmeticException(message, SWIGPendingException.Retrieve()));
180     }
181     static void SetPendingDivideByZeroException(string message) {
182       SWIGPendingException.Set(new System.DivideByZeroException(message, SWIGPendingException.Retrieve()));
183     }
184     static void SetPendingIndexOutOfRangeException(string message) {
185       SWIGPendingException.Set(new System.IndexOutOfRangeException(message, SWIGPendingException.Retrieve()));
186     }
187     static void SetPendingInvalidCastException(string message) {
188       SWIGPendingException.Set(new System.InvalidCastException(message, SWIGPendingException.Retrieve()));
189     }
190     static void SetPendingInvalidOperationException(string message) {
191       SWIGPendingException.Set(new System.InvalidOperationException(message, SWIGPendingException.Retrieve()));
192     }
193     static void SetPendingIOException(string message) {
194       SWIGPendingException.Set(new System.IO.IOException(message, SWIGPendingException.Retrieve()));
195     }
196     static void SetPendingNullReferenceException(string message) {
197       SWIGPendingException.Set(new System.NullReferenceException(message, SWIGPendingException.Retrieve()));
198     }
199     static void SetPendingOutOfMemoryException(string message) {
200       SWIGPendingException.Set(new System.OutOfMemoryException(message, SWIGPendingException.Retrieve()));
201     }
202     static void SetPendingOverflowException(string message) {
203       SWIGPendingException.Set(new System.OverflowException(message, SWIGPendingException.Retrieve()));
204     }
205     static void SetPendingSystemException(string message) {
206       SWIGPendingException.Set(new System.SystemException(message, SWIGPendingException.Retrieve()));
207     }
208
209     static void SetPendingArgumentException(string message, string paramName) {
210       SWIGPendingException.Set(new System.ArgumentException(message, paramName, SWIGPendingException.Retrieve()));
211     }
212     static void SetPendingArgumentNullException(string message, string paramName) {
213       Exception e = SWIGPendingException.Retrieve();
214       if (e != null) message = message + " Inner Exception: " + e.Message;
215       SWIGPendingException.Set(new System.ArgumentNullException(paramName, message));
216     }
217     static void SetPendingArgumentOutOfRangeException(string message, string paramName) {
218       Exception e = SWIGPendingException.Retrieve();
219       if (e != null) message = message + " Inner Exception: " + e.Message;
220       SWIGPendingException.Set(new System.ArgumentOutOfRangeException(paramName, message));
221     }
222
223     static SWIGExceptionHelper() {
224       SWIGRegisterExceptionCallbacks_$module(
225                                 applicationDelegate,
226                                 arithmeticDelegate,
227                                 divideByZeroDelegate,
228                                 indexOutOfRangeDelegate,
229                                 invalidCastDelegate,
230                                 invalidOperationDelegate,
231                                 ioDelegate,
232                                 nullReferenceDelegate,
233                                 outOfMemoryDelegate,
234                                 overflowDelegate,
235                                 systemDelegate);
236
237       SWIGRegisterExceptionCallbacksArgument_$module(
238                                 argumentDelegate,
239                                 argumentNullDelegate,
240                                 argumentOutOfRangeDelegate);
241     }
242   }
243
244   protected static SWIGExceptionHelper swigExceptionHelper = new SWIGExceptionHelper();
245
246   public class SWIGPendingException {
247     [ThreadStatic]
248     private static Exception pendingException = null;
249     private static int numExceptionsPending = 0;
250
251     public static bool Pending {
252       get {
253         bool pending = false;
254         if (numExceptionsPending > 0)
255           if (pendingException != null)
256             pending = true;
257         return pending;
258       } 
259     }
260
261     public static void Set(Exception e) {
262       if (pendingException != null)
263         throw new ApplicationException("FATAL: An earlier pending exception from unmanaged code was missed and thus not thrown (" + pendingException.ToString() + ")", e);
264       pendingException = e;
265       lock(typeof($imclassname)) {
266         numExceptionsPending++;
267       }
268     }
269
270     public static Exception Retrieve() {
271       Exception e = null;
272       if (numExceptionsPending > 0) {
273         if (pendingException != null) {
274           e = pendingException;
275           pendingException = null;
276           lock(typeof($imclassname)) {
277             numExceptionsPending--;
278           }
279         }
280       }
281       return e;
282     }
283   }
284 %}
285 #endif // SWIG_CSHARP_NO_EXCEPTION_HELPER
286
287 #if !defined(SWIG_CSHARP_NO_STRING_HELPER)
288 %insert(runtime) %{
289 /* Callback for returning strings to C# without leaking memory */
290 typedef char * (SWIGSTDCALL* SWIG_CSharpStringHelperCallback)(const char *);
291 static SWIG_CSharpStringHelperCallback SWIG_csharp_string_callback = NULL;
292 %}
293
294 %pragma(csharp) imclasscode=%{
295   protected class SWIGStringHelper {
296
297     public delegate string SWIGStringDelegate(string message);
298     static SWIGStringDelegate stringDelegate = new SWIGStringDelegate(CreateString);
299
300     [DllImport("$dllimport", EntryPoint="SWIGRegisterStringCallback_$module")]
301     public static extern void SWIGRegisterStringCallback_$module(SWIGStringDelegate stringDelegate);
302
303     static string CreateString(string cString) {
304       return cString;
305     }
306
307     static SWIGStringHelper() {
308       SWIGRegisterStringCallback_$module(stringDelegate);
309     }
310   }
311
312   static protected SWIGStringHelper swigStringHelper = new SWIGStringHelper();
313 %}
314
315 %insert(runtime) %{
316 #ifdef __cplusplus
317 extern "C" 
318 #endif
319 SWIGEXPORT void SWIGSTDCALL SWIGRegisterStringCallback_$module(SWIG_CSharpStringHelperCallback callback) {
320   SWIG_csharp_string_callback = callback;
321 }
322 %}
323 #endif // SWIG_CSHARP_NO_STRING_HELPER
324
325 %insert(runtime) %{
326 /* Contract support */
327
328 #define SWIG_contract_assert(nullreturn, expr, msg) if (!(expr)) {SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, msg, ""); return nullreturn; } else
329 %}