Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / APG / Containers / RB_Tree_Functors.cpp
blobd1fb62435bab4095cbdbd421b090d744ffb9fa8a
1 #include "ace/Log_Msg.h"
2 #include "DataElement.h"
3 #include "RB_Tree_Functors.h"
5 // Listing 0 code/ch05
6 #include "ace/RB_Tree.h"
7 #include "ace/Synch.h"
9 // Little helper class.
10 template<class EXT_ID, class INT_ID>
11 class Tree : public ACE_RB_Tree<EXT_ID, INT_ID,
12 ACE_Less_Than<EXT_ID>,
13 ACE_Null_Mutex>
14 {};
15 // Listing 0
17 class Tree_Example
19 public:
20 // Illustrate the tree.
21 int run ();
23 private:
24 // Use the forward iterator.
25 void iterate_forward ();
27 // Use the reverse iterator.
28 void iterate_reverse ();
30 // Remove all elements from the tree.
31 int remove_all ();
33 private:
34 Tree<KeyType, DataElement*> tree_;
38 int Tree_Example::run ()
40 ACE_TRACE ("Tree_Example::run");
42 DataElement *d = 0;
43 for (int i = 0; i < 100; i++)
45 ACE_NEW_RETURN (d, DataElement (i), -1);
46 int result = tree_.bind(i, d);
47 if (result != 0)
49 ACE_ERROR_RETURN((LM_ERROR, "%p\n", "Bind"), -1);
53 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Using find:\n")));
54 for (int j = 0; j < 100; j++)
56 DataElement* d = 0;
57 int result = tree_.find (j, d);
58 if (result != 0)
60 ACE_ERROR_RETURN((LM_ERROR, "%p\n", "Find"), -1);
62 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), d->getData ()));
64 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
66 // Use the forward iterator.
67 this->iterate_forward ();
69 // Use the reverse iterator.
70 this->iterate_reverse ();
72 // Remove all elements from the tree.
73 ACE_ASSERT (this->remove_all ()!= -1);
75 // Iterate through once again.
76 this->iterate_forward ();
78 return 0;
81 void Tree_Example::iterate_forward ()
83 ACE_TRACE ("Tree_Example::iterate_forward");
85 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Forward Iteration\n")));
86 for (Tree<KeyType, DataElement*>::iterator iter = tree_.begin ();
87 iter != tree_.end (); iter++)
89 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), (*iter).item ()->getData ()));
91 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
94 void Tree_Example::iterate_reverse ()
96 ACE_TRACE ("Tree_Example::iterate_reverse");
98 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Reverse Iteration\n")));
99 for (Tree<KeyType, DataElement*>::reverse_iterator iter = tree_.rbegin ();
100 iter != tree_.rend (); iter++)
102 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), (*iter).item ()->getData ()));
104 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
107 int Tree_Example::remove_all ()
109 ACE_TRACE ("Tree_Example::remove_all");
111 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Removing elements\n")));
112 for (int i = 0; i < 100; i++)
114 DataElement * d = 0;
115 int result = tree_.unbind (i, d);
116 if (result != 0)
118 ACE_ERROR_RETURN((LM_ERROR, "%p\n", "Unbind"), -1);
120 ACE_ASSERT (d != 0);
121 delete d;
124 return 0;
127 int ACE_TMAIN (int, ACE_TCHAR *[])
129 Tree_Example te;
130 return te.run ();