From 2824b0cbb66e715490e1ef13250bd675d87b32d9 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Wed, 2 Jun 2010 20:53:17 +0000 Subject: rel-2.0.0 git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/tags/rel-2.0.0@12089 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- trunk/Examples/csharp/class/Makefile | 20 ++++ trunk/Examples/csharp/class/example-cs.csproj | 104 +++++++++++++++++ trunk/Examples/csharp/class/example-vc.vcproj | 158 ++++++++++++++++++++++++++ trunk/Examples/csharp/class/example.cxx | 28 +++++ trunk/Examples/csharp/class/example.h | 39 +++++++ trunk/Examples/csharp/class/example.i | 10 ++ trunk/Examples/csharp/class/example.sln | 30 +++++ trunk/Examples/csharp/class/runme.cs | 66 +++++++++++ 8 files changed, 455 insertions(+) create mode 100644 trunk/Examples/csharp/class/Makefile create mode 100644 trunk/Examples/csharp/class/example-cs.csproj create mode 100644 trunk/Examples/csharp/class/example-vc.vcproj create mode 100644 trunk/Examples/csharp/class/example.cxx create mode 100644 trunk/Examples/csharp/class/example.h create mode 100644 trunk/Examples/csharp/class/example.i create mode 100644 trunk/Examples/csharp/class/example.sln create mode 100644 trunk/Examples/csharp/class/runme.cs (limited to 'trunk/Examples/csharp/class') diff --git a/trunk/Examples/csharp/class/Makefile b/trunk/Examples/csharp/class/Makefile new file mode 100644 index 000000000..20f0dd5bb --- /dev/null +++ b/trunk/Examples/csharp/class/Makefile @@ -0,0 +1,20 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +SWIGOPT = +CSHARPSRCS = *.cs +CSHARPFLAGS= -nologo -out:runme.exe + +all:: csharp + +csharp:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' csharp_cpp + $(MAKE) -f $(TOP)/Makefile CSHARPSRCS='$(CSHARPSRCS)' CSHARPFLAGS='$(CSHARPFLAGS)' csharp_compile + +clean:: + $(MAKE) -f $(TOP)/Makefile csharp_clean + +check: all diff --git a/trunk/Examples/csharp/class/example-cs.csproj b/trunk/Examples/csharp/class/example-cs.csproj new file mode 100644 index 000000000..0b9ea2e05 --- /dev/null +++ b/trunk/Examples/csharp/class/example-cs.csproj @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/trunk/Examples/csharp/class/example-vc.vcproj b/trunk/Examples/csharp/class/example-vc.vcproj new file mode 100644 index 000000000..7c0b8ef13 --- /dev/null +++ b/trunk/Examples/csharp/class/example-vc.vcproj @@ -0,0 +1,158 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/trunk/Examples/csharp/class/example.cxx b/trunk/Examples/csharp/class/example.cxx new file mode 100644 index 000000000..1e8e203dd --- /dev/null +++ b/trunk/Examples/csharp/class/example.cxx @@ -0,0 +1,28 @@ +/* File : example.c */ + +#include "example.h" +#define M_PI 3.14159265358979323846 + +/* Move the shape to a new location */ +void Shape::move(double dx, double dy) { + x += dx; + y += dy; +} + +int Shape::nshapes = 0; + +double Circle::area(void) { + return M_PI*radius*radius; +} + +double Circle::perimeter(void) { + return 2*M_PI*radius; +} + +double Square::area(void) { + return width*width; +} + +double Square::perimeter(void) { + return 4*width; +} diff --git a/trunk/Examples/csharp/class/example.h b/trunk/Examples/csharp/class/example.h new file mode 100644 index 000000000..46d901361 --- /dev/null +++ b/trunk/Examples/csharp/class/example.h @@ -0,0 +1,39 @@ +/* File : example.h */ + +class Shape { +public: + Shape() { + nshapes++; + } + virtual ~Shape() { + nshapes--; + }; + double x, y; + void move(double dx, double dy); + virtual double area(void) = 0; + virtual double perimeter(void) = 0; + static int nshapes; +}; + +class Circle : public Shape { +private: + double radius; +public: + Circle(double r) : radius(r) { }; + virtual double area(void); + virtual double perimeter(void); +}; + +class Square : public Shape { +private: + double width; +public: + Square(double w) : width(w) { }; + virtual double area(void); + virtual double perimeter(void); +}; + + + + + diff --git a/trunk/Examples/csharp/class/example.i b/trunk/Examples/csharp/class/example.i new file mode 100644 index 000000000..75700b305 --- /dev/null +++ b/trunk/Examples/csharp/class/example.i @@ -0,0 +1,10 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +/* Let's just grab the original header file here */ +%include "example.h" + diff --git a/trunk/Examples/csharp/class/example.sln b/trunk/Examples/csharp/class/example.sln new file mode 100644 index 000000000..28b9851b0 --- /dev/null +++ b/trunk/Examples/csharp/class/example.sln @@ -0,0 +1,30 @@ +Microsoft Visual Studio Solution File, Format Version 8.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "example-cs", "example-cs.csproj", "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + ProjectSection(ProjectDependencies) = postProject + {C2302635-D489-4678-96B4-70F5309DCBE6} = {C2302635-D489-4678-96B4-70F5309DCBE6} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example-vc", "example-vc.vcproj", "{C2302635-D489-4678-96B4-70F5309DCBE6}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfiguration) = preSolution + Debug = Debug + Release = Release + EndGlobalSection + GlobalSection(ProjectConfiguration) = postSolution + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.ActiveCfg = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Debug.Build.0 = Debug|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.ActiveCfg = Release|.NET + {C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}.Release.Build.0 = Release|.NET + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.ActiveCfg = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Debug.Build.0 = Debug|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.ActiveCfg = Release|Win32 + {C2302635-D489-4678-96B4-70F5309DCBE6}.Release.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + EndGlobalSection + GlobalSection(ExtensibilityAddIns) = postSolution + EndGlobalSection +EndGlobal diff --git a/trunk/Examples/csharp/class/runme.cs b/trunk/Examples/csharp/class/runme.cs new file mode 100644 index 000000000..9088031d6 --- /dev/null +++ b/trunk/Examples/csharp/class/runme.cs @@ -0,0 +1,66 @@ +// This example illustrates how C++ classes can be used from C# using SWIG. +// The C# class gets mapped onto the C++ class and behaves as if it is a C# class. + +using System; + +public class runme +{ + static void Main() + { + // ----- Object creation ----- + + Console.WriteLine( "Creating some objects:" ); + + using (Square s = new Square(10)) + using (Circle c = new Circle(10)) + { + Console.WriteLine( " Created circle " + c ); + Console.WriteLine( " Created square " + s ); + + // ----- Access a static member ----- + + Console.WriteLine( "\nA total of " + Shape.nshapes + " shapes were created" ); + + // ----- Member data access ----- + + // Notice how we can do this using functions specific to + // the 'Circle' class. + c.x = 20; + c.y = 30; + + // Now use the same functions in the base class + Shape shape = s; + shape.x = -10; + shape.y = 5; + + Console.WriteLine( "\nHere is their current position:" ); + Console.WriteLine( " Circle = (" + c.x + " " + c.y + ")" ); + Console.WriteLine( " Square = (" + s.x + " " + s.y + ")" ); + + // ----- Call some methods ----- + + Console.WriteLine( "\nHere are some properties of the shapes:" ); + Shape[] shapes = {c,s}; + // for (int i=0; i