1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Diagnostics;
|
---|
5 |
|
---|
6 |
|
---|
7 | namespace Netron.Diagramming.Core
|
---|
8 | {
|
---|
9 | /// <summary>
|
---|
10 | /// This is a wrapper around the <see cref="Stack"/> which communicates Push and Pop events
|
---|
11 | /// </summary>
|
---|
12 | /// <typeparam name="T"></typeparam>
|
---|
13 | public class StackBase<T>
|
---|
14 | {
|
---|
15 | #region Fields
|
---|
16 | /// <summary>
|
---|
17 | /// the internal stack based on the .Net <see cref="Stack"/> implementation
|
---|
18 | /// </summary>
|
---|
19 | private Stack<T> InnerStack;
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | #region Events
|
---|
23 | /// <summary>
|
---|
24 | /// Occurs when an item is pushed onto the stack
|
---|
25 | /// </summary>
|
---|
26 | public event EventHandler<CollectionEventArgs<T>> OnItemPushed;
|
---|
27 | /// <summary>
|
---|
28 | /// Occurs when an item is popped from the stack
|
---|
29 | /// </summary>
|
---|
30 | public event EventHandler<CollectionEventArgs<T>> OnItemPopped;
|
---|
31 | #endregion;
|
---|
32 |
|
---|
33 | #region Constructor
|
---|
34 | /// <summary>
|
---|
35 | /// Initializes a new instance of the <see cref="T:StackBase<T>"/> class.
|
---|
36 | /// </summary>
|
---|
37 | public StackBase()
|
---|
38 | {
|
---|
39 | InnerStack = new Stack<T>();
|
---|
40 | }
|
---|
41 | #endregion
|
---|
42 |
|
---|
43 | #region Methods
|
---|
44 |
|
---|
45 | /// <summary>
|
---|
46 | /// Gets the number of elements in the stack
|
---|
47 | /// </summary>
|
---|
48 | /// <value>The count.</value>
|
---|
49 | public int Count
|
---|
50 | {
|
---|
51 | get { return InnerStack.Count; }
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | /// <summary>
|
---|
56 | /// Pushes the specified item.
|
---|
57 | /// </summary>
|
---|
58 | /// <param name="item">A parameter of the generics Type T</param>
|
---|
59 | public void Push(T item)
|
---|
60 | {
|
---|
61 |
|
---|
62 | this.InnerStack.Push(item);
|
---|
63 | RaiseOnItemPushed(item);
|
---|
64 |
|
---|
65 | }
|
---|
66 |
|
---|
67 | /// <summary>
|
---|
68 | /// Pops the next item from the stack
|
---|
69 | /// </summary>
|
---|
70 | /// <returns></returns>
|
---|
71 | public T Pop()
|
---|
72 | {
|
---|
73 | T item =
|
---|
74 | InnerStack.Pop();
|
---|
75 | RaiseOnItemPopped(item);
|
---|
76 | return item;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /// <summary>
|
---|
80 | /// Peeks the next item in the stack
|
---|
81 | /// </summary>
|
---|
82 | /// <returns></returns>
|
---|
83 | public T Peek()
|
---|
84 | {
|
---|
85 | return InnerStack.Peek();
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | /// <summary>
|
---|
90 | /// Converts the stack to an array.
|
---|
91 | /// </summary>
|
---|
92 | /// <returns></returns>
|
---|
93 | public T[] ToArray()
|
---|
94 | {
|
---|
95 | return InnerStack.ToArray();
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /// <summary>
|
---|
100 | /// Raises the <see cref="OnItemPushed"/>.
|
---|
101 | /// </summary>
|
---|
102 | /// <param name="item">A parameter of the generics Type T</param>
|
---|
103 | private void RaiseOnItemPushed(T item)
|
---|
104 | {
|
---|
105 | EventHandler<CollectionEventArgs<T>> handler = OnItemPushed;
|
---|
106 | handler(this, new CollectionEventArgs<T>(item));
|
---|
107 | }
|
---|
108 |
|
---|
109 | /// <summary>
|
---|
110 | /// Raises the on OnItemPopped event.
|
---|
111 | /// </summary>
|
---|
112 | /// <param name="item">A parameter of the generics Type T</param>
|
---|
113 | private void RaiseOnItemPopped(T item)
|
---|
114 | {
|
---|
115 | EventHandler<CollectionEventArgs<T>> handler = OnItemPopped;
|
---|
116 | handler(this, new CollectionEventArgs<T>(item));
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | #endregion
|
---|
121 |
|
---|
122 | }
|
---|
123 | }
|
---|