HOWTO use null callbacks

From Nsnam
Jump to: navigation, search

Main Page - Current Development - Developer FAQ - Tools - Related Projects - Project Ideas - Summer Projects

Installation - Troubleshooting - User FAQ - HOWTOs - Samples - Models - Education - Contributed Code - Papers

Sometimes the API in use may require a callback; however, for your particular function, you may not wish to provide one. In this case, it is possible to provide a null callback.

HOWTO use null callbacks

To use a null callback, use the ns3::MakeNullCallback construct:

   template<typename R>
   Callback< R, T1, T2, T3, T4, T5, T6 > ns3::MakeNullCallback (void)

Example usage: The ns3::Socket class uses callbacks to indicate completion of events such as a successful TCP connect(). These callbacks are set in the following function:

   void Socket::SetConnectCallback (Callback<void, Ptr<Socket> > connectionSucceeded,
                          Callback<void, Ptr<Socket> > connectionFailed,
                          Callback<void, Ptr<Socket> > halfClose);

But suppose you do not care about registering a callback for the halfClose event (but you want to register one for the connectionSucceeded and connectionFailed cases). In that case, you can pass a null callback as the third argument. You just need to pass a callback with the matching signature, as follows:

   localSocket->SetConnectCallback (
   MakeCallback (&ConnectionSucceededCallback),
   MakeCallback (&ConnectionFailedCallback),
   MakeNullCallback<void, Ptr<Socket> > () );