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