copying the latest Sys.Web.Services from trunk.
[mono.git] / mcs / class / corlib / System.Threading / CompressedStack.cs
1 //
2 // System.Threading.Thread.cs
3 //
4 // Authors:
5 //      Zoltan Varga (vargaz@freemail.hu)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Collections;
31 using System.Diagnostics;
32 using System.Reflection;
33 using System.Runtime.InteropServices;
34 using System.Runtime.Serialization;
35 using System.Security;
36 using System.Security.Permissions;
37
38 namespace System.Threading {
39
40 #if NET_2_0
41         [Serializable]
42         [ComVisibleAttribute (false)]
43         public sealed class CompressedStack : ISerializable {
44 #else
45         public class CompressedStack {
46 #endif
47                 private ArrayList _list;
48
49                 internal CompressedStack (int length)
50                 {
51                         if (length > 0)
52                                 _list = new ArrayList (length);
53                 }
54
55                 internal CompressedStack (CompressedStack cs)
56                 {
57                         if ((cs != null) && (cs._list != null))
58                                 _list = (ArrayList) cs._list.Clone ();
59                 }
60
61                 ~CompressedStack ()
62                 {
63                 }
64
65 #if NET_2_0
66                 [ComVisibleAttribute (false)]
67                 public CompressedStack CreateCopy ()
68                 {
69                         // FIXME: in 2.0 beta1 Object.ReferenceEquals (cs, cs.CreateCopy ()) == true !!!
70                         return this;
71 //                      return new CompressedStack (this);
72                 }
73
74                 [MonoTODO ("incomplete")]
75                 public void GetObjectData (SerializationInfo info, StreamingContext context)
76                 {
77                         if (info == null)
78                                 throw new ArgumentNullException ("info");
79                 }
80
81                 static public CompressedStack Capture ()
82                 {
83                         CompressedStack cs = new CompressedStack (0);
84                         cs._list = SecurityFrame.GetStack (1);
85                         return cs;
86                 }
87
88                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
89                 [StrongNameIdentityPermission (SecurityAction.LinkDemand, PublicKey="00000000000000000400000000000000")]
90                 static public CompressedStack GetCompressedStack ()
91                 {
92                         // Note: CompressedStack.GetCompressedStack doesn't return null
93                         // like Thread.CurrentThread.GetCompressedStack if no compressed
94                         // stack is present.
95                         return new CompressedStack (Thread.CurrentThread.GetCompressedStack ());
96                 }
97
98                 static public CompressedStackSwitcher SetCompressedStack (CompressedStack cs)
99                 {
100                         Thread t = Thread.CurrentThread;
101                         CompressedStack ctcs = t.GetCompressedStack ();
102                         if (ctcs != null) {
103                                 string msg = Locale.GetText ("You must Undo previous CompressedStack.");
104                                 throw new SecurityException (msg);
105                         }
106                         CompressedStackSwitcher csw = new CompressedStackSwitcher (ctcs, t);
107                         t.SetCompressedStack (cs);
108                         return csw;
109                 }
110 #endif
111                 internal bool Equals (CompressedStack cs)
112                 {
113                         if (IsEmpty ())
114                                 return cs.IsEmpty ();
115                         if (cs.IsEmpty ())
116                                 return false;
117                         if (_list.Count != cs._list.Count)
118                                 return false;
119
120                         for (int i=0; i < _list.Count; i++) {
121                                 SecurityFrame sf1 = (SecurityFrame) _list [i];
122                                 SecurityFrame sf2 = (SecurityFrame) cs._list [i];
123                                 if (!sf1.Equals (sf2))
124                                         return false;
125                         }
126                         return true;
127                 }
128
129                 internal bool IsEmpty ()
130                 {
131                         return ((_list == null) || (_list.Count == 0));
132                 }
133
134                 internal IList List {
135                         get { return _list; }
136                 }
137         }
138 }