New test.
[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 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Runtime.CompilerServices;
34 using System.Runtime.InteropServices;
35
36 namespace System.Runtime.InteropServices
37 {
38
39 #if NET_2_0
40         [ComVisible(true)]
41 #endif
42         [MonoTODO("Struct should be [StructLayout(LayoutKind.Sequential)] but will need to be reordered for that.")]
43         public struct GCHandle 
44         {
45                 // fields
46                 private int handle;
47
48                 private GCHandle(IntPtr h)
49                 {
50                         handle = (int)h;
51                 }
52                 
53                 // Constructors
54                 private GCHandle(object obj)
55                         : this(obj, GCHandleType.Normal)
56                 {}
57
58                 private GCHandle(object value, GCHandleType type)
59                 {
60                         handle = GetTargetHandle (value, 0, type);
61                 }
62
63                 // Properties
64
65                 public bool IsAllocated 
66                 { 
67                         get
68                         {
69                                 return (handle != 0);
70                         }
71                 }
72
73                 public object Target
74                 { 
75                         get
76                         {
77                                 return GetTarget (handle);
78                         } 
79                         set
80                         {
81                                 handle = GetTargetHandle (value, handle, (GCHandleType)(-1));
82                         } 
83                 }
84
85                 // Methods
86                 public IntPtr AddrOfPinnedObject()
87                 {
88                         IntPtr res = GetAddrOfPinnedObject(handle);
89                         if (res == IntPtr.Zero)
90                                 throw new InvalidOperationException("The handle is not of Pinned type");
91                         if (res == (IntPtr)(-1))
92                                 throw new ArgumentException ("Object contains non-primitive or non-blittable data.");
93                         return res;
94                 }
95
96                 public static System.Runtime.InteropServices.GCHandle Alloc(object value)
97                 {
98                         return new GCHandle (value);
99                 }
100
101                 public static System.Runtime.InteropServices.GCHandle Alloc(object value, GCHandleType type)
102                 {
103                         return new GCHandle (value,type);
104                 }
105
106                 public void Free()
107                 {
108                         FreeHandle(handle);
109                         handle = 0;
110                 }
111                 
112                 public static explicit operator IntPtr (GCHandle value)
113                 {
114                         return (IntPtr) value.handle;
115                 }
116                 
117                 public static explicit operator GCHandle(IntPtr value)
118                 {
119                         if (value == IntPtr.Zero)
120                                 throw new ArgumentException ("GCHandle value cannot be zero");
121                         if (!CheckCurrentDomain ((int)value))
122                                 throw new ArgumentException ("GCHandle value belongs to a different domain");
123                         return new GCHandle (value);
124                 }
125
126                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
127                 private extern static bool CheckCurrentDomain (int handle);
128                 
129                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
130                 private extern static object GetTarget(int handle);
131
132                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
133                 private extern static int GetTargetHandle(object obj, int handle, GCHandleType type);
134                 
135                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
136                 private extern static void FreeHandle(int handle);
137                 
138                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
139                 private extern static IntPtr GetAddrOfPinnedObject(int handle);
140
141 #if NET_2_0
142                 public static bool operator ==(GCHandle a, GCHandle b)
143                 {
144                         return a.Equals(b);
145                 }
146
147                 public static bool operator !=(GCHandle a, GCHandle b)
148                 {
149                         return (!(a.Equals(b)));
150                 }
151                 
152                 public override bool Equals(object o)
153                 {
154                         if (o == null || !(o is GCHandle))
155                                 return false;
156
157                         return (handle == ((GCHandle)o).handle);
158                 }
159
160                 public override int GetHashCode()
161                 {
162                         return handle.GetHashCode ();
163                 }
164
165                 public static GCHandle FromIntPtr (IntPtr value)
166                 {
167                         return (GCHandle)value;
168                 }
169
170                 public static IntPtr ToIntPtr (GCHandle value)
171                 {
172                         return (IntPtr)value;
173                 }
174 #endif
175         } 
176 }
177