ns-3
ns3::Callback< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 > Class Template Reference

Callback template class. More...

#include <callback.h>

Inheritance diagram for ns3::Callback< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 >:
Collaboration diagram for ns3::Callback< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 >:

List of all members.

Public Member Functions

template<typename FUNCTOR >
 Callback (FUNCTOR const &functor, bool, bool)
template<typename OBJ_PTR , typename MEM_PTR >
 Callback (OBJ_PTR const &objPtr, MEM_PTR mem_ptr)
 Callback (Ptr< CallbackImpl< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 > > const &impl)
template<typename T >
Callback< R, T2, T3, T4, T5,
T6, T7, T8, T9 > 
Bind (T a)
bool IsNull (void) const
void Nullify (void)
operator() (void) const
operator() (T1 a1) const
operator() (T1 a1, T2 a2) const
operator() (T1 a1, T2 a2, T3 a3) const
operator() (T1 a1, T2 a2, T3 a3, T4 a4) const
operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) const
operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6) const
operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7) const
operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7, T8 a8) const
operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7, T8 a8, T9 a9) const
bool IsEqual (const CallbackBase &other) const
bool CheckType (const CallbackBase &other) const
void Assign (const CallbackBase &other)

Detailed Description

template<typename R, typename T1 = empty, typename T2 = empty, typename T3 = empty, typename T4 = empty, typename T5 = empty, typename T6 = empty, typename T7 = empty, typename T8 = empty, typename T9 = empty>
class ns3::Callback< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 >

Callback template class.

This class template implements the Functor Design Pattern. It is used to declare the type of a Callback:

  • the first non-optional template argument represents the return type of the callback.
  • the second optional template argument represents the type of the first argument to the callback.
  • the third optional template argument represents the type of the second argument to the callback.
  • the fourth optional template argument represents the type of the third argument to the callback.
  • the fifth optional template argument represents the type of the fourth argument to the callback.
  • the sixth optional template argument represents the type of the fifth argument to the callback.

Callback instances are built with the MakeCallback template functions. Callback instances have POD semantics: the memory they allocate is managed automatically, without user intervention which allows you to pass around Callback instances by value.

Sample code which shows how to use this class template as well as the function templates MakeCallback :

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
#include "ns3/callback.h"
#include "ns3/assert.h"
#include <iostream>

using namespace ns3;

static double 
CbOne (double a, double b)
{
  std::cout << "invoke cbOne a=" << a << ", b=" << b << std::endl;
  return a;
}

class MyCb {
public:
  int CbTwo (double a) {
    std::cout << "invoke cbTwo a=" << a << std::endl;
    return -5;
  }
};


int main (int argc, char *argv[])
{
  // return type: double
  // first arg type: double
  // second arg type: double
  Callback<double, double, double> one;
  // build callback instance which points to cbOne function
  one = MakeCallback (&CbOne);
  // this is not a null callback
  NS_ASSERT (!one.IsNull ());
  // invoke cbOne function through callback instance
  double retOne;
  retOne = one (10.0, 20.0);
  // cast retOne to void, to suppress variable ‘retOne’ set but
  // not used compiler warning
  (void) retOne; 

  // return type: int
  // first arg type: double
  Callback<int, double> two;
  MyCb cb;
  // build callback instance which points to MyCb::cbTwo
  two = MakeCallback (&MyCb::CbTwo, &cb);
  // this is not a null callback
  NS_ASSERT (!two.IsNull ());
  // invoke MyCb::cbTwo through callback instance
  int retTwo;
  retTwo = two (10.0);
  // cast retTwo to void, to suppress variable ‘retTwo’ set but
  // not used compiler warning
  (void) retTwo;
  two = MakeNullCallback<int, double> ();
  // invoking a null callback is just like
  // invoking a null function pointer:
  // it will crash.
  //int retTwoNull = two (20.0);
  NS_ASSERT (two.IsNull ());

#if 0
  // The below type mismatch between CbOne() and callback two will fail to 
  // compile if enabled in this program.
  two = MakeCallback (&CbOne);
#endif

#if 0
  // This is a slightly different example, in which the code will compile
  // but because callbacks are type-safe, will cause a fatal error at runtime 
  // (the difference here is that Assign() is called instead of operator=)
  Callback<void, float> three;
  three.Assign (MakeCallback (&CbOne));
#endif

  return 0;
}

The documentation for this class was generated from the following file: