Initial commit
[mono.git] / mcs / class / referencesource / mscorlib / microsoft / win32 / safehandles / win32safehandles.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 //
7 // Abstract derivations of SafeHandle designed to provide the common
8 // functionality supporting Win32 handles. More specifically, they describe how
9 // an invalid handle looks (for instance, some handles use -1 as an invalid
10 // handle value, others use 0).
11 //
12 // Further derivations of these classes can specialise this even further (e.g.
13 // file or registry handles).
14 // 
15 //
16
17 namespace Microsoft.Win32.SafeHandles
18 {
19     using System;
20     using System.Runtime.InteropServices;
21     using System.Runtime.CompilerServices;
22     using System.Security.Permissions;
23     using System.Runtime.ConstrainedExecution;
24
25     // Class of safe handle which uses 0 or -1 as an invalid handle.
26     [System.Security.SecurityCritical]  // auto-generated_required
27 #if !FEATURE_CORECLR
28     [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode=true)]
29 #endif
30     public abstract class SafeHandleZeroOrMinusOneIsInvalid : SafeHandle
31     {
32         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
33         protected SafeHandleZeroOrMinusOneIsInvalid(bool ownsHandle) : base(IntPtr.Zero, ownsHandle) 
34         {
35         }
36
37 #if FEATURE_CORECLR
38         // A default constructor is needed to satisfy CoreCLR inheritence rules. It should not be called at runtime
39         protected SafeHandleZeroOrMinusOneIsInvalid()
40         {
41             throw new NotImplementedException();
42         }
43 #endif // FEATURE_CORECLR
44
45         public override bool IsInvalid {
46             [System.Security.SecurityCritical]
47             get { return handle.IsNull() || handle == new IntPtr(-1); }
48         }
49     }
50
51     // Class of safe handle which uses only -1 as an invalid handle.
52     [System.Security.SecurityCritical]  // auto-generated_required
53 #if !FEATURE_CORECLR
54     [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode=true)]
55 #endif
56     public abstract class SafeHandleMinusOneIsInvalid : SafeHandle
57     {
58         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
59         protected SafeHandleMinusOneIsInvalid(bool ownsHandle) : base(new IntPtr(-1), ownsHandle) 
60         {
61         }
62
63 #if FEATURE_CORECLR
64         // A default constructor is needed to satisfy CoreCLR inheritence rules. It should not be called at runtime
65         protected SafeHandleMinusOneIsInvalid()
66         {
67             throw new NotImplementedException();
68         }
69 #endif // FEATURE_CORECLR
70
71         public override bool IsInvalid {
72             [System.Security.SecurityCritical]
73             get { return handle == new IntPtr(-1); }
74         }
75     }
76
77     // Class of critical handle which uses 0 or -1 as an invalid handle.
78     [System.Security.SecurityCritical]  // auto-generated_required
79 #if !FEATURE_CORECLR
80     [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode=true)]
81 #endif
82     public abstract class CriticalHandleZeroOrMinusOneIsInvalid : CriticalHandle
83     {
84         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
85         protected CriticalHandleZeroOrMinusOneIsInvalid() : base(IntPtr.Zero) 
86         {
87         }
88
89         public override bool IsInvalid {
90             [System.Security.SecurityCritical]
91             get { return handle.IsNull() || handle == new IntPtr(-1); }
92         }
93     }
94
95     // Class of critical handle which uses only -1 as an invalid handle.
96     [System.Security.SecurityCritical]  // auto-generated_required
97 #if !FEATURE_CORECLR
98     [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode=true)]
99 #endif
100     public abstract class CriticalHandleMinusOneIsInvalid : CriticalHandle
101     {
102         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
103         protected CriticalHandleMinusOneIsInvalid() : base(new IntPtr(-1)) 
104         {
105         }
106
107         public override bool IsInvalid {
108             [System.Security.SecurityCritical]
109             get { return handle == new IntPtr(-1); }
110         }
111     }
112
113 }