Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / Containers / Map_Manager.cpp
blobcc1e71440353f6185a269fc42f0597af8ad49376
1 #include "ace/Log_Msg.h"
2 #include "ace/Map_Manager.h"
3 #include "ace/Synch.h"
4 #include "DataElement.h"
5 #include "KeyType.h"
7 class Map_Example
9 public:
10 // Illustrate the ACE_Map_Manager.
11 int run ();
13 private:
14 // Iterate in the forward direction.
15 void iterate_forward ();
17 // Iterate in the other direction.
18 void iterate_reverse ();
20 // Remove all elements from the map.
21 void remove_all ();
23 private:
24 ACE_Map_Manager<KeyType,DataElement,ACE_Null_Mutex> map_;
27 // Listing 2 code/ch05
28 int Map_Example::run ()
30 ACE_TRACE ("Map_Example::run");
32 // Corresponding KeyType objects are created on the fly.
33 for (int i = 0; i < 100; i++)
35 map_.bind (i, DataElement (i));
38 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Map has\n")));
39 for (int j = 0; j < 100; j++)
41 DataElement d;
42 map_.find (j,d);
43 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), d.getData ()));
45 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
47 // Iterate in the forward direction.
48 this->iterate_forward ();
50 // Iterate in the other direction.
51 this->iterate_reverse ();
53 // Remove all elements from the map.
54 this->remove_all ();
56 // Iterate in the forward direction.
57 this->iterate_forward ();
59 return 0;
61 // Listing 2
62 // Listing 3 code/ch05
63 void Map_Example::iterate_forward ()
65 ACE_TRACE ("Map_Example::iterate_forward");
67 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Forward iteration\n")));
68 for (ACE_Map_Manager<KeyType,
69 DataElement,
70 ACE_Null_Mutex>::iterator
71 iter = map_.begin ();
72 iter != map_.end ();
73 iter++)
75 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"),
76 (*iter).int_id_.getData ()));
78 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
82 void Map_Example::iterate_reverse ()
84 ACE_TRACE ("Map_Example::iterate_reverse");
85 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Reverse iteration\n")));
86 for (ACE_Map_Manager<KeyType,
87 DataElement,
88 ACE_Null_Mutex>::reverse_iterator
89 iter = map_.rbegin ();
90 iter != map_.end ();
91 iter++)
93 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"),
94 (*iter).int_id_.getData ()));
96 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
98 // Listing 3
99 // Listing 4 code/ch05
100 void Map_Example::remove_all ()
102 ACE_TRACE ("Map_Example::remove_all");
104 // Note that we can't use the iterators here as they
105 // are invalidated after deletions or insertions.
106 map_.unbind_all ();
108 // Listing 4
110 int ACE_TMAIN (int, ACE_TCHAR *[])
112 Map_Example me;
113 return me.run ();