summaryrefslogtreecommitdiff
path: root/Examples
diff options
context:
space:
mode:
authorWilliam S Fulton <wsf@fultondesigns.co.uk>2008-09-18 20:32:28 +0000
committerWilliam S Fulton <wsf@fultondesigns.co.uk>2008-09-18 20:32:28 +0000
commit09915566bcc6703b0ca645998c722c112d1dbef4 (patch)
treeece722bfc2ad7571f995813b2b21dbef64b26fe8 /Examples
parent1ebd0466f5cd8dbfd91d2fd7c27d7e68bce11897 (diff)
downloadswig-09915566bcc6703b0ca645998c722c112d1dbef4.tar.gz
Add C# array typemaps provided by Antti Karanta.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@10872 626c5289-ae23-0410-ae9c-e8d60b6d4f22
Diffstat (limited to 'Examples')
-rw-r--r--Examples/csharp/arrays/Makefile20
-rw-r--r--Examples/csharp/arrays/example.c22
-rw-r--r--Examples/csharp/arrays/example.h4
-rw-r--r--Examples/csharp/arrays/example.i42
-rw-r--r--Examples/csharp/arrays/runme.cs43
-rw-r--r--Examples/csharp/check.list1
-rw-r--r--Examples/test-suite/csharp/Makefile.in12
-rw-r--r--Examples/test-suite/csharp/csharp_lib_arrays_runme.cs70
-rw-r--r--Examples/test-suite/csharp_lib_arrays.i61
9 files changed, 272 insertions, 3 deletions
diff --git a/Examples/csharp/arrays/Makefile b/Examples/csharp/arrays/Makefile
new file mode 100644
index 000000000..b3446d895
--- /dev/null
+++ b/Examples/csharp/arrays/Makefile
@@ -0,0 +1,20 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS = example.c
+TARGET = example
+INTERFACE = example.i
+SWIGOPT =
+CSHARPSRCS = *.cs
+CSHARPFLAGS= -nologo -unsafe -out:runme.exe
+
+all:: csharp
+
+csharp::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' csharp
+ $(MAKE) -f $(TOP)/Makefile CSHARPSRCS='$(CSHARPSRCS)' CSHARPFLAGS='$(CSHARPFLAGS)' csharp_compile
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile csharp_clean
+
+check: all
diff --git a/Examples/csharp/arrays/example.c b/Examples/csharp/arrays/example.c
new file mode 100644
index 000000000..2498e1f4e
--- /dev/null
+++ b/Examples/csharp/arrays/example.c
@@ -0,0 +1,22 @@
+/* File : example.c */
+
+#include "example.h"
+
+/* copy the contents of the first array to the second */
+void myArrayCopy( int* sourceArray, int* targetArray, int nitems ) {
+ int i;
+ for ( i = 0; i < nitems; i++ ) {
+ targetArray[ i ] = sourceArray[ i ];
+ }
+}
+
+/* swap the contents of the two arrays */
+void myArraySwap( int* array1, int* array2, int nitems ) {
+ int i, temp;
+ for ( i = 0; i < nitems; i++ ) {
+ temp = array1[ i ];
+ array1[ i ] = array2[ i ];
+ array2[ i ] = temp;
+ }
+}
+
diff --git a/Examples/csharp/arrays/example.h b/Examples/csharp/arrays/example.h
new file mode 100644
index 000000000..113b92c1b
--- /dev/null
+++ b/Examples/csharp/arrays/example.h
@@ -0,0 +1,4 @@
+
+void myArrayCopy( int *sourceArray, int* targetArray, int nitems );
+void myArraySwap( int* array1, int* array2, int nitems );
+
diff --git a/Examples/csharp/arrays/example.i b/Examples/csharp/arrays/example.i
new file mode 100644
index 000000000..22b9aa19d
--- /dev/null
+++ b/Examples/csharp/arrays/example.i
@@ -0,0 +1,42 @@
+/* File : example.i */
+%module example
+
+%include "arrays_csharp.i"
+
+%apply int INPUT[] { int* sourceArray }
+%apply int OUTPUT[] { int* targetArray }
+
+%apply int INOUT[] { int* array1 }
+%apply int INOUT[] { int* array2 }
+
+%include "example.h"
+
+%clear int* sourceArray;
+%clear int* targetArray;
+
+%clear int* array1;
+%clear int* array2;
+
+
+// Below replicates the above array handling but this time using the pinned (fixed) array typemaps
+%csmethodmodifiers "public unsafe";
+
+%apply int FIXED[] { int* sourceArray }
+%apply int FIXED[] { int* targetArray }
+
+%inline %{
+void myArrayCopyUsingFixedArrays( int *sourceArray, int* targetArray, int nitems ) {
+ myArrayCopy(sourceArray, targetArray, nitems);
+}
+%}
+
+%apply int FIXED[] { int* array1 }
+%apply int FIXED[] { int* array2 }
+
+%inline %{
+void myArraySwapUsingFixedArrays( int* array1, int* array2, int nitems ) {
+ myArraySwap(array1, array2, nitems);
+}
+%}
+
+
diff --git a/Examples/csharp/arrays/runme.cs b/Examples/csharp/arrays/runme.cs
new file mode 100644
index 000000000..c2b8a6bdd
--- /dev/null
+++ b/Examples/csharp/arrays/runme.cs
@@ -0,0 +1,43 @@
+using System;
+
+public class runme
+{
+ static void Main()
+ {
+ int[] source = { 1, 2, 3 };
+ int[] target = new int[ source.Length ];
+
+ example.myArrayCopy( source, target, target.Length );
+
+ Console.WriteLine( "Contents of copy target array using default marshaling" );
+ PrintArray( target );
+
+ target = new int[ source.Length ];
+
+ example.myArrayCopyUsingFixedArrays( source, target, target.Length );
+ Console.WriteLine( "Contents of copy target array using fixed arrays" );
+ PrintArray( target );
+
+ target = new int[] { 4, 5, 6 };
+ example.myArraySwap( source, target, target.Length );
+ Console.WriteLine( "Contents of arrays after swapping using default marshaling" );
+ PrintArray( source );
+ PrintArray( target );
+
+ source = new int[] { 1, 2, 3 };
+ target = new int[] { 4, 5, 6 };
+
+ example.myArraySwapUsingFixedArrays( source, target, target.Length );
+ Console.WriteLine( "Contents of arrays after swapping using fixed arrays" );
+ PrintArray( source );
+ PrintArray( target );
+ }
+
+ static void PrintArray( int[] a )
+ {
+ foreach ( int i in a )
+ Console.Write( "{0} ", i );
+ Console.WriteLine();
+ }
+}
+
diff --git a/Examples/csharp/check.list b/Examples/csharp/check.list
index c4d92402b..5454d8531 100644
--- a/Examples/csharp/check.list
+++ b/Examples/csharp/check.list
@@ -1,4 +1,5 @@
# see top-level Makefile.in
+arrays
callback
class
enum
diff --git a/Examples/test-suite/csharp/Makefile.in b/Examples/test-suite/csharp/Makefile.in
index 5fd576ed8..f269b3276 100644
--- a/Examples/test-suite/csharp/Makefile.in
+++ b/Examples/test-suite/csharp/Makefile.in
@@ -21,13 +21,17 @@ CPP_TEST_CASES = \
enum_thorough_typesafe \
exception_partial_info
-CUSTOM_TEST_CASES = intermediary_classname
+CUSTOM_TEST_CASES = \
+ csharp_lib_arrays \
+ intermediary_classname
include $(srcdir)/../common.mk
# Overridden variables here
SWIGOPT += -namespace $*Namespace $(SWIGOPTSPECIAL)
+CSHARPFLAGSSPECIAL =
+
# Rules for the different types of tests
%.cpptest:
$(setup)
@@ -47,6 +51,8 @@ SWIGOPT += -namespace $*Namespace $(SWIGOPTSPECIAL)
# Rules for custom tests
intermediary_classname.customtest:
$(MAKE) intermediary_classname.cpptest SWIGOPTSPECIAL="-dllimport intermediary_classname"
+csharp_lib_arrays.customtest:
+ $(MAKE) csharp_lib_arrays.cpptest CSHARPFLAGSSPECIAL="-unsafe"
# Makes a directory for the testcase if it does not exist
setup = \
@@ -65,14 +71,14 @@ setup = \
run_testcase = \
if [ -f $(srcdir)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) ]; then ( \
$(MAKE) -f $*/$(top_builddir)/$(EXAMPLES)/Makefile \
- CSHARPFLAGS='-nologo -out:$*_runme.exe' \
+ CSHARPFLAGS='-nologo $(CSHARPFLAGSSPECIAL) -out:$*_runme.exe' \
CSHARPSRCS='`$(CSHARPCYGPATH_W) $(srcdir)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX)` \
$*$(CSHARPPATHSEPARATOR)*.cs' csharp_compile && \
env LD_LIBRARY_PATH="$*:$$LD_LIBRARY_PATH" PATH="$*:$$PATH" SHLIB_PATH="$*:$$SHLIB_PATH" $(RUNTOOL) $(INTERPRETER) $*_runme.exe; ) \
else ( \
cd $* && \
$(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile \
- CSHARPFLAGS='-nologo -t:module -out:$*.netmodule' \
+ CSHARPFLAGS='-nologo $(CSHARPFLAGSSPECIAL) -t:module -out:$*.netmodule' \
CSHARPSRCS='*.cs' csharp_compile; ); \
fi;
diff --git a/Examples/test-suite/csharp/csharp_lib_arrays_runme.cs b/Examples/test-suite/csharp/csharp_lib_arrays_runme.cs
new file mode 100644
index 000000000..9f3ea6b88
--- /dev/null
+++ b/Examples/test-suite/csharp/csharp_lib_arrays_runme.cs
@@ -0,0 +1,70 @@
+using System;
+using csharp_lib_arraysNamespace;
+
+public class runme
+{
+ static void Main()
+ {
+ {
+ int[] source = { 1, 2, 3, 4, 5 };
+ int[] target = new int[ source.Length ];
+
+ csharp_lib_arrays.myArrayCopy( source, target, target.Length );
+ CompareArrays(source, target);
+ }
+
+ {
+ int[] source = { 1, 2, 3, 4, 5 };
+ int[] target = new int[ source.Length ];
+
+ csharp_lib_arrays.myArrayCopyUsingFixedArrays( source, target, target.Length );
+ CompareArrays(source, target);
+ }
+
+ {
+ int[] source = { 1, 2, 3, 4, 5 };
+ int[] target = new int[] { 6, 7, 8, 9, 10 };
+
+ csharp_lib_arrays.myArraySwap( source, target, target.Length );
+
+ for (int i=0; i<target.Length; ++i)
+ target[i] += 5;
+ CompareArrays(source, target);
+ }
+
+ {
+ int[] source = { 1, 2, 3, 4, 5 };
+ int[] target = new int[] { 6, 7, 8, 9, 10 };
+
+ csharp_lib_arrays.myArraySwapUsingFixedArrays( source, target, target.Length );
+
+ for (int i=0; i<target.Length; ++i)
+ target[i] += 5;
+ CompareArrays(source, target);
+ }
+ }
+
+ static void CompareArrays( int[] a, int[] b )
+ {
+ if (a.Length != b.Length)
+ throw new Exception("size mismatch");
+
+ for(int i=0; i<a.Length; ++i) {
+ if (a[i] != b[i]) {
+ Console.Error.WriteLine("a:");
+ PrintArray(a);
+ Console.Error.WriteLine("b:");
+ PrintArray(b);
+ throw new Exception("element mismatch");
+ }
+ }
+ }
+
+ static void PrintArray( int[] a )
+ {
+ foreach ( int i in a )
+ Console.Error.Write( "{0} ", i );
+ Console.Error.WriteLine();
+ }
+}
+
diff --git a/Examples/test-suite/csharp_lib_arrays.i b/Examples/test-suite/csharp_lib_arrays.i
new file mode 100644
index 000000000..d07d43737
--- /dev/null
+++ b/Examples/test-suite/csharp_lib_arrays.i
@@ -0,0 +1,61 @@
+%module csharp_lib_arrays
+
+%include "arrays_csharp.i"
+
+%apply int INPUT[] { int* sourceArray }
+%apply int OUTPUT[] { int* targetArray }
+
+%apply int INOUT[] { int* array1 }
+%apply int INOUT[] { int* array2 }
+
+%inline %{
+/* copy the contents of the first array to the second */
+void myArrayCopy( int* sourceArray, int* targetArray, int nitems ) {
+ int i;
+ for ( i = 0; i < nitems; i++ ) {
+ targetArray[ i ] = sourceArray[ i ];
+ }
+}
+
+/* swap the contents of the two arrays */
+void myArraySwap( int* array1, int* array2, int nitems ) {
+ int i, temp;
+ for ( i = 0; i < nitems; i++ ) {
+ temp = array1[ i ];
+ array1[ i ] = array2[ i ];
+ array2[ i ] = temp;
+ }
+}
+%}
+
+
+%clear int* sourceArray;
+%clear int* targetArray;
+
+%clear int* array1;
+%clear int* array2;
+
+
+// Below replicates the above array handling but this time using the pinned (fixed) array typemaps
+%csmethodmodifiers myArrayCopyUsingFixedArrays "public unsafe";
+%csmethodmodifiers myArraySwapUsingFixedArrays "public unsafe";
+
+%apply int FIXED[] { int* sourceArray }
+%apply int FIXED[] { int* targetArray }
+
+%inline %{
+void myArrayCopyUsingFixedArrays( int *sourceArray, int* targetArray, int nitems ) {
+ myArrayCopy(sourceArray, targetArray, nitems);
+}
+%}
+
+%apply int FIXED[] { int* array1 }
+%apply int FIXED[] { int* array2 }
+
+%inline %{
+void myArraySwapUsingFixedArrays( int* array1, int* array2, int nitems ) {
+ myArraySwap(array1, array2, nitems);
+}
+%}
+
+