diff options
Diffstat (limited to 'Examples/csharp/reference')
-rw-r--r-- | Examples/csharp/reference/Makefile | 20 | ||||
-rw-r--r-- | Examples/csharp/reference/example-cs.csproj | 99 | ||||
-rw-r--r-- | Examples/csharp/reference/example-vc.vcproj | 158 | ||||
-rw-r--r-- | Examples/csharp/reference/example.cxx | 46 | ||||
-rw-r--r-- | Examples/csharp/reference/example.h | 26 | ||||
-rw-r--r-- | Examples/csharp/reference/example.i | 46 | ||||
-rw-r--r-- | Examples/csharp/reference/example.sln | 30 | ||||
-rw-r--r-- | Examples/csharp/reference/runme.cs | 73 |
8 files changed, 498 insertions, 0 deletions
diff --git a/Examples/csharp/reference/Makefile b/Examples/csharp/reference/Makefile new file mode 100644 index 0000000..20f0dd5 --- /dev/null +++ b/Examples/csharp/reference/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/Examples/csharp/reference/example-cs.csproj b/Examples/csharp/reference/example-cs.csproj new file mode 100644 index 0000000..e55912b --- /dev/null +++ b/Examples/csharp/reference/example-cs.csproj @@ -0,0 +1,99 @@ +<VisualStudioProject> + <CSHARP + ProjectType = "Local" + ProductVersion = "7.10.3077" + SchemaVersion = "2.0" + ProjectGuid = "{C17D27DF-4C57-4625-AEE0-A40C4F48FF1A}" + > + <Build> + <Settings + ApplicationIcon = "" + AssemblyKeyContainerName = "" + AssemblyName = "runme" + AssemblyOriginatorKeyFile = "" + DefaultClientScript = "JScript" + DefaultHTMLPageLayout = "Grid" + DefaultTargetSchema = "IE50" + DelaySign = "false" + OutputType = "Exe" + PreBuildEvent = "" + PostBuildEvent = "" + RootNamespace = "runme" + RunPostBuildEvent = "OnBuildSuccess" + StartupObject = "" + > + <Config + Name = "Debug" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "DEBUG;TRACE" + DocumentationFile = "" + DebugSymbols = "true" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "false" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + <Config + Name = "Release" + AllowUnsafeBlocks = "false" + BaseAddress = "285212672" + CheckForOverflowUnderflow = "false" + ConfigurationOverrideFile = "" + DefineConstants = "TRACE" + DocumentationFile = "" + DebugSymbols = "false" + FileAlignment = "4096" + IncrementalBuild = "false" + NoStdLib = "false" + NoWarn = "" + Optimize = "true" + OutputPath = ".\" + RegisterForComInterop = "false" + RemoveIntegerChecks = "false" + TreatWarningsAsErrors = "false" + WarningLevel = "4" + /> + </Settings> + <References/> + </Build> + <Files> + <Include> + <File + RelPath = "example.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "examplePINVOKE.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "runme.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Vector.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "VectorArray.cs" + SubType = "Code" + BuildAction = "Compile" + /> + </Include> + </Files> + </CSHARP> +</VisualStudioProject> + diff --git a/Examples/csharp/reference/example-vc.vcproj b/Examples/csharp/reference/example-vc.vcproj new file mode 100644 index 0000000..7c0b8ef --- /dev/null +++ b/Examples/csharp/reference/example-vc.vcproj @@ -0,0 +1,158 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="7.10" + Name="example-vc" + ProjectGUID="{C2302635-D489-4678-96B4-70F5309DCBE6}" + Keyword="Win32Proj"> + <Platforms> + <Platform + Name="Win32"/> + </Platforms> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="Debug" + IntermediateDirectory="Debug" + ConfigurationType="2" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + MinimalRebuild="TRUE" + BasicRuntimeChecks="3" + RuntimeLibrary="1" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="4"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="2" + GenerateDebugInformation="TRUE" + ProgramDatabaseFile="$(OutDir)/example-vc.pdb" + SubSystem="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory="Release" + IntermediateDirectory="Release" + ConfigurationType="2" + CharacterSet="2"> + <Tool + Name="VCCLCompilerTool" + PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;EXAMPLEVC_EXPORTS" + RuntimeLibrary="0" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="TRUE" + DebugInformationFormat="3"/> + <Tool + Name="VCCustomBuildTool"/> + <Tool + Name="VCLinkerTool" + OutputFile="example.dll" + LinkIncremental="1" + GenerateDebugInformation="TRUE" + SubSystem="2" + OptimizeReferences="2" + EnableCOMDATFolding="2" + ImportLibrary="$(OutDir)/example-vc.lib" + TargetMachine="1"/> + <Tool + Name="VCMIDLTool"/> + <Tool + Name="VCPostBuildEventTool"/> + <Tool + Name="VCPreBuildEventTool"/> + <Tool + Name="VCPreLinkEventTool"/> + <Tool + Name="VCResourceCompilerTool"/> + <Tool + Name="VCWebServiceProxyGeneratorTool"/> + <Tool + Name="VCXMLDataGeneratorTool"/> + <Tool + Name="VCWebDeploymentTool"/> + <Tool + Name="VCManagedWrapperGeneratorTool"/> + <Tool + Name="VCAuxiliaryManagedWrapperGeneratorTool"/> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Source Files" + Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" + UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"> + <File + RelativePath="example.cxx"> + </File> + <File + RelativePath="example_wrap.cxx"> + </File> + </Filter> + <Filter + Name="Header Files" + Filter="h;hpp;hxx;hm;inl;inc;xsd" + UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"> + <File + RelativePath="example.h"> + </File> + </Filter> + <File + RelativePath=".\example.i"> + <FileConfiguration + Name="Debug|Win32"> + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG... +echo on +..\..\..\swig.exe -c++ -csharp "$(InputPath)" +@echo off" + Outputs="$(InputName)_wrap.cxx"/> + </FileConfiguration> + <FileConfiguration + Name="Release|Win32"> + <Tool + Name="VCCustomBuildTool" + CommandLine="echo Invoking SWIG... +echo on +..\..\..\swig.exe -c++ -csharp "$(InputPath)" +@echo off" + Outputs="$(InputName)_wrap.cxx"/> + </FileConfiguration> + </File> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/Examples/csharp/reference/example.cxx b/Examples/csharp/reference/example.cxx new file mode 100644 index 0000000..8a513bf --- /dev/null +++ b/Examples/csharp/reference/example.cxx @@ -0,0 +1,46 @@ +/* File : example.cxx */ + +/* Deal with Microsoft's attempt at deprecating C standard runtime functions */ +#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) +# define _CRT_SECURE_NO_DEPRECATE +#endif + +#include "example.h" +#include <stdio.h> +#include <stdlib.h> + +Vector operator+(const Vector &a, const Vector &b) { + Vector r; + r.x = a.x + b.x; + r.y = a.y + b.y; + r.z = a.z + b.z; + return r; +} + +char *Vector::print() { + static char temp[512]; + sprintf(temp,"Vector %p (%g,%g,%g)", this, x,y,z); + return temp; +} + +VectorArray::VectorArray(int size) { + items = new Vector[size]; + maxsize = size; +} + +VectorArray::~VectorArray() { + delete [] items; +} + +Vector &VectorArray::operator[](int index) { + if ((index < 0) || (index >= maxsize)) { + printf("Panic! Array index out of bounds.\n"); + exit(1); + } + return items[index]; +} + +int VectorArray::size() { + return maxsize; +} + diff --git a/Examples/csharp/reference/example.h b/Examples/csharp/reference/example.h new file mode 100644 index 0000000..4915adb --- /dev/null +++ b/Examples/csharp/reference/example.h @@ -0,0 +1,26 @@ +/* File : example.h */ + +class Vector { +private: + double x,y,z; +public: + Vector() : x(0), y(0), z(0) { }; + Vector(double x, double y, double z) : x(x), y(y), z(z) { }; + friend Vector operator+(const Vector &a, const Vector &b); + char *print(); +}; + +class VectorArray { +private: + Vector *items; + int maxsize; +public: + VectorArray(int maxsize); + ~VectorArray(); + Vector &operator[](int); + int size(); +}; + + + + diff --git a/Examples/csharp/reference/example.i b/Examples/csharp/reference/example.i new file mode 100644 index 0000000..6daa3b1 --- /dev/null +++ b/Examples/csharp/reference/example.i @@ -0,0 +1,46 @@ +/* File : example.i */ + +/* This file has a few "typical" uses of C++ references. */ + +%module example + +%{ +#include "example.h" +%} + +class Vector { +public: + Vector(double x, double y, double z); + ~Vector(); + char *print(); +}; + +/* This helper function calls an overloaded operator */ +%inline %{ +Vector addv(Vector &a, Vector &b) { + return a+b; +} +%} + +/* Wrapper around an array of vectors class */ + +class VectorArray { +public: + VectorArray(int maxsize); + ~VectorArray(); + int size(); + + /* This wrapper provides an alternative to the [] operator */ + %extend { + Vector &get(int index) { + return (*$self)[index]; + } + void set(int index, Vector &a) { + (*$self)[index] = a; + } + } +}; + + + + diff --git a/Examples/csharp/reference/example.sln b/Examples/csharp/reference/example.sln new file mode 100644 index 0000000..28b9851 --- /dev/null +++ b/Examples/csharp/reference/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/Examples/csharp/reference/runme.cs b/Examples/csharp/reference/runme.cs new file mode 100644 index 0000000..499ddea --- /dev/null +++ b/Examples/csharp/reference/runme.cs @@ -0,0 +1,73 @@ +// This example illustrates the manipulation of C++ references in C#. + +using System; + +public class runme { + + public static void Main() + { + Console.WriteLine( "Creating some objects:" ); + Vector a = new Vector(3,4,5); + Vector b = new Vector(10,11,12); + + Console.WriteLine( " Created " + a.print() ); + Console.WriteLine( " Created " + b.print() ); + + // ----- Call an overloaded operator ----- + + // This calls the wrapper we placed around + // + // operator+(const Vector &a, const Vector &) + // + // It returns a new allocated object. + + Console.WriteLine( "Adding a+b" ); + Vector c = example.addv(a,b); + Console.WriteLine( " a+b = " + c.print() ); + + // Note: Unless we free the result, a memory leak will occur if the -noproxy commandline + // is used as the proxy classes define finalizers which call the Dispose() method. When + // -noproxy is not specified the memory management is controlled by the garbage collector. + // You can still call Dispose(). It will free the c++ memory immediately, but not the + // C# memory! You then must be careful not to call any member functions as it will + // use a NULL c pointer on the underlying c++ object. We set the C# object to null + // which will then throw a C# exception should we attempt to use it again. + c.Dispose(); + c = null; + + // ----- Create a vector array ----- + + Console.WriteLine( "Creating an array of vectors" ); + VectorArray va = new VectorArray(10); + Console.WriteLine( " va = " + va.ToString() ); + + // ----- Set some values in the array ----- + + // These operators copy the value of Vector a and Vector b to the vector array + va.set(0,a); + va.set(1,b); + + // This works, but it would cause a memory leak if -noproxy was used! + + va.set(2,example.addv(a,b)); + + + // Get some values from the array + + Console.WriteLine( "Getting some array values" ); + for (int i=0; i<5; i++) + Console.WriteLine( " va(" + i + ") = " + va.get(i).print() ); + + // Watch under resource meter to check on this + Console.WriteLine( "Making sure we don't leak memory." ); + for (int i=0; i<1000000; i++) + c = va.get(i%10); + + // ----- Clean up ----- + // This could be omitted. The garbage collector would then clean up for us. + Console.WriteLine( "Cleaning up" ); + va.Dispose(); + a.Dispose(); + b.Dispose(); + } +} |