Merge pull request #2317 from jwillemsen/jwi-deleteop
[ACE_TAO.git] / ACE / examples / APG / Containers / Map_Manager_Specialization.cpp
blobe28634d5a0d46c2e3c36a72f4a6dc59a922d5e21
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"
5 #include "KeyType.h"
7 /*
8 ** This needs to stay in the book for 2nd printing, but is the same as
9 ** what's in KeyType.h.
11 #if 0
12 // Listing 1 code/ch05
13 class KeyType
15 public:
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_; };
24 private:
25 int val_;
28 template<>
29 int
30 ACE_Map_Manager<KeyType, DataElement, ACE_Null_Mutex>::equal
31 (const KeyType& r1, const KeyType &r2)
33 return (r1 == r2);
35 // Listing 1
36 #else
37 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
38 template<>
39 int
40 ACE_Map_Manager<KeyType, DataElement, ACE_Null_Mutex>::equal
41 (const KeyType& r1, const KeyType &r2)
43 return (r1 == r2);
45 ACE_END_VERSIONED_NAMESPACE_DECL
46 #endif /* 0 */
48 class Map_Example
50 public:
51 // Illustrate the ACE_Map_Manager<>.
52 int run ();
54 private:
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.
62 void remove_all ();
64 private:
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++)
81 DataElement d;
82 int result = map_.find (j,d);
83 if (result == 0)
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.
97 this->remove_all ();
99 // Iterate in the forward direction.
100 this->iterate_forward ();
102 return 0;
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 ();
112 iter!= map_.end ();
113 iter++)
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 ();
127 iter!= map_.end ();
128 iter++)
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
141 // or insertions.
142 for (int i = 0; i < 100; i++)
144 map_.unbind (i);
148 int ACE_TMAIN (int, ACE_TCHAR *[])
150 Map_Example me;
151 return me.run ();