BasicNotification

BasicNotification is a sample that introduces the sample user classes TSender and TReceiver, which implement the simplest form of notification using TNotifier, TInterest, and TMemberFunctionConnectionTo<>.

Running the sample

Execute BasicNotificationSApp. As the notifications are sent and received, a series of values appear in the shell window.

Files and classes

TSender

TSender is defined in Sender.h and Sender.C. It inherits from TNotifier in order to easily use notification protocol. TNotifier implements synchronous notification.

TSender provides an interface, GetValueChangedInterest, that receivers will use to express interest in particular notifications. This creates an interest that specifies the notifier (the same as TSender, as it inherits from TNotifier) and a token to distinguish this interest from others.

TSender calls Notify, sending a standard notification with the interest defined above, when it makes a change that it wants to notify receivers of.

TReceiver

TReceiver is defined in Receiver.h and Receiver.C. It contains an instance of TMemberFunctionConnectionTo, which it uses to receive notifications. It uses the VersionDeclarationsMacro to define the protocol this connection requires.

TReceiver provides this instance with an object and member function to call when notifications are received. In this sample, it does this in the constructor, because its initialization is complete at this time. (Other receivers might have to delay setting up the constructor until later.)

TReceiver uses the protocol of TSender to get an interest in changes by the sender it listens to, and tells the connection to inform it when the connection receives notifications sent using that interest.

TReceiver calls Connect on its connection to start receiving notifications.

Notes

In cases where a receiver is only interested in one kind of interest, setting up the connection to distribute notifications via the default handler, as here, is most efficient, because the connection does not need to build a dictionary to map interests to member functions.

The order of calls to Connect and to AddInterest is not important. Interests can be added and removed, and connections connected and disconnected, at any time.

Take special care to ensure that the notifier is not destroyed before the connection. Otherwise, when the connection is destroyed, it will attempt to disconnect from the notifier using an invalid pointer, resulting in a bus error.

This technique of defining TSender protocol to create interests is typical. TInterest is quite lightweight, so copying it is not a problem. This also hides the actual notifier used in the TInterest, allowing later versions of TSender to use a different notifier instead of inheriting from one. Receivers cannot assume that the notifier in the interest can be coerced to the class of the sender.


Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.