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