A Discrete-Event Network Simulator
API
main-callback.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  */
19 
20 #include "ns3/callback.h"
21 #include "ns3/assert.h"
22 #include <iostream>
23 
33 using namespace ns3;
34 
35 namespace {
36 
44 static double
45 CbOne (double a, double b)
46 {
47  std::cout << "invoke cbOne a=" << a << ", b=" << b << std::endl;
48  return a;
49 }
50 
52 class MyCb {
53 public:
60  int CbTwo (double a) {
61  std::cout << "invoke cbTwo a=" << a << std::endl;
62  return -5;
63  }
64 };
65 
66 } // unnamed namespace
67 
68 
69 int main (int argc, char *argv[])
70 {
71  // return type: double
72  // first arg type: double
73  // second arg type: double
75  // build callback instance which points to cbOne function
76  one = MakeCallback (&CbOne);
77  // this is not a null callback
78  NS_ASSERT (!one.IsNull ());
79  // invoke cbOne function through callback instance
80  double retOne;
81  retOne = one (10.0, 20.0);
82  // callback returned expected value
83  NS_ASSERT (retOne == 10.0);
84 
85  // return type: int
86  // first arg type: double
88  MyCb cb;
89  // build callback instance which points to MyCb::cbTwo
90  two = MakeCallback (&MyCb::CbTwo, &cb);
91  // this is not a null callback
92  NS_ASSERT (!two.IsNull ());
93  // invoke MyCb::cbTwo through callback instance
94  int retTwo;
95  retTwo = two (10.0);
96  // callback returned expected value
97  NS_ASSERT (retTwo == -5);
98 
99  two = MakeNullCallback<int, double> ();
100  // invoking a null callback is just like
101  // invoking a null function pointer:
102  // it will crash.
103  //int retTwoNull = two (20.0);
104  NS_ASSERT (two.IsNull ());
105 
106 #if 0
107  // The below type mismatch between CbOne() and callback two will fail to
108  // compile if enabled in this program.
109  two = MakeCallback (&CbOne);
110 #endif
111 
112 #if 0
113  // This is a slightly different example, in which the code will compile
114  // but because callbacks are type-safe, will cause a fatal error at runtime
115  // (the difference here is that Assign() is called instead of operator=)
116  Callback<void, float> three;
117  three.Assign (MakeCallback (&CbOne));
118 #endif
119 
120  return 0;
121 }
Callback template class.
Definition: callback.h:1176
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
bool Assign(const CallbackBase &other)
Adopt the other's implementation, if type compatible.
Definition: callback.h:1412
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
Every class exported by the ns3 library is enclosed in the ns3 namespace.