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 "ns3/command-line.h"
23 #include <iostream>
24 
34 using namespace ns3;
35 
36 namespace {
37 
45 static double
46 CbOne (double a, double b)
47 {
48  std::cout << "invoke cbOne a=" << a << ", b=" << b << std::endl;
49  return a;
50 }
51 
53 class MyCb {
54 public:
61  int CbTwo (double a) {
62  std::cout << "invoke cbTwo a=" << a << std::endl;
63  return -5;
64  }
65 };
66 
67 } // unnamed namespace
68 
69 
70 int main (int argc, char *argv[])
71 {
73  cmd.Parse(argc, argv);
74 
75  // return type: double
76  // first arg type: double
77  // second arg type: double
79  // build callback instance which points to cbOne function
80  one = MakeCallback (&CbOne);
81  // this is not a null callback
82  NS_ASSERT (!one.IsNull ());
83  // invoke cbOne function through callback instance
84  double retOne;
85  retOne = one (10.0, 20.0);
86  // callback returned expected value
87  NS_ASSERT (retOne == 10.0);
88 
89  // return type: int
90  // first arg type: double
92  MyCb cb;
93  // build callback instance which points to MyCb::cbTwo
94  two = MakeCallback (&MyCb::CbTwo, &cb);
95  // this is not a null callback
96  NS_ASSERT (!two.IsNull ());
97  // invoke MyCb::cbTwo through callback instance
98  int retTwo;
99  retTwo = two (10.0);
100  // callback returned expected value
101  NS_ASSERT (retTwo == -5);
102 
103  two = MakeNullCallback<int, double> ();
104  // invoking a null callback is just like
105  // invoking a null function pointer:
106  // it will crash.
107  //int retTwoNull = two (20.0);
108  NS_ASSERT (two.IsNull ());
109 
110 #if 0
111  // The below type mismatch between CbOne() and callback two will fail to
112  // compile if enabled in this program.
113  two = MakeCallback (&CbOne);
114 #endif
115 
116 #if 0
117  // This is a slightly different example, in which the code will compile
118  // but because callbacks are type-safe, will cause a fatal error at runtime
119  // (the difference here is that Assign() is called instead of operator=)
120  Callback<void, float> three;
121  three.Assign (MakeCallback (&CbOne));
122 #endif
123 
124  return 0;
125 }
Callback template class.
Definition: callback.h:1176
#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
cmd
Definition: second.py:35
bool Assign(const CallbackBase &other)
Adopt the other&#39;s implementation, if type compatible.
Definition: callback.h:1412
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
Parse command-line arguments.
Definition: command-line.h:213
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270