Receiver customizes for batching

To process batched notifications, the receiver must make a few adjustments: it must add a new handler for the batched notification, and it needs to add a data member to hold state that it, as well as the other handlers, can modify when processing notification.

Follow these steps to process batched notification:

  1. Declare a handler for batched notifications and declare state for it and the other notification handlers to use.
  2. Set up the connection to batch notifications with a call to SetBatchedNotify.
  3. Implement the batch notification handler to set up the state that will be modified, distribute the notifications, then process the state.
  4. Implement the other handlers to modify the state that the handlers use.
HandleBatchNotification is the handler that receives the TBatchNotifications. An instance of TGArea, fAreaToInvalidate, holds the state.

      class TShapeView : public TSimpleView, public virtual VViewInitialize {
      protected:
          virtual void            HandleBatchNotification(const TNotification&);
          // ...
      
      private:
          TGArea                  fAreaToInvalidate;
          // ...
      };
The code to set up the connection now calls SetBatchNotify, which causes all notifications to be batched. The default handler, which is set in the connection's constructor, is used to handle these notifications.

      TShapeView::TShapeView(TShapeList* shapeList, const TGPoint& size,
          const TGPoint& locationInParent)
          : TSimpleView (size, locationInParent),
          fConnection(this, &TShapeView::HandleBatchNotification),
          fAreaToInvalidate(),
          VViewInitialize(&gMetaInfo)
      {
          fShapeList = shapeList;
      
          CheckForInitialize(&gMetaInfo);
      
          fConnection.AddInterest(fShapeList->GetAddedShapeInterest(),
               &TShapeView::HandleAddedShape);
          fConnection.AddInterest(fShapeList->GetRemovedShapeInterest(),
               &TShapeView::HandleRemovedShape);
      
          fConnection.SetBatchNotify();
          fConnection.Connect();
      }
The connection sends the notifications it receives, all of which are TBatchNotifications, to the batched notification handler.

HandleBatchNotification coerces the notification into a TBatchNotification and calls DispatchNotifications. The call to DispatchNotifications sends the notifications contained in the batch notification to the other appropriate handlers. These handlers modify the area to invalidate.

Finally, HandleBatchNotification invalidates the area and restores it for the next time it is called.

      void TShapeView::HandleBatchNotification(const TNotification& notification)
      {
          TBatchedNotification* batch = (const TBatchNotification*)&notification;
      
          batch->DispatchNotifications();
          Invalidate(fAreaToInvalidate);
          fAreaToInvalidate.SetToEmpty();
      }
When the batched notifications are dispatched, the other handlers are called. Their implementations all change to modify fAreaToInvalidate, rather than invalidating directly. Only HandleAddedShape is shown.

      void TShapeView::HandleAddedShape(const TNotification& notification)
      {
          const TShapeNotification* shapeNotification = 
              (const TShapeNotification*)&notification;
      
          fAreaToInvalidate += areaNotification->GetShape()->GetLooseFitBounds();
      }

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

Generated with WebMaker