This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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         /// <summary>
39         /// Summary description for GCHandle.
40         /// </summary>
41         public struct GCHandle 
42         {
43                 // fields
44                 private int handle;
45
46                 private GCHandle(IntPtr h)
47                 {
48                         handle = (int)h;
49                 }
50                 
51                 // Constructors
52                 private GCHandle(object obj)
53                         : this(obj, GCHandleType.Normal)
54                 {}
55
56                 private GCHandle(object value, GCHandleType type)
57                 {
58                         handle = GetTargetHandle (value, 0, type);
59                 }
60
61                 // Properties
62
63                 public bool IsAllocated 
64                 { 
65                         get
66                         {
67                                 return (handle != 0);
68                         }
69                 }
70
71                 public object Target
72                 { 
73                         get
74                         {
75                                 return GetTarget (handle);
76                         } 
77                         set
78                         {
79                                 handle = GetTargetHandle (value, handle, (GCHandleType)(-1));
80                         } 
81                 }
82
83                 // Methods
84                 public IntPtr AddrOfPinnedObject()
85                 {
86                         IntPtr res = GetAddrOfPinnedObject(handle);
87                         if (res == IntPtr.Zero)
88                                 throw new InvalidOperationException("The handle is not of Pinned type");
89                         return res;
90                 }
91
92                 public static System.Runtime.InteropServices.GCHandle Alloc(object value)
93                 {
94                         return new GCHandle (value);
95                 }
96
97                 public static System.Runtime.InteropServices.GCHandle Alloc(object value, GCHandleType type)
98                 {
99                         return new GCHandle (value,type);
100                 }
101
102                 public void Free()
103                 {
104                         FreeHandle(handle);
105                         handle = 0;
106                 }
107                 
108                 public static explicit operator IntPtr (GCHandle value)
109                 {
110                         return (IntPtr) value.handle;
111                 }
112                 
113                 public static explicit operator GCHandle(IntPtr value)
114                 {
115                         return new GCHandle (value);
116                 }
117
118                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
119                 private extern static object GetTarget(int handle);
120
121                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
122                 private extern static int GetTargetHandle(object obj, int handle, GCHandleType type);
123                 
124                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
125                 private extern static void FreeHandle(int handle);
126                 
127                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
128                 private extern static IntPtr GetAddrOfPinnedObject(int handle);
129         } 
130 }
131