Writing the receiver

Writing receivers requires explicit knowledge of the sender. Follow these steps when writing a receiver:

  1. Determine the senders and events the receiver responds to.
  2. Define the member functions or functions that handle the notifications.
  3. Set up the connection to dispatch the notifications to the correct member functions and open the connection. Often, this is done in the constructor of the receiver.
Here is the interface for TShapeView. It responds to both kinds of notifications from the TShapeList instance on which it is a view. HandleAddedShape and HandleRemovedShape define the response to the corresponding notifications. The receiver uses the
TMemberFunctionConnectionTo<AReceiver> template class to handle the connection to the sender.

      class TShapeView : public TSimpleView, public virtual VViewInitialize {
      public:
          MCollectibleDeclarationsMacro(TShapeView);
      
                                  TShapeView(TShapeList* shapeList,
                                      const TGPoint& size,
                                      const TGPoint& locationInParent = TGPoint::kOrigin);
                                  TShapeView(const TShapeView&);
          virtual                 ~TShapeView();
      
      protected:
          virtual void            DrawContents(TGrafPort&) const;
      
          virtual void            HandleAddedShape(const TNotification&);
          virtual void            HandleRemovedShape(const TNotification&);
      
      private:
          TShapeList*             fShapeList;
          TMemberFunctionConnectionTo<TShapeView> fConnection;
      
          enum { kOriginalVersion };
      };
The connection is set up in TShapeView's constructor. The connection needs a pointer to the receiver, which is passed to the connection's constructor in TShapeView's member initialization list.

The protocol for creating interests in an instance of TShapeList is to call the appropriate member functions of that instance. The receiver creates the interests and adds them to the connection, associating a member function with each interest. The connection automatically dispatches notifications to the appropriate handler.

When the receiver is ready to handle notifications, it calls Connect on its connection.

      TShapeView::TShapeView(TShapeList* shapeList, const TGPoint& size,
          const TGPoint& locationInParent)
          : TSimpleView (size, locationInParent),
          fConnection(this),
          VViewInitialize(&gMetaInfo)
      {
          fShapeList = shapeList;
      
          CheckForInitialize(&gMetaInfo);
      
          fConnection.AddInterest(fShapeList->GetAddedShapeInterest(),
               &TShapeView::HandleAddedShape);
          fConnection.AddInterest(fShapeList->GetRemovedShapeInterest(),
               &TShapeView::HandleRemovedShape);
      
          fConnection.Connect();
      }
In the example presented here, HandleAddedShape and HandleRemovedShape share the same inefficient implementation--they invalidate the entire view and then redraw all the shapes. They cannot really do anything else because the notification does not contain additional information that would allow them to optimize their behavior.

      void TShapeView::HandleAddedShape(const TNotification& notification)
      {
          InvalidateAll();
      }

[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