/
csmodule.i
480 lines (429 loc) · 16.7 KB
/
csmodule.i
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: C#-specific enhancements to MapScript
* Author: Tamas Szekeres, szekerest@gmail.com
*
******************************************************************************
* Copyright (c) 1996-2008 Regents of the University of Minnesota.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
/*Uncomment the following lines if you want to receive subsequent exceptions as
inner exceptions. Otherwise the exception message will be concatenated*/
//#if SWIG_VERSION >= 0x010329
//#define ALLOW_INNER_EXCEPTIONS
//#endif
%ignore fp;
/******************************************************************************
* Implement Equals and GetHashCode properly
*****************************************************************************/
%typemap(cscode) SWIGTYPE %{
public override bool Equals(object obj) {
if (obj == null)
return false;
if (this.GetType() != obj.GetType())
return false;
return swigCPtr.Handle.Equals($csclassname.getCPtr(($csclassname)obj).Handle);
}
public override int GetHashCode() {
return swigCPtr.Handle.GetHashCode();
}
%}
/******************************************************************************
* Module initialization helper (bug 1665)
*****************************************************************************/
%pragma(csharp) imclasscode=%{
protected class $moduleHelper
{
static $moduleHelper()
{
$module.msSetup();
}
~$moduleHelper()
{
//$module.msCleanup();
}
}
protected static $moduleHelper the$moduleHelper = new $moduleHelper();
%}
/******************************************************************************
* C# exception redefinition
*****************************************************************************/
#ifdef ALLOW_INNER_EXCEPTIONS
%exception
{
errorObj *ms_error;
$action
ms_error = msGetErrorObj();
if (ms_error != NULL && ms_error->code != MS_NOERR) {
if (ms_error->code != MS_NOTFOUND && ms_error->code != -1) {
int ms_errorcode = ms_error->code;
while (ms_error!=NULL && ms_error->code != MS_NOERR) {
char* msg = msAddErrorDisplayString(NULL, ms_error);
if (msg) {
SWIG_CSharpException(SWIG_SystemError, msg);
free(msg);
}
else SWIG_CSharpException(SWIG_SystemError, "MapScript unknown error");
ms_error = ms_error->next;
}
msResetErrorList();
return $null;
}
msResetErrorList();
}
}
#else
%exception
{
errorObj *ms_error;
$action
ms_error = msGetErrorObj();
if (ms_error != NULL && ms_error->code != MS_NOERR) {
if (ms_error->code != MS_NOTFOUND && ms_error->code != -1) {
char* msg = msGetErrorString(";");
if (msg) {
SWIG_CSharpException(SWIG_SystemError, msg);
free(msg);
}
else SWIG_CSharpException(SWIG_SystemError, "MapScript unknown error");
msResetErrorList();
return $null;
}
msResetErrorList();
}
}
#endif
/******************************************************************************
* typemaps for string arrays (for supporting map templates)
*****************************************************************************/
%pragma(csharp) imclasscode=%{
public class StringArrayMarshal : System.IDisposable {
public readonly System.IntPtr[] _ar;
public StringArrayMarshal(string[] ar) {
_ar = new System.IntPtr[ar.Length];
for (int cx = 0; cx < _ar.Length; cx++) {
_ar[cx] = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(ar[cx]);
}
}
public virtual void Dispose() {
for (int cx = 0; cx < _ar.Length; cx++) {
System.Runtime.InteropServices.Marshal.FreeHGlobal(_ar[cx]);
}
GC.SuppressFinalize(this);
}
}
%}
%typemap(imtype, out="System.IntPtr") char** "System.IntPtr[]"
%typemap(cstype) char** %{string[]%}
%typemap(in) char** %{ $1 = ($1_ltype)$input; %}
%typemap(out) char** %{ $result = $1; %}
%typemap(csin) char** "new $modulePINVOKE.StringArrayMarshal($csinput)._ar"
%typemap(csout, excode=SWIGEXCODE) char** {
$excode
throw new System.NotSupportedException("Returning string arrays is not implemented yet.");
}
%typemap(csvarin, excode=SWIGEXCODE2) char** %{
set {
$excode
throw new System.NotSupportedException("Setting string arrays is not supported now.");
}
%}
/* specializations */
%typemap(csvarout, excode=SWIGEXCODE2) char** formatoptions %{
get {
System.IntPtr cPtr = $imcall;
System.IntPtr objPtr;
string[] ret = new string[this.numformatoptions];
for(int cx = 0; cx < this.numformatoptions; cx++) {
objPtr = System.Runtime.InteropServices.Marshal.ReadIntPtr(cPtr, cx * System.Runtime.InteropServices.Marshal.SizeOf(typeof(System.IntPtr)));
ret[cx]= (objPtr == System.IntPtr.Zero) ? null : System.Runtime.InteropServices.Marshal.PtrToStringAnsi(objPtr);
}
$excode
return ret;
}
%}
%typemap(csvarout, excode=SWIGEXCODE2) char** values %{
get {
System.IntPtr cPtr = $imcall;
System.IntPtr objPtr;
string[] ret = new string[this.numvalues];
for(int cx = 0; cx < this.numvalues; cx++) {
objPtr = System.Runtime.InteropServices.Marshal.ReadIntPtr(cPtr, cx * System.Runtime.InteropServices.Marshal.SizeOf(typeof(System.IntPtr)));
ret[cx]= (objPtr == System.IntPtr.Zero) ? null : System.Runtime.InteropServices.Marshal.PtrToStringAnsi(objPtr);
}
$excode
return ret;
}
%}
/******************************************************************************
* typemaps for outputFormatObj arrays
*****************************************************************************/
%typemap(ctype) outputFormatObj** "void*"
%typemap(imtype) outputFormatObj** "System.IntPtr"
%typemap(cstype) outputFormatObj** "outputFormatObj[]"
%typemap(out) outputFormatObj** %{ $result = $1; %}
%typemap(csout, excode=SWIGEXCODE) outputFormatObj** {
System.IntPtr cPtr = $imcall;
System.IntPtr objPtr;
outputFormatObj[] ret = new outputFormatObj[this.numoutputformats];
for(int cx = 0; cx < this.numoutputformats; cx++) {
objPtr = System.Runtime.InteropServices.Marshal.ReadIntPtr(cPtr, cx * System.Runtime.InteropServices.Marshal.SizeOf(typeof(System.IntPtr)));
ret[cx] = (objPtr == System.IntPtr.Zero) ? null : new outputFormatObj(objPtr, false, ThisOwn_false());
}
$excode
return ret;
}
%typemap(csvarout, excode=SWIGEXCODE2) outputFormatObj** %{
get {
System.IntPtr cPtr = $imcall;
System.IntPtr objPtr;
outputFormatObj[] ret = new outputFormatObj[this.numoutputformats];
for(int cx = 0; cx < this.numoutputformats; cx++) {
objPtr = System.Runtime.InteropServices.Marshal.ReadIntPtr(cPtr, cx * System.Runtime.InteropServices.Marshal.SizeOf(typeof(System.IntPtr)));
ret[cx] = (objPtr == System.IntPtr.Zero) ? null : new outputFormatObj(objPtr, false, ThisOwn_false());
}
$excode
return ret;
}
%}
/******************************************************************************
* gdBuffer Typemaps and helpers
*****************************************************************************/
%pragma(csharp) imclasscode=%{
public delegate void SWIGByteArrayDelegate(System.IntPtr data, int size);
%}
%insert(runtime) %{
/* Callback for returning byte arrays to C# */
typedef void (SWIGSTDCALL* SWIG_CSharpByteArrayHelperCallback)(const unsigned char *, const int);
/* Default callback interface */
static SWIG_CSharpByteArrayHelperCallback SWIG_csharp_bytearray_callback = NULL;
%}
%typemap(ctype) SWIG_CSharpByteArrayHelperCallback %{SWIG_CSharpByteArrayHelperCallback%}
%typemap(imtype) SWIG_CSharpByteArrayHelperCallback %{SWIGByteArrayDelegate%}
%typemap(cstype) SWIG_CSharpByteArrayHelperCallback %{$modulePINVOKE.SWIGByteArrayDelegate%}
%typemap(in) SWIG_CSharpByteArrayHelperCallback %{ $1 = ($1_ltype)$input; %}
%typemap(csin) SWIG_CSharpByteArrayHelperCallback "$csinput"
%typemap(imtype) (unsigned char* pixels) "System.IntPtr"
%typemap(cstype) (unsigned char* pixels) "System.IntPtr"
%typemap(in) (unsigned char* pixels) %{ $1 = ($1_ltype)$input; %}
%typemap(csin) (unsigned char* pixels) "$csinput"
%csmethodmodifiers getBytes "private";
%ignore imageObj::getBytes();
%extend imageObj
{
void getBytes(SWIG_CSharpByteArrayHelperCallback callback) {
gdBuffer buffer;
buffer.owns_data = MS_TRUE;
buffer.data = msSaveImageBuffer(self, &buffer.size, self->format);
if( buffer.data == NULL || buffer.size == 0 )
{
msSetError(MS_MISCERR, "Failed to get image buffer", "getBytes");
return;
}
callback(buffer.data, buffer.size);
msFree(buffer.data);
}
int getRawPixels(unsigned char* pixels) {
if (MS_RENDERER_PLUGIN(self->format)) {
rendererVTableObj *renderer = self->format->vtable;
if(renderer->supports_pixel_buffer) {
rasterBufferObj rb;
int status = MS_SUCCESS;
int size = self->width * self->height * 4 * sizeof(unsigned char);
status = renderer->getRasterBufferHandle(self,&rb);
if(UNLIKELY(status == MS_FAILURE)) {
return MS_FAILURE;
}
memcpy(pixels, rb.data.rgba.pixels, size);
return status;
}
}
return MS_FAILURE;
}
}
%ignore imageObj::write;
%typemap(cscode) imageObj, struct imageObj %{
private byte[] gdbuffer;
private void CreateByteArray(System.IntPtr data, int size)
{
gdbuffer = new byte[size];
Marshal.Copy(data, gdbuffer, 0, size);
}
public byte[] getBytes()
{
getBytes(new $modulePINVOKE.SWIGByteArrayDelegate(this.CreateByteArray));
return gdbuffer;
}
public void write(System.IO.Stream stream)
{
getBytes(new $modulePINVOKE.SWIGByteArrayDelegate(this.CreateByteArray));
stream.Write(gdbuffer, 0, gdbuffer.Length);
}
%}
%csmethodmodifiers processTemplate "private";
%csmethodmodifiers processLegendTemplate "private";
%csmethodmodifiers processQueryTemplate "private";
%typemap(csinterfaces) mapObj "System.IDisposable, System.Runtime.Serialization.ISerializable";
%typemap(csattributes) mapObj "[Serializable]"
%typemap(cscode) mapObj, struct mapObj %{
public string processTemplate(int bGenerateImages, string[] names, string[] values)
{
if (names.Length != values.Length)
throw new ArgumentException("Invalid array length specified!");
return processTemplate(bGenerateImages, names, values, values.Length);
}
public string processLegendTemplate(string[] names, string[] values)
{
if (names.Length != values.Length)
throw new ArgumentException("Invalid array length specified!");
return processLegendTemplate(names, values, values.Length);
}
public string processQueryTemplate(string[] names, string[] values)
{
if (names.Length != values.Length)
throw new ArgumentException("Invalid array length specified!");
return processQueryTemplate(names, values, values.Length);
}
public mapObj(
System.Runtime.Serialization.SerializationInfo info
, System.Runtime.Serialization.StreamingContext context) : this(info.GetString("mapText"), 1)
{
//this constructor is needed for ISerializable interface
}
public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
info.AddValue( "mapText", this.convertToString() );
}
%}
%typemap(ctype) gdBuffer %{void%}
%typemap(imtype) gdBuffer %{void%}
%typemap(cstype) gdBuffer %{byte[]%}
%typemap(out, null="") gdBuffer
%{ SWIG_csharp_bytearray_callback($1.data, $1.size);
if( $1.owns_data ) msFree($1.data); %}
// SWIGEXCODE is a macro used by many other csout typemaps
#ifdef SWIGEXCODE
%typemap(csout, excode=SWIGEXCODE) gdBuffer {
$imcall;$excode
return $modulePINVOKE.GetBytes();
}
#else
%typemap(csout) gdBuffer {
$imcall;
return $modulePINVOKE.GetBytes();
}
#endif
%pragma(csharp) imclasscode=%{
protected class SWIGByteArrayHelper
{
public delegate void SWIGByteArrayDelegate(System.IntPtr data, int size);
static SWIGByteArrayDelegate bytearrayDelegate = new SWIGByteArrayDelegate(CreateByteArray);
[System.Runtime.InteropServices.DllImport("$dllimport", EntryPoint="SWIGRegisterByteArrayCallback_$module")]
public static extern void SWIGRegisterByteArrayCallback_mapscript(SWIGByteArrayDelegate bytearrayDelegate);
static void CreateByteArray(System.IntPtr data, int size)
{
arraybuffer = new byte[size];
Marshal.Copy(data, arraybuffer, 0, size);
}
static SWIGByteArrayHelper()
{
SWIGRegisterByteArrayCallback_$module(bytearrayDelegate);
}
}
protected static SWIGByteArrayHelper bytearrayHelper = new SWIGByteArrayHelper();
[System.ThreadStatic]
private static byte[] arraybuffer;
internal static byte[] GetBytes()
{
return arraybuffer;
}
%}
%insert(runtime) %{
#ifdef __cplusplus
extern "C"
#endif
#ifdef SWIGEXPORT
SWIGEXPORT void SWIGSTDCALL SWIGRegisterByteArrayCallback_$module(SWIG_CSharpByteArrayHelperCallback callback) {
SWIG_csharp_bytearray_callback = callback;
}
#else
DllExport void SWIGSTDCALL SWIGRegisterByteArrayCallback_$module(SWIG_CSharpByteArrayHelperCallback callback) {
SWIG_csharp_bytearray_callback = callback;
}
#endif
%}
/* Typemaps for pattern array */
%typemap(imtype) (double pattern[ANY]) "System.IntPtr"
%typemap(cstype) (double pattern[ANY]) "double[]"
%typemap(in) (double pattern[ANY]) %{ $1 = ($1_ltype)$input; %}
%typemap(csin) (double pattern[ANY]) "$csinput"
%typemap(csvarout, excode=SWIGEXCODE2) (double pattern[ANY]) %{
get {
System.IntPtr cPtr = $imcall;
double[] ret = new double[patternlength];
if (patternlength > 0) {
System.Runtime.InteropServices.Marshal.Copy(cPtr, ret, 0, patternlength);
}
$excode
return ret;
}
set {
System.IntPtr cPtr = $imcall;
if (value.Length > 0) {
System.Runtime.InteropServices.Marshal.Copy(value, 0, cPtr, value.Length);
}
patternlength = value.Length;
$excode
}
%}
%typemap(csvarin, excode="") (double pattern[ANY]) %{$excode%}
/* Typemaps for int array */
%typemap(imtype, out="System.IntPtr") int *panIndexes "int[]"
%typemap(cstype) int *panIndexes %{int[]%}
%typemap(in) int *panIndexes %{ $1 = ($1_ltype)$input; %}
%typemap(csin) (int *panIndexes) "$csinput"
/* Typemaps for device handle */
%typemap(imtype) (void* device) %{ System.IntPtr%}
%typemap(cstype) (void* device) %{ System.IntPtr%}
%typemap(in) (void* device) %{ $1 = ($1_ltype)$input; %}
%typemap(csin) (void* device) "$csinput"
/******************************************************************************
* Preventing to take ownership of the memory when constructing objects
* with parent objects (causing nullreference exception, Bug 1743)
*****************************************************************************/
%typemap(csconstruct, excode=SWIGEXCODE) layerObj(mapObj map) %{: this($imcall, true, map) {
$excode
}
%}
%typemap(csconstruct, excode=SWIGEXCODE) classObj(layerObj layer) %{: this($imcall, true, layer) {
$excode
}
%}
%typemap(csconstruct, excode=SWIGEXCODE) styleObj(classObj parent_class) %{: this($imcall, true, parent_class) {
$excode
}
%}
%typemap(csout, excode=SWIGEXCODE) classObj* getClass, layerObj* getLayer, layerObj *getLayerByName, styleObj* getStyle {
System.IntPtr cPtr = $imcall;
$csclassname ret = (cPtr == System.IntPtr.Zero) ? null : new $csclassname(cPtr, $owner, ThisOwn_false());$excode
return ret;
}