Bringing C5 1.0 into the main branch.
[mono.git] / mcs / class / Mono.C5 / C5 / ViewSupport.cs
diff --git a/mcs/class/Mono.C5/C5/ViewSupport.cs b/mcs/class/Mono.C5/C5/ViewSupport.cs
new file mode 100644 (file)
index 0000000..ca2a6d7
--- /dev/null
@@ -0,0 +1,101 @@
+/*\r
+ Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft\r
+ Permission is hereby granted, free of charge, to any person obtaining a copy\r
+ of this software and associated documentation files (the "Software"), to deal\r
+ in the Software without restriction, including without limitation the rights\r
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+ copies of the Software, and to permit persons to whom the Software is\r
+ furnished to do so, subject to the following conditions:\r
\r
+ The above copyright notice and this permission notice shall be included in\r
+ all copies or substantial portions of the Software.\r
\r
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
+ SOFTWARE.\r
+*/\r
+\r
+using System;\r
+using System.Diagnostics;\r
+using SCG = System.Collections.Generic;\r
+\r
+namespace C5\r
+{\r
+  /// <summary>\r
+  /// Characterize the mutual position of some view B (other) relative to view A (this)\r
+  /// </summary>\r
+  enum MutualViewPosition\r
+  {\r
+    /// <summary>\r
+    /// B contains A(this)\r
+    /// </summary>\r
+    Contains,\r
+    /// <summary>\r
+    /// B is containd in A(this), but not vice versa\r
+    /// </summary>\r
+    ContainedIn,\r
+    /// <summary>\r
+    /// A and B does not overlap\r
+    /// </summary>\r
+    NonOverlapping,\r
+    /// <summary>\r
+    /// A and B overlap, but neither is contained in the other\r
+    /// </summary>\r
+    Overlapping\r
+  }\r
+\r
+  #region View List Nested class\r
+  /// <summary>\r
+  /// This class is shared between the linked list and array list implementations.\r
+  /// </summary>\r
+  /// <typeparam name="V"></typeparam>\r
+  [Serializable]\r
+  class WeakViewList<V> where V : class\r
+  {\r
+    Node start;\r
+    [Serializable]\r
+    internal class Node\r
+    {\r
+      internal WeakReference weakview; internal Node prev, next;\r
+      internal Node(V view) { weakview = new WeakReference(view); }\r
+    }\r
+    internal Node Add(V view)\r
+    {\r
+      Node newNode = new Node(view);\r
+      if (start != null) { start.prev = newNode; newNode.next = start; }\r
+      start = newNode;\r
+      return newNode;\r
+    }\r
+    internal void Remove(Node n)\r
+    {\r
+      if (n == start) { start = start.next; if (start != null) start.prev = null; }\r
+      else { n.prev.next = n.next; if (n.next != null) n.next.prev = n.prev; }\r
+    }\r
+    /// <summary>\r
+    /// Note that it is safe to call views.Remove(view.myWeakReference) if view\r
+    /// is the currently yielded object\r
+    /// </summary>\r
+    /// <returns></returns>\r
+    public SCG.IEnumerator<V> GetEnumerator()\r
+    {\r
+      Node n = start;\r
+      while (n != null)\r
+      {\r
+        //V view = n.weakview.Target as V; //This provokes a bug in the beta1 verifyer\r
+        object o = n.weakview.Target;\r
+        V view = o is V ? (V)o : null;\r
+        if (view == null)\r
+          Remove(n);\r
+        else\r
+          yield return view;\r
+        n = n.next;\r
+      }\r
+    }\r
+  }\r
+\r
+  #endregion\r
+}
\ No newline at end of file