Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / Containers / Stacks.cpp
blobe1a6b535c2e4c2c4293da0b47e628324790e2fed
1 #include "ace/OS_Memory.h"
2 #include "ace/Log_Msg.h"
3 #include "ace/Containers.h"
4 #include "DataElement.h"
6 class StackExample
8 public:
9 StackExample (): privateStack_(100) {}
11 // Illustrate all the differnet
12 // types of stacks provided by ACE.
13 int run ();
15 private:
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.
23 int runFixedStack ();
25 private:
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()));
45 return 0;
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++)
60 elem[i].setData(i);
61 // Push the element on the stack.
62 bstack1.push (elem[i]);
66 ACE_Bounded_Stack<DataElement> bstack2 (100);
68 // Make a copy!
69 bstack2 = bstack1;
70 for (int j = 0; j < 100; j++)
72 DataElement elem;
73 bstack2.pop (elem);
74 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), elem.getData ()));
77 return 0;
79 // Listing 1
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++)
89 DataElement* elem;
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;
97 fstack.pop (elem);
98 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), elem->getData ()));
99 delete elem;
102 return 0;
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++)
113 DataElement *elem;
114 ACE_NEW_RETURN(elem, DataElement (m), -1);
115 // Push the element on both stacks.
116 ustack.push (elem);
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
125 // also disappear!
126 ACE_Unbounded_Stack_Iterator<DataElement*> iter (ustack);
127 for (iter.first (); !iter.done (); iter.advance ())
129 DataElement** elem = 0;
130 iter.next (elem);
131 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"),
132 (*elem)->getData ()));
133 delete (*elem);
136 return 0;
138 // Listing 2
140 int ACE_TMAIN (int, ACE_TCHAR *[])
142 StackExample se;
143 return se.run ();