Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / Containers / Sets.cpp
blob771cfb80f59d82359222cc1ef81427477ddb98a5
1 #include "ace/OS_Memory.h"
2 #include "ace/Log_Msg.h"
3 #include "ace/Containers.h"
4 #include "DataElement.h"
6 class SetExample
8 public:
9 // Illustrate all ACE set types.
10 int run ();
12 private:
13 // Illustrate the ACE Bounded Sets.
14 int runBoundedSet ();
16 // Illustrate the ACE Unbounded sets.
17 int runUnboundedSet ();
20 int SetExample::run ()
22 ACE_TRACE ("SetExample::run");
24 ACE_ASSERT (!this->runBoundedSet ());
25 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n# of live objects %d\n"),
26 DataElement::numOfActiveObjects ()));
28 ACE_ASSERT (!this->runUnboundedSet ());
29 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n# of live objects %d\n"),
30 DataElement::numOfActiveObjects ()));
32 return 0;
34 // Listing 1 code/ch05
35 int SetExample::runBoundedSet ()
37 ACE_TRACE ("SetExample::runBoundedSet");
38 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Using a bounded set\n")));
39 ACE_Bounded_Set<DataElement> bset (100);
41 DataElement elem[100];
42 for (int i = 0; i < 100; i++)
44 elem[i].setData (i);
46 // Inserting two copies of the same element isn't allowed.
47 bset.insert (elem[i]);
48 if (bset.insert (elem[i]) == -1)
50 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("%p\n"),
51 ACE_TEXT ("insert set")));
54 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d\n"),
55 DataElement::numOfActiveObjects ()));
57 DataElement elem1 (10), elem2 (99);
58 if (!bset.find (elem1) && !bset.find (elem2))
60 ACE_DEBUG ((LM_INFO,
61 ACE_TEXT ("The elements %d and %d are ")
62 ACE_TEXT ("in the set!\n"),
63 elem1.getData (), elem2.getData ()));
66 for (int j = 0; j < 50; j++)
68 bset.remove (elem[j]); // Remove the element from the set.
69 ACE_DEBUG
70 ((LM_DEBUG, ACE_TEXT ("%d:"), elem[j].getData ()));
73 if ((bset.find (elem[0]) == -1) && (bset.find (elem[49]) == -1))
75 ACE_DEBUG ((LM_INFO,
76 ACE_TEXT ("The elements %d and %d are ")
77 ACE_TEXT ("NOT in the set!\n"),
78 elem[0].getData (), elem[99].getData ()));
81 return 0;
83 // Listing 1
84 // Listing 2 code/ch05
85 int SetExample::runUnboundedSet ()
87 ACE_TRACE ("SetExample::runUnboundedSet");
88 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Using an unbounded set.\n")));
89 ACE_Unbounded_Set<DataElement*> uset;
90 for (int m = 0; m < 100; m++)
92 DataElement *elem;
93 ACE_NEW_RETURN (elem, DataElement (m), -1);
94 uset.insert (elem);
96 DataElement deBegin (0), deEnd (99);
97 if (!uset.find (&deBegin) && !uset.find (&deEnd))
99 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Found the elements\n")));
102 // Iterate and destroy the elements in the set.
103 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Deleting the elements\n")));
104 ACE_Unbounded_Set_Iterator<DataElement*> iter (uset);
105 for (iter = uset.begin (); iter != uset.end (); iter++)
107 DataElement* elem = (*iter);
108 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("%d:"), elem->getData ()));
109 delete elem;
112 return 0;
114 // Listing 2
116 int ACE_TMAIN (int, ACE_TCHAR *[])
118 SetExample se;
119 se.run ();
120 return 0;