2006-11-01 13:11:30 +01:00
|
|
|
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
2006-08-29 17:51:04 +02:00
|
|
|
#include "ns3/callback.h"
|
2007-02-16 09:56:21 +01:00
|
|
|
#include "ns3/assert.h"
|
2006-08-29 17:47:17 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2006-08-29 17:55:34 +02:00
|
|
|
using namespace ns3;
|
2006-08-29 17:47:17 +02:00
|
|
|
|
|
|
|
|
static double
|
2006-10-06 13:37:25 +02:00
|
|
|
CbOne (double a, double b)
|
2006-08-29 17:47:17 +02:00
|
|
|
{
|
2006-11-01 13:11:30 +01:00
|
|
|
std::cout << "invoke cbOne a=" << a << ", b=" << b << std::endl;
|
|
|
|
|
return a;
|
2006-08-29 17:47:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MyCb {
|
|
|
|
|
public:
|
2006-11-01 13:11:30 +01:00
|
|
|
int CbTwo (double a) {
|
|
|
|
|
std::cout << "invoke cbTwo a=" << a << std::endl;
|
|
|
|
|
return -5;
|
|
|
|
|
}
|
2006-08-29 17:47:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main (int argc, char *argv[])
|
|
|
|
|
{
|
2006-11-01 13:11:30 +01:00
|
|
|
// 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
|
2007-02-16 09:56:21 +01:00
|
|
|
NS_ASSERT (!one.IsNull ());
|
2006-11-01 13:11:30 +01:00
|
|
|
// invoke cbOne function through callback instance
|
|
|
|
|
double retOne;
|
|
|
|
|
retOne = one (10.0, 20.0);
|
|
|
|
|
|
|
|
|
|
// 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
|
2007-02-16 09:56:21 +01:00
|
|
|
NS_ASSERT (!two.IsNull ());
|
2006-11-01 13:11:30 +01:00
|
|
|
// invoke MyCb::cbTwo through callback instance
|
|
|
|
|
int retTwo;
|
|
|
|
|
retTwo = two (10.0);
|
|
|
|
|
|
|
|
|
|
two = MakeNullCallback<int, double> ();
|
|
|
|
|
// invoking a null callback is just like
|
|
|
|
|
// invoking a null function pointer:
|
|
|
|
|
// it will crash.
|
|
|
|
|
//int retTwoNull = two (20.0);
|
2007-02-16 09:56:21 +01:00
|
|
|
NS_ASSERT (two.IsNull ());
|
2006-11-01 13:11:30 +01:00
|
|
|
|
2010-05-10 21:10:15 -07:00
|
|
|
#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
|
|
|
|
|
|
2006-11-01 13:11:30 +01:00
|
|
|
return 0;
|
2006-08-29 17:47:17 +02:00
|
|
|
}
|