diff options
author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2009-08-18 20:56:02 +0000 |
---|---|---|
committer | Lorry <lorry@roadtrain.codethink.co.uk> | 2012-09-25 16:59:08 +0000 |
commit | 9f8a09ed743cedd9547bf0661d518647966ab114 (patch) | |
tree | 9c7803d3b27a8ec22e91792ac7f7932efa128b20 /Examples/csharp/extend/runme.cs | |
download | swig-tarball-master.tar.gz |
Imported from /srv/lorry/lorry-area/swig-tarball/swig-1.3.40.tar.gz.HEADswig-1.3.40master
Diffstat (limited to 'Examples/csharp/extend/runme.cs')
-rw-r--r-- | Examples/csharp/extend/runme.cs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/Examples/csharp/extend/runme.cs b/Examples/csharp/extend/runme.cs new file mode 100644 index 0000000..825dcdb --- /dev/null +++ b/Examples/csharp/extend/runme.cs @@ -0,0 +1,77 @@ +// This file illustrates the cross language polymorphism using directors. + +using System; + +// CEO class, which overrides Employee::getPosition(). + +class CEO : Manager { + public CEO(String name) : base(name) { + } + public override String getPosition() { + return "CEO"; + } + // Public method to stop the SWIG proxy base class from thinking it owns the underlying C++ memory. + public void disownMemory() { + swigCMemOwn = false; + } +} + + +public class runme +{ + static void Main() + { + // Create an instance of CEO, a class derived from the C# proxy of the + // underlying C++ class. The calls to getName() and getPosition() are standard, + // the call to getTitle() uses the director wrappers to call CEO.getPosition(). + + CEO e = new CEO("Alice"); + Console.WriteLine( e.getName() + " is a " + e.getPosition() ); + Console.WriteLine( "Just call her \"" + e.getTitle() + "\"" ); + Console.WriteLine( "----------------------" ); + + // Create a new EmployeeList instance. This class does not have a C++ + // director wrapper, but can be used freely with other classes that do. + + using (EmployeeList list = new EmployeeList()) { + + // EmployeeList owns its items, so we must surrender ownership of objects we add. + e.disownMemory(); + list.addEmployee(e); + Console.WriteLine( "----------------------" ); + + // Now we access the first four items in list (three are C++ objects that + // EmployeeList's constructor adds, the last is our CEO). The virtual + // methods of all these instances are treated the same. For items 0, 1, and + // 2, all methods resolve in C++. For item 3, our CEO, getTitle calls + // getPosition which resolves in C#. The call to getPosition is + // slightly different, however, because of the overidden getPosition() call, since + // now the object reference has been "laundered" by passing through + // EmployeeList as an Employee*. Previously, C# resolved the call + // immediately in CEO, but now C# thinks the object is an instance of + // class Employee. So the call passes through the + // Employee proxy class and on to the C wrappers and C++ director, + // eventually ending up back at the C# CEO implementation of getPosition(). + // The call to getTitle() for item 3 runs the C++ Employee::getTitle() + // method, which in turn calls getPosition(). This virtual method call + // passes down through the C++ director class to the C# implementation + // in CEO. All this routing takes place transparently. + + Console.WriteLine( "(position, title) for items 0-3:" ); + + Console.WriteLine( " " + list.get_item(0).getPosition() + ", \"" + list.get_item(0).getTitle() + "\"" ); + Console.WriteLine( " " + list.get_item(1).getPosition() + ", \"" + list.get_item(1).getTitle() + "\"" ); + Console.WriteLine( " " + list.get_item(2).getPosition() + ", \"" + list.get_item(2).getTitle() + "\"" ); + Console.WriteLine( " " + list.get_item(3).getPosition() + ", \"" + list.get_item(3).getTitle() + "\"" ); + Console.WriteLine( "----------------------" ); + + // The using statement ensures the EmployeeList.Dispose() will be called, which will delete all the Employee* + // items it contains. The last item is our CEO, which gets destroyed as well. + } + Console.WriteLine( "----------------------" ); + + // All done. + + Console.WriteLine( "C# exit" ); + } +} |