CompressedStack.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // System.Threading.Thread.cs
  3. //
  4. // Authors:
  5. // Zoltan Varga ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2004 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. using System.Collections;
  30. using System.Diagnostics;
  31. using System.Reflection;
  32. using System.Runtime.InteropServices;
  33. using System.Runtime.Serialization;
  34. using System.Security;
  35. namespace System.Threading {
  36. #if NET_2_0
  37. [Serializable]
  38. [ComVisibleAttribute (false)]
  39. public sealed class CompressedStack : ISerializable {
  40. #else
  41. public class CompressedStack {
  42. #endif
  43. private ArrayList _list;
  44. internal CompressedStack (int length)
  45. {
  46. if (length > 0)
  47. _list = new ArrayList (length);
  48. }
  49. internal CompressedStack (CompressedStack cs)
  50. {
  51. if ((cs != null) && (cs._list != null))
  52. _list = (ArrayList) cs._list.Clone ();
  53. }
  54. ~CompressedStack ()
  55. {
  56. }
  57. #if NET_2_0
  58. [ComVisibleAttribute (false)]
  59. public CompressedStack CreateCopy ()
  60. {
  61. // in 2.0 beta1 Object.ReferenceEquals (cs, cs.CreateCopy ()) == true !!!
  62. return this;
  63. // return new CompressedStack (this);
  64. }
  65. [MonoTODO ("incomplete")]
  66. public void GetObjectData (SerializationInfo info, StreamingContext context)
  67. {
  68. if (info == null)
  69. throw new ArgumentNullException ("info");
  70. }
  71. static public CompressedStack Capture ()
  72. {
  73. CompressedStack cs = new CompressedStack (0);
  74. cs._list = SecurityFrame.GetStack (1);
  75. return cs;
  76. }
  77. static public CompressedStack GetCompressedStack ()
  78. {
  79. // Note: CompressedStack.GetCompressedStack doesn't return null
  80. // like Thread.CurrentThread.GetCompressedStack if no compressed
  81. // stack is present.
  82. return new CompressedStack (Thread.CurrentThread.GetCompressedStack ());
  83. }
  84. static public CompressedStackSwitcher SetCompressedStack (CompressedStack cs)
  85. {
  86. Thread t = Thread.CurrentThread;
  87. CompressedStack ctcs = t.GetCompressedStack ();
  88. if (ctcs != null) {
  89. string msg = Locale.GetText ("You must Undo previous CompressedStack.");
  90. throw new SecurityException (msg);
  91. }
  92. CompressedStackSwitcher csw = new CompressedStackSwitcher (ctcs, t);
  93. t.SetCompressedStack (cs);
  94. return csw;
  95. }
  96. #endif
  97. internal bool Equals (CompressedStack cs)
  98. {
  99. if (IsEmpty ())
  100. return cs.IsEmpty ();
  101. if (cs.IsEmpty ())
  102. return false;
  103. if (_list.Count != cs._list.Count)
  104. return false;
  105. for (int i=0; i < _list.Count; i++) {
  106. SecurityFrame sf1 = (SecurityFrame) _list [i];
  107. SecurityFrame sf2 = (SecurityFrame) cs._list [i];
  108. if (!sf1.Equals (sf2))
  109. return false;
  110. }
  111. return true;
  112. }
  113. internal bool IsEmpty ()
  114. {
  115. return ((_list == null) || (_list.Count == 0));
  116. }
  117. internal IList List {
  118. get { return _list; }
  119. }
  120. }
  121. }