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