1 #include "ace/OS_Memory.h"
2 #include "ace/Log_Msg.h"
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
;
12 // Listing 2 code/ch05
17 void displayList (MyList
& list
); // Display all elements.
18 void destroyList (MyList
& list
); // Destroy all elements.
21 // Listing 3 code/ch05
25 ACE_TRACE ("ListTest::run");
27 // Create a list and insert 100 elements.
30 for (int i
= 0; i
< 100; i
++)
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.
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.
64 // Listing 4 code/ch05
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
);
77 DataElement
*de
= iter
.next ();
82 // Listing 5 code/ch05
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
);
93 ((LM_DEBUG
, ACE_TEXT ("%d:"), iter
.next()->getData()));
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 ())
103 ((LM_DEBUG
, ACE_TEXT ("%d:"), riter
.next()->getData()));
106 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
109 int ACE_TMAIN (int, ACE_TCHAR
*[])