Merge pull request #2317 from jwillemsen/jwi-deleteop
[ACE_TAO.git] / ACE / examples / APG / Containers / Queues.cpp
bloba0dfda6274c26d59e31f7d8f8a760b381e18e75a
1 #include "ace/OS_Memory.h"
2 #include "ace/Log_Msg.h"
3 #include "ace/Containers.h"
4 #include "DataElement.h"
6 class QueueExample
8 public:
9 // Illustrate the various ACE Queues.
10 int run ();
12 private:
13 // Illustrate the ACE unbounded queue
14 // that has copies of the data elements.
15 int runStackUnboundedQueue ();
17 // Illustrate the ACE unbounded queue
18 // with pointers to elements on the heap.
19 int runHeapUnboundedQueue ();
22 int QueueExample::run ()
24 ACE_TRACE ("QueueExample::run");
26 // Illustrate the queue with elements on the stack.
27 if (this->runStackUnboundedQueue () != 0)
29 return -1;
32 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n# of live objects %d\n"),
33 DataElement::numOfActiveObjects ()));
35 // Illustrate the queue with elements on the heap.
36 if (this->runHeapUnboundedQueue () != 0)
38 return -1;
41 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n# of live objects %d\n"),
42 DataElement::numOfActiveObjects ()));
44 return 0;
47 // Listing 1 code/ch05
48 int QueueExample::runStackUnboundedQueue ()
50 ACE_TRACE ("QueueExample::runStackUnboundedQueue");
52 ACE_Unbounded_Queue<DataElement> queue;
53 DataElement elem1[10];
54 int i;
55 for (i = 0; i < 10; i++)
57 elem1[i].setData (9-i);
58 queue.enqueue_head (elem1[i]);
61 DataElement elem2[10];
62 for (i = 0; i < 10; i++)
64 elem2[i].setData (i+10);
65 queue.enqueue_tail (elem2[i]);
68 for (ACE_Unbounded_Queue_Iterator<DataElement> iter (queue);
69 !iter.done ();
70 iter.advance ())
72 DataElement *elem = 0;
73 iter.next (elem);
74 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), elem->getData ()));
77 return 0;
79 // Listing 1
80 // Listing 2 code/ch05
81 int QueueExample::runHeapUnboundedQueue ()
83 ACE_TRACE ("QueueExample::runHeapUnboundedQueue");
85 ACE_Unbounded_Queue<DataElement*> queue;
86 for (int i = 0; i < 20; i++)
88 DataElement *elem;
89 ACE_NEW_RETURN(elem, DataElement (i), -1);
90 queue.enqueue_head (elem);
93 for (ACE_Unbounded_Queue_Iterator<DataElement*> iter
94 = queue.begin ();
95 !iter.done ();
96 iter.advance ())
98 DataElement **elem = 0;
99 iter.next(elem);
100 ACE_DEBUG
101 ((LM_DEBUG, ACE_TEXT ("%d:"), (*elem)->getData ()));
102 delete (*elem);
105 return 0;
107 // Listing 2
108 int ACE_TMAIN (int, ACE_TCHAR *[])
110 QueueExample que;
111 return que.run ();