This documentation is not the Latest Release.
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 
32 using namespace ns3;
33 
41 static double
42 CbOne (double a, double b)
43 {
44  std::cout << "invoke cbOne a=" << a << ", b=" << b << std::endl;
45  return a;
46 }
47 
49 class MyCb {
50 public:
57  int CbTwo (double a) {
58  std::cout << "invoke cbTwo a=" << a << std::endl;
59  return -5;
60  }
61 };
62 
63 
64 int main (int argc, char *argv[])
65 {
66  // return type: double
67  // first arg type: double
68  // second arg type: double
70  // build callback instance which points to cbOne function
71  one = MakeCallback (&CbOne);
72  // this is not a null callback
73  NS_ASSERT (!one.IsNull ());
74  // invoke cbOne function through callback instance
75  double retOne;
76  retOne = one (10.0, 20.0);
77  // callback returned expected value
78  NS_ASSERT (retOne == 10.0);
79 
80  // return type: int
81  // first arg type: double
83  MyCb cb;
84  // build callback instance which points to MyCb::cbTwo
85  two = MakeCallback (&MyCb::CbTwo, &cb);
86  // this is not a null callback
87  NS_ASSERT (!two.IsNull ());
88  // invoke MyCb::cbTwo through callback instance
89  int retTwo;
90  retTwo = two (10.0);
91  // callback returned expected value
92  NS_ASSERT (retTwo == -5);
93 
94  two = MakeNullCallback<int, double> ();
95  // invoking a null callback is just like
96  // invoking a null function pointer:
97  // it will crash.
98  //int retTwoNull = two (20.0);
99  NS_ASSERT (two.IsNull ());
100 
101 #if 0
102  // The below type mismatch between CbOne() and callback two will fail to
103  // compile if enabled in this program.
104  two = MakeCallback (&CbOne);
105 #endif
106 
107 #if 0
108  // This is a slightly different example, in which the code will compile
109  // but because callbacks are type-safe, will cause a fatal error at runtime
110  // (the difference here is that Assign() is called instead of operator=)
111  Callback<void, float> three;
112  three.Assign (MakeCallback (&CbOne));
113 #endif
114 
115  return 0;
116 }
Callback template class.
Definition: callback.h:1164
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1258
#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:1399
static double CbOne(double a, double b)
Example Callback function.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1480
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Example Callback class.
int CbTwo(double a)
Example Callback class method.