2002-02-26 Duncan Mak <duncan@ximian.com>
[mono.git] / mcs / class / corlib / System.Runtime.InteropServices / GCHandle.cs
1 using System;
2 using System.Runtime.CompilerServices;
3 using System.Runtime.InteropServices;
4 namespace System.Runtime.InteropServices
5 {
6         /// <summary>
7         /// Summary description for GCHandle.
8         /// </summary>
9         public struct GCHandle 
10         {
11                 // fields
12                 private IntPtr handle;
13                 private GCHandleType handleType;
14
15                 // Constructors
16                 private GCHandle(object obj)
17                         : this(obj, GCHandleType.Normal)
18                 {}
19
20                 private GCHandle(object value, GCHandleType type)
21                 {
22                         handle = IntPtr.Zero;
23                         handleType = type;
24                 }
25
26                 // Properties
27
28                 public bool IsAllocated 
29                 { 
30                         get
31                         {
32                                 return (handle != IntPtr.Zero);
33                         }
34                 }
35
36                 public object Target
37                 { 
38                         get
39                         {
40                                 return GetTarget(handle);
41                         } 
42                         set
43                         {
44                                 SetTarget(handle,value);
45                         } 
46                 }
47
48                 // Methods
49                 public IntPtr AddrOfPinnedObject()
50                 {
51                         if(this.handleType == System.Runtime.InteropServices.GCHandleType.Pinned)
52                         {
53                                 throw new InvalidOperationException("The handle is not of Pinned type");
54                         }
55                         return GetAddrOfPinnedObject();
56                 }
57
58                 public static System.Runtime.InteropServices.GCHandle Alloc(object value)
59                 {
60                         return new GCHandle(value);
61                 }
62
63                 public static System.Runtime.InteropServices.GCHandle Alloc(object value, GCHandleType type)
64                 {
65                         return new GCHandle(value,type);
66                 }
67
68                 public void Free()
69                 {
70                         FreeHandle(handle);
71                         handle = IntPtr.Zero;
72                 }
73                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
74                 public extern static explicit operator IntPtr(GCHandle value);
75                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
76                 public extern static explicit operator GCHandle(IntPtr value);
77
78                 //TODO: Private Native Functions
79                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
80                 private extern object GetTarget(IntPtr pointer);
81
82                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
83                 private extern void SetTarget(IntPtr pointer,object obj);
84                 
85                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
86                 private extern void FreeHandle(IntPtr pointer);
87                 
88                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
89                 private extern IntPtr GetAddrOfPinnedObject();
90         } 
91 }