Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / APG / Containers / Hash_Map_Hash.cpp
blobd56305e931891b37fefc87d73f41158d2103f0d8
1 #include "ace/Hash_Map_Manager.h"
2 #include "ace/Synch.h" // Needed for the lock
3 #include "ace/Functor.h"
4 #include "DataElement.h"
5 #include "Hash_Map_Hash.h"
7 // Little helper class
8 template<class EXT_ID, class INT_ID>
9 class Hash_Map :
10 public ACE_Hash_Map_Manager_Ex<EXT_ID, INT_ID,
11 ACE_Hash<EXT_ID>, ACE_Equal_To<EXT_ID>, ACE_Null_Mutex>
12 {};
15 class Hash_Map_Example
17 public:
18 ~Hash_Map_Example ()
20 map_.close ();
23 // illustrate the hash map
24 int run ();
26 // use the forward iterate
27 void iterate_forward ();
29 // use the reverse iterator
30 void iterate_reverse ();
32 // remove all the elements from the map
33 void remove_all ();
35 private:
36 Hash_Map<KeyType, DataElement> map_;
39 int Hash_Map_Example::run ()
41 ACE_TRACE ("Hash_Map_Example::run");
43 for (int i = 0; i < 100; i++)
45 map_.bind (i, DataElement (i));
48 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Map has\n")));
49 for (int j = 0; j < 100; j++)
51 DataElement d;
52 map_.find (j, d);
53 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), d.getData ()));
55 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
57 // Use the forward iterator.
58 this->iterate_forward ();
60 // Use the reverse iterator.
61 this->iterate_reverse ();
63 // Remove all the elements from the map.
64 this->remove_all ();
66 // Iterate through the map again.
67 this->iterate_forward ();
69 return 0;
72 void Hash_Map_Example::iterate_forward ()
74 ACE_TRACE ("Hash_Map_Example::iterate_forward");
76 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Forward Iteration\n")));
77 for (Hash_Map<KeyType, DataElement>::iterator iter = map_.begin ();
78 iter != map_.end (); iter++)
80 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), (*iter).int_id_.getData ()));
82 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
85 void Hash_Map_Example::iterate_reverse ()
87 ACE_TRACE ("Hash_Map_Example::iterate_reverse");
89 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Reverse Iteration\n")));
90 for (Hash_Map<KeyType, DataElement>::reverse_iterator iter = map_.rbegin ();
91 iter != map_.rend (); iter++)
93 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), (*iter).int_id_.getData ()));
95 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
98 void Hash_Map_Example::remove_all ()
100 ACE_TRACE ("Hash_Map_Example::remove_all");
101 map_.unbind_all ();
104 int ACE_TMAIN (int, ACE_TCHAR *[])
106 Hash_Map_Example me;
107 return me.run ();