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