1 #include "ace/Log_Msg.h"
2 #include "ace/Map_Manager.h"
3 #include "ace/Synch.h" // Needed for the lock.
4 #include "DataElement.h"
8 ** This needs to stay in the book for 2nd printing, but is the same as
9 ** what's in KeyType.h.
12 // Listing 1 code/ch05
16 KeyType () : val_(0) {}
18 KeyType (int i
) : val_(i
) {}
20 KeyType (const KeyType
& kt
) { this->val_
= kt
.val_
; };
22 operator int () const { return val_
; };
30 ACE_Map_Manager
<KeyType
, DataElement
, ACE_Null_Mutex
>::equal
31 (const KeyType
& r1
, const KeyType
&r2
)
37 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
40 ACE_Map_Manager
<KeyType
, DataElement
, ACE_Null_Mutex
>::equal
41 (const KeyType
& r1
, const KeyType
&r2
)
45 ACE_END_VERSIONED_NAMESPACE_DECL
51 // Illustrate the ACE_Map_Manager<>.
55 // Iterate in the forward direction.
56 void iterate_forward ();
58 // Iterate in the other direction.
59 void iterate_reverse ();
61 // Remove all elements from the map.
65 ACE_Map_Manager
<KeyType
,DataElement
,ACE_Null_Mutex
> map_
;
68 int Map_Example::run ()
70 ACE_TRACE ("Map_Example::run");
72 // Corresponding KeyType objects are created on the fly.
73 for (int i
= 0; i
< 100; i
++)
75 map_
.bind (i
, DataElement (i
));
78 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Map has\n")));
79 for (int j
= 0; j
< 100; j
++)
82 int result
= map_
.find (j
,d
);
85 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%d:"), d
.getData ()));
88 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
90 // Iterate in the forward direction.
91 this->iterate_forward ();
93 // Iterate in the other direction.
94 this->iterate_reverse ();
96 // Remove all elements from the map.
99 // Iterate in the forward direction.
100 this->iterate_forward ();
105 void Map_Example::iterate_forward ()
107 ACE_TRACE ("Map_Example::iterate_forward");
109 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Forward iteration\n")));
110 for (ACE_Map_Manager
<KeyType
, DataElement
, ACE_Null_Mutex
>::iterator
111 iter
= map_
.begin ();
115 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%d:"), (*iter
).int_id_
.getData ()));
117 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
120 void Map_Example::iterate_reverse ()
122 ACE_TRACE ("Map_Example::iterate_reverse");
124 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Reverse iteration\n")));
125 for (ACE_Map_Manager
<KeyType
, DataElement
, ACE_Null_Mutex
>::reverse_iterator
126 iter
= map_
.rbegin ();
130 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%d:"), (*iter
).int_id_
.getData ()));
132 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
135 void Map_Example::remove_all ()
137 ACE_TRACE ("Map_Example::remove_all");
139 // Note that we can't use the iterators here
140 // as they are invalidated after deletions
142 for (int i
= 0; i
< 100; i
++)
148 int ACE_TMAIN (int, ACE_TCHAR
*[])