Merge pull request #2317 from jwillemsen/jwi-deleteop
[ACE_TAO.git] / ACE / examples / APG / Containers / DLList.cpp
blob0de893dd330d9db666ddeab6e6804b3d3f18b53c
1 #include "ace/OS_Memory.h"
2 #include "ace/Log_Msg.h"
4 // Listing 1 code/ch05
5 #include "ace/Containers.h"
6 #include "DataElement.h"
8 // Create a new type of list that can store only DataElements.
9 typedef ACE_DLList<DataElement> MyList;
10 // Listing 1
12 // Listing 2 code/ch05
13 class ListTest
15 public:
16 int run ();
17 void displayList (MyList & list); // Display all elements.
18 void destroyList (MyList& list); // Destroy all elements.
20 // Listing 2
21 // Listing 3 code/ch05
22 int
23 ListTest::run ()
25 ACE_TRACE ("ListTest::run");
27 // Create a list and insert 100 elements.
28 MyList list1;
30 for (int i = 0; i < 100; i++)
32 DataElement *element;
33 ACE_NEW_RETURN (element, DataElement (i), -1);
34 list1.insert_tail (element);
37 // Iterate through and display to output.
38 this->displayList (list1);
40 // Create a copy of list1.
41 MyList list2;
42 list2 = list1;
44 // Iterate over the copy and display it to output.
45 this->displayList(list2);
47 // Get rid of the copy list and all its elements.
48 // Since both lists had the *same* elements
49 // this will cause list1 to contain pointers that
50 // point to data elements that have already been destroyed!
51 this->destroyList (list2);
53 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("# of live objects: %d\n"),
54 DataElement::numOfActiveObjects()));
56 // The lists themselves are destroyed here. Note that the
57 // list destructor will destroy copies of whatever data the
58 // list contained. Since in this case the list contained
59 // copies of pointers to the data elements these are the
60 // only thing that gets destroyed here.
61 return 0;
63 // Listing 3
64 // Listing 4 code/ch05
65 void
66 ListTest::destroyList (MyList& list)
68 ACE_TRACE ("ListTest::destroyList");
70 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Destroying data elements\n")));
72 // Iterate through and delete all the data elements on the list.
73 for (ACE_DLList_Iterator<DataElement> iter (list);
74 !iter.done ();
75 iter++)
77 DataElement *de = iter.next ();
78 delete de;
81 // Listing 4
82 // Listing 5 code/ch05
83 void
84 ListTest::displayList (MyList& list)
86 ACE_TRACE ("ListTest::displayList");
88 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Forward iteration\n")));
89 ACE_DLList_Iterator<DataElement> iter (list);
90 while (!iter.done ())
92 ACE_DEBUG
93 ((LM_DEBUG, ACE_TEXT ("%d:"), iter.next()->getData()));
94 iter++;
96 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
98 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Reverse Iteration\n")));
99 ACE_DLList_Reverse_Iterator<DataElement> riter (list);
100 while (!riter.done ())
102 ACE_DEBUG
103 ((LM_DEBUG, ACE_TEXT ("%d:"), riter.next()->getData()));
104 riter++;
106 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
108 // Listing 5
109 int ACE_TMAIN (int, ACE_TCHAR *[])
111 ListTest test;
112 return test.run ();