2003-04-26 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System.Runtime.InteropServices / GCHandle.cs
1 //
2 // System.Runtime.InteropServices/GCHandle.cs
3 //
4 // Authors:
5 //   Ajay kumar Dwivedi (adwiv@yahoo.com) ??
6 //   Paolo Molaro (lupus@ximian.com)
7 //
8
9 using System;
10 using System.Runtime.CompilerServices;
11 using System.Runtime.InteropServices;
12
13 namespace System.Runtime.InteropServices
14 {
15         /// <summary>
16         /// Summary description for GCHandle.
17         /// </summary>
18         public struct GCHandle 
19         {
20                 // fields
21                 private int handle;
22
23                 private GCHandle(IntPtr h)
24                 {
25                         handle = (int)h;
26                 }
27                 
28                 // Constructors
29                 private GCHandle(object obj)
30                         : this(obj, GCHandleType.Normal)
31                 {}
32
33                 private GCHandle(object value, GCHandleType type)
34                 {
35                         handle = GetTargetHandle (value, 0, type);
36                 }
37
38                 // Properties
39
40                 public bool IsAllocated 
41                 { 
42                         get
43                         {
44                                 return (handle != 0);
45                         }
46                 }
47
48                 public object Target
49                 { 
50                         get
51                         {
52                                 return GetTarget (handle);
53                         } 
54                         set
55                         {
56                                 handle = GetTargetHandle (value, handle, (GCHandleType)(-1));
57                         } 
58                 }
59
60                 // Methods
61                 public IntPtr AddrOfPinnedObject()
62                 {
63                         IntPtr res = GetAddrOfPinnedObject(handle);
64                         if (res == IntPtr.Zero)
65                                 throw new InvalidOperationException("The handle is not of Pinned type");
66                         return res;
67                 }
68
69                 public static System.Runtime.InteropServices.GCHandle Alloc(object value)
70                 {
71                         return new GCHandle (value);
72                 }
73
74                 public static System.Runtime.InteropServices.GCHandle Alloc(object value, GCHandleType type)
75                 {
76                         return new GCHandle (value,type);
77                 }
78
79                 public void Free()
80                 {
81                         FreeHandle(handle);
82                         handle = 0;
83                 }
84                 
85                 public static explicit operator IntPtr (GCHandle value)
86                 {
87                         return (IntPtr) value.handle;
88                 }
89                 
90                 public static explicit operator GCHandle(IntPtr value)
91                 {
92                         return new GCHandle (value);
93                 }
94
95                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
96                 private extern static object GetTarget(int handle);
97
98                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
99                 private extern static int GetTargetHandle(object obj, int handle, GCHandleType type);
100                 
101                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
102                 private extern static void FreeHandle(int handle);
103                 
104                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
105                 private extern static IntPtr GetAddrOfPinnedObject(int handle);
106         } 
107 }
108