PageRenderTime 25ms CodeModel.GetById 16ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/csharp/reference/runme.cs

#
C# | 73 lines | 32 code | 19 blank | 22 comment | 2 complexity | b6f462e1017f84cf4b071804be912238 MD5 | raw file
 1// This example illustrates the manipulation of C++ references in C#.
 2
 3using System;
 4
 5public class runme {
 6
 7  public static void Main() 
 8  {
 9    Console.WriteLine( "Creating some objects:" );
10    Vector a = new Vector(3,4,5);
11    Vector b = new Vector(10,11,12);
12    
13    Console.WriteLine( "    Created " + a.print() );
14    Console.WriteLine( "    Created " + b.print() );
15    
16    // ----- Call an overloaded operator -----
17    
18    // This calls the wrapper we placed around
19    //
20    //      operator+(const Vector &a, const Vector &) 
21    //
22    // It returns a new allocated object.
23    
24    Console.WriteLine( "Adding a+b" );
25    Vector c = example.addv(a,b);
26    Console.WriteLine( "    a+b = " + c.print() );
27    
28    // Note: Unless we free the result, a memory leak will occur if the -noproxy commandline
29    // is used as the proxy classes define finalizers which call the Dispose() method. When
30    // -noproxy is not specified the memory management is controlled by the garbage collector.
31    // You can still call Dispose(). It will free the c++ memory immediately, but not the 
32    // C# memory! You then must be careful not to call any member functions as it will 
33    // use a NULL c pointer on the underlying c++ object. We set the C# object to null
34    // which will then throw a C# exception should we attempt to use it again.
35    c.Dispose();
36    c = null;
37    
38    // ----- Create a vector array -----
39    
40    Console.WriteLine( "Creating an array of vectors" );
41    VectorArray va = new VectorArray(10);
42    Console.WriteLine( "    va = " + va.ToString() );
43    
44    // ----- Set some values in the array -----
45    
46    // These operators copy the value of Vector a and Vector b to the vector array
47    va.set(0,a);
48    va.set(1,b);
49    
50    // This works, but it would cause a memory leak if -noproxy was used!
51    
52    va.set(2,example.addv(a,b));
53    
54
55    // Get some values from the array
56    
57    Console.WriteLine( "Getting some array values" );
58    for (int i=0; i<5; i++)
59        Console.WriteLine( "    va(" + i + ") = " + va.get(i).print() );
60    
61    // Watch under resource meter to check on this
62    Console.WriteLine( "Making sure we don't leak memory." );
63    for (int i=0; i<1000000; i++)
64        c = va.get(i%10);
65    
66    // ----- Clean up -----
67    // This could be omitted. The garbage collector would then clean up for us.
68    Console.WriteLine( "Cleaning up" );
69    va.Dispose();
70    a.Dispose();
71    b.Dispose();
72  }
73}