Debian Codegraph @

mcs/tools/cil-strip/Mono.Cecil/SecurityDeclarationCollection.cs

raw
1 //
2 // SecurityDeclarationCollection.cs
3 //
4 // Author:
5 // Jb Evain ([email protected])
6 //
7 // (C) 2005 Jb Evain
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 namespace Mono.Cecil {
30
31 using System;
32 using System.Collections;
33
34 internal sealed class SecurityDeclarationCollection : IReflectionVisitable, IEnumerable {
35
36 IDictionary m_items;
37 IHasSecurity m_container;
38
39 public SecurityDeclaration this [int index] {
40 get { return m_items [index] as SecurityDeclaration; }
41 set { m_items [index] = value; }
42 }
43
44 public SecurityDeclaration this [SecurityAction action] {
45 get { return m_items [action] as SecurityDeclaration; }
46 set { m_items [action] = value; }
47 }
48
49 public IHasSecurity Container {
50 get { return m_container; }
51 }
52
53 public int Count {
54 get { return m_items.Count; }
55 }
56
57 public bool IsSynchronized {
58 get { return false; }
59 }
60
61 public object SyncRoot {
62 get { return this; }
63 }
64
65 public SecurityDeclarationCollection (IHasSecurity container)
66 {
67 m_container = container;
68 m_items = new Hashtable ();
69 }
70
71 public void Add (SecurityDeclaration value)
72 {
73 if (value == null)
74 throw new ArgumentNullException ("value");
75
76 // Each action can only be added once so...
77 SecurityDeclaration current = (SecurityDeclaration) m_items[value.Action];
78 if (current != null) {
79 // ... further additions are transformed into unions
80 #if !CF_1_0 && !CF_2_0
81 current.PermissionSet = current.PermissionSet.Union (value.PermissionSet);
82 #endif
83 } else {
84 m_items.Add (value.Action, value);
85 SetHasSecurity (true);
86 }
87 }
88
89 public void Clear ()
90 {
91 m_items.Clear ();
92 SetHasSecurity (false);
93 }
94
95 public bool Contains (SecurityAction action)
96 {
97 return (m_items [action] != null);
98 }
99
100 public bool Contains (SecurityDeclaration value)
101 {
102 if (value == null)
103 return (m_items.Count == 0);
104
105 SecurityDeclaration item = (SecurityDeclaration) m_items[value.Action];
106 if (item == null)
107 return false;
108
109 #if !CF_1_0 && !CF_2_0
110 return value.PermissionSet.IsSubsetOf (item.PermissionSet);
111 #else
112 // XXX For CF, this concept does not exist--so always be true
113 return true;
114 #endif
115 }
116
117 public void Remove (SecurityAction action)
118 {
119 m_items.Remove (action);
120 SetHasSecurity (this.Count > 0);
121 }
122
123 public void CopyTo (Array ary, int index)
124 {
125 m_items.Values.CopyTo (ary, index);
126 }
127
128 public IEnumerator GetEnumerator ()
129 {
130 return m_items.Values.GetEnumerator ();
131 }
132
133 public void Accept (IReflectionVisitor visitor)
134 {
135 visitor.VisitSecurityDeclarationCollection (this);
136 }
137
138 private void SetHasSecurity (bool value)
139 {
140 TypeDefinition td = (m_container as TypeDefinition);
141 if (td != null) {
142 if (value)
143 td.Attributes |= TypeAttributes.HasSecurity;
144 else
145 td.Attributes &= ~TypeAttributes.HasSecurity;
146 return;
147 }
148 MethodDefinition md = (m_container as MethodDefinition);
149 if (md != null) {
150 if (value)
151 md.Attributes |= MethodAttributes.HasSecurity;
152 else
153 md.Attributes &= ~MethodAttributes.HasSecurity;
154 }
155 }
156 }
157 }