The following example demonstrates the use of collection event data for a sequence of IString objects. IString is the main string handling class provided by the IBM Open Class Library.
#include <iobservr.hpp>
#include <inotifev.hpp>
#include <iseq.h>
typedef IVSequence <long> Notifier;
#include <iostream.h>
template <class Notifier>
class Observer : public IObserver {
public:
Observer (Notifier* notifier)
: ivNotifier (notifier)
{
handleNotificationsFor (*ivNotifier);
}
~Observer ()
{
if (ivNotifier != 0) // critical !
stopHandlingNotificationsFor (*ivNotifier);
}
IObserver& dispatchNotificationEvent(
INotificationEvent const& event)
{
if (event.notificationId() == IStandardNotifier::deleteId) {
cout << "IStandardNotifier::deleteId received" << endl;
}
}
else if (event.notificationId() == IVCollection::removeId) {
cout << "IVCollection::removeId received" << endl;
cout << "Old Data: ";
cout << ((IASequence<long>)(event.notifier()));
//
// IASequence can be either replaced by IACollection
// or IAOrderedCollection:
// cout << ((IACollection<long>)(event.notifier()));
//
cout.elementAt(
((IVCollectionEventData<long>*)
((char*)event.eventData()))->cursor());
cout << endl;
}
else if (event.notificationId() == IVCollection::replaceId) {
cout << "IVCollection::replaceId received" << endl;
cout << "Replace at position: ";
cout << ((IASequence<long>)(event.notifier()));
//
// IASequence can be replaced by IAOrderedCollection
// cout << ((IAOrderedCollection<long>)(event.notifier()));
//
cout.positionAt(
((IVCollectionEventData<long>*)
((char*)event.eventData()))->cursor());
cout << endl;
cout << "Old Data: "
cout << ((IASequence<long>)(event.notifier()));
//
// IASequence can be either replaced by IACollection
// or IAOrderedCollection:
// cout << ((IACollection<long>)(event.notifier()));
//
cout.elementAt(
((IVCollectionEventData<long>*)
((char*)event.eventData()))->cursor());
cout << endl;
cout << "New Data: ";
cout << *(((IVCollectionEventData<long>*)
((char*)event.eventData()))->element());
cout << endl;
}
else if (event.notificationId() == IVCollection::addId)
{
cout << "IVCollection::addId received" << endl;
cout << "Add at position: ";
cout << ((IAOrderedCollection<long>)(event.notifier()));
cout.positionAt(((IVCollectionEventData<long>*)
((char*)event.eventData()))->cursor());
cout << endl;
cout << "New Data: ";
cout << ((IASequence<long>)(event.notifier()));
//
// IASequence can be either replaced by IACollection
// or IAOrderedCollection:
// cout << ((IACollection<long>)(event.notifier()));
//
cout.elementAt(
((IVCollectionEventData<long>*)
((char*)event.eventData()))->cursor());
cout << endl;
}
else {
cout << "unknown event received" << endl;
}
return *this;
}
private:
Notifier* ivNotifier;
};
int main ()
{
Notifier* n = new Notifier;
Notifier::Cursor c(*n);
Observer <Notifier> o (n);
n->enableNotification ();
{
n->add(123,c);
cout << "element in collection: "
<< n->elementAt(c) << endl;
n->replaceAt(c,456);
cout << "element in collection: "
<< n->elementAt(c) << endl;
n->removeAt(c);
cout << "Number of elements in collection: "
<< n->numberOfElements() << endl;
}
delete n;
return 0;
}
![]()
Introduction
to the Collection Classes
Overall
Implementation Structure
Adding
Elements
Removing
Elements
Replacing
Elements
Support
for Notifications
![]()
Adding an Element to a
Collection
Removing an Element from
a Collection
Instantiating the
Collection Classes