import source from 1.3.40
[external/swig.git] / Examples / csharp / extend / runme.cs
1 // This file illustrates the cross language polymorphism using directors.
2
3 using System;
4
5 // CEO class, which overrides Employee::getPosition().
6
7 class CEO : Manager {
8   public CEO(String name) : base(name) {
9   }
10   public override String getPosition() {
11     return "CEO";
12   }
13   // Public method to stop the SWIG proxy base class from thinking it owns the underlying C++ memory.
14   public void disownMemory() {
15     swigCMemOwn = false; 
16   } 
17 }
18
19
20 public class runme
21 {
22   static void Main() 
23   {
24     // Create an instance of CEO, a class derived from the C# proxy of the 
25     // underlying C++ class. The calls to getName() and getPosition() are standard,
26     // the call to getTitle() uses the director wrappers to call CEO.getPosition().
27
28     CEO e = new CEO("Alice");
29     Console.WriteLine( e.getName() + " is a " + e.getPosition() );
30     Console.WriteLine( "Just call her \"" + e.getTitle() + "\"" );
31     Console.WriteLine( "----------------------" );
32
33     // Create a new EmployeeList instance.  This class does not have a C++
34     // director wrapper, but can be used freely with other classes that do.
35
36     using (EmployeeList list = new EmployeeList()) {
37
38     // EmployeeList owns its items, so we must surrender ownership of objects we add.
39     e.disownMemory();
40     list.addEmployee(e);
41     Console.WriteLine( "----------------------" );
42
43     // Now we access the first four items in list (three are C++ objects that
44     // EmployeeList's constructor adds, the last is our CEO). The virtual
45     // methods of all these instances are treated the same. For items 0, 1, and
46     // 2, all methods resolve in C++. For item 3, our CEO, getTitle calls
47     // getPosition which resolves in C#. The call to getPosition is
48     // slightly different, however, because of the overidden getPosition() call, since
49     // now the object reference has been "laundered" by passing through
50     // EmployeeList as an Employee*. Previously, C# resolved the call
51     // immediately in CEO, but now C# thinks the object is an instance of
52     // class Employee. So the call passes through the
53     // Employee proxy class and on to the C wrappers and C++ director,
54     // eventually ending up back at the C# CEO implementation of getPosition().
55     // The call to getTitle() for item 3 runs the C++ Employee::getTitle()
56     // method, which in turn calls getPosition(). This virtual method call
57     // passes down through the C++ director class to the C# implementation
58     // in CEO. All this routing takes place transparently.
59
60     Console.WriteLine( "(position, title) for items 0-3:" );
61
62     Console.WriteLine( "  " + list.get_item(0).getPosition() + ", \"" + list.get_item(0).getTitle() + "\"" );
63     Console.WriteLine( "  " + list.get_item(1).getPosition() + ", \"" + list.get_item(1).getTitle() + "\"" );
64     Console.WriteLine( "  " + list.get_item(2).getPosition() + ", \"" + list.get_item(2).getTitle() + "\"" );
65     Console.WriteLine( "  " + list.get_item(3).getPosition() + ", \"" + list.get_item(3).getTitle() + "\"" );
66     Console.WriteLine( "----------------------" );
67
68     // The using statement ensures the EmployeeList.Dispose() will be called, which will delete all the Employee*
69     // items it contains. The last item is our CEO, which gets destroyed as well.
70     }
71     Console.WriteLine( "----------------------" );
72
73     // All done.
74
75     Console.WriteLine( "C# exit" );
76   }
77 }