1 #include "ace/OS_Memory.h"
2 #include "ace/Log_Msg.h"
3 #include "ace/Containers.h"
4 #include "DataElement.h"
9 StackExample (): privateStack_(100) {}
11 // Illustrate all the differnet
12 // types of stacks provided by ACE.
16 // Illustrate the use of a bounded stack.
17 int runBoundedStack ();
19 // Illustrate the use of an unbounded stack.
20 int runUnboundedStack ();
22 // Illustrate the use of a compile time fixed stack.
26 ACE_Bounded_Stack
<DataElement
*> privateStack_
;
29 int StackExample::run ()
31 ACE_TRACE ("StackExample::run");
33 ACE_ASSERT(!this->runBoundedStack());
34 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n# of live objects %d\n"),
35 DataElement::numOfActiveObjects()));
37 ACE_ASSERT(!this->runFixedStack());
38 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n# of live objects %d\n"),
39 DataElement::numOfActiveObjects()));
41 ACE_ASSERT(!this->runUnboundedStack());
42 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n# of live objects %d\n"),
43 DataElement::numOfActiveObjects()));
47 // Listing 1 code/ch05
48 int StackExample::runBoundedStack ()
50 ACE_TRACE ("StackExample::runBoundedStack");
51 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Using a bounded stack\n")));
53 ACE_Bounded_Stack
<DataElement
> bstack1 (100);
55 // The element array is constrained to this scope.
57 DataElement elem
[100];
58 for (int i
= 0; i
< 100; i
++)
61 // Push the element on the stack.
62 bstack1
.push (elem
[i
]);
66 ACE_Bounded_Stack
<DataElement
> bstack2 (100);
70 for (int j
= 0; j
< 100; j
++)
74 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%d:"), elem
.getData ()));
80 // Listing 2 code/ch05
81 int StackExample::runFixedStack ()
83 ACE_TRACE ("StackExample::runFixedStack");
84 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Using a fixed stack\n")));
86 ACE_Fixed_Stack
<DataElement
*, 100> fstack
;
87 for (int k
= 0; k
< 100; k
++)
90 ACE_NEW_RETURN(elem
, DataElement (k
), -1);
91 fstack
.push (elem
); // Push the element on the stack.
94 for (int l
= 0; l
< 100; l
++)
96 DataElement
* elem
= 0;
98 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%d:"), elem
->getData ()));
105 int StackExample::runUnboundedStack ()
107 ACE_TRACE ("StackExample::runUnboundedStack");
108 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Using an unbounded stack\n")));
110 ACE_Unbounded_Stack
<DataElement
*> ustack
;
111 for (int m
= 0; m
< 100; m
++)
114 ACE_NEW_RETURN(elem
, DataElement (m
), -1);
115 // Push the element on both stacks.
117 privateStack_
.push (elem
);
120 // Oddly enough, you can actually iterate through an
121 // unbounded stack! This is because underneath the covers
122 // the unbounded stack is a linked list.
124 // This will cause the elements in the private stack to
126 ACE_Unbounded_Stack_Iterator
<DataElement
*> iter (ustack
);
127 for (iter
.first (); !iter
.done (); iter
.advance ())
129 DataElement
** elem
= 0;
131 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%d:"),
132 (*elem
)->getData ()));
140 int ACE_TMAIN (int, ACE_TCHAR
*[])