Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / C5 / ViewSupport.cs @ 3

1
/*
2
 Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
3
 Permission is hereby granted, free of charge, to any person obtaining a copy
4
 of this software and associated documentation files (the "Software"), to deal
5
 in the Software without restriction, including without limitation the rights
6
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
 copies of the Software, and to permit persons to whom the Software is
8
 furnished to do so, subject to the following conditions:
9
 
10
 The above copyright notice and this permission notice shall be included in
11
 all copies or substantial portions of the Software.
12
 
13
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
 SOFTWARE.
20
*/
21

    
22
using System;
23
using System.Diagnostics;
24
using SCG = System.Collections.Generic;
25

    
26
namespace C5
27
{
28
  /// <summary>
29
  /// Characterize the mutual position of some view B (other) relative to view A (this)
30
  /// </summary>
31
  enum MutualViewPosition
32
  {
33
    /// <summary>
34
    /// B contains A(this)
35
    /// </summary>
36
    Contains,
37
    /// <summary>
38
    /// B is containd in A(this), but not vice versa
39
    /// </summary>
40
    ContainedIn,
41
    /// <summary>
42
    /// A and B does not overlap
43
    /// </summary>
44
    NonOverlapping,
45
    /// <summary>
46
    /// A and B overlap, but neither is contained in the other
47
    /// </summary>
48
    Overlapping
49
  }
50

    
51
  #region View List Nested class
52
  /// <summary>
53
  /// This class is shared between the linked list and array list implementations.
54
  /// </summary>
55
  /// <typeparam name="V"></typeparam>
56
  [Serializable]
57
  class WeakViewList<V> where V : class
58
  {
59
    Node start;
60
    [Serializable]
61
    internal class Node
62
    {
63
      internal WeakReference weakview; internal Node prev, next;
64
      internal Node(V view) { weakview = new WeakReference(view); }
65
    }
66
    internal Node Add(V view)
67
    {
68
      Node newNode = new Node(view);
69
      if (start != null) { start.prev = newNode; newNode.next = start; }
70
      start = newNode;
71
      return newNode;
72
    }
73
    internal void Remove(Node n)
74
    {
75
      if (n == start) { start = start.next; if (start != null) start.prev = null; }
76
      else { n.prev.next = n.next; if (n.next != null) n.next.prev = n.prev; }
77
    }
78
    /// <summary>
79
    /// Note that it is safe to call views.Remove(view.myWeakReference) if view
80
    /// is the currently yielded object
81
    /// </summary>
82
    /// <returns></returns>
83
    public SCG.IEnumerator<V> GetEnumerator()
84
    {
85
      Node n = start;
86
      while (n != null)
87
      {
88
        //V view = n.weakview.Target as V; //This provokes a bug in the beta1 verifyer
89
        object o = n.weakview.Target;
90
        V view = o is V ? (V)o : null;
91
        if (view == null)
92
          Remove(n);
93
        else
94
          yield return view;
95
        n = n.next;
96
      }
97
    }
98
  }
99

    
100
  #endregion
101
}