A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
main-callback.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2006 INRIA
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
*/
7
8
#include "ns3/assert.h"
9
#include "ns3/callback.h"
10
#include "ns3/command-line.h"
11
12
#include <iostream>
13
14
/**
15
* \file
16
* \ingroup core-examples
17
* \ingroup callback
18
* Example program illustrating use of callback functions and methods.
19
*
20
* See \ref callback
21
*/
22
23
using namespace
ns3
;
24
25
namespace
26
{
27
28
/**
29
* Example Callback function.
30
*
31
* \param [in] a The first argument.
32
* \param [in] b The second argument.
33
* \returns The first argument.
34
*/
35
double
36
CbOne(
double
a,
double
b)
37
{
38
std::cout <<
"invoke cbOne a="
<< a <<
", b="
<< b << std::endl;
39
return
a;
40
}
41
42
/** Example Callback class. */
43
class
MyCb
44
{
45
public
:
46
/**
47
* Example Callback class method.
48
*
49
* \param [in] a The argument.
50
* \returns -5
51
*/
52
int
CbTwo(
double
a)
53
{
54
std::cout <<
"invoke cbTwo a="
<< a << std::endl;
55
return
-5;
56
}
57
};
58
59
}
// unnamed namespace
60
61
int
62
main(
int
argc,
char
* argv[])
63
{
64
CommandLine
cmd(__FILE__);
65
cmd.Parse(argc, argv);
66
67
// return type: double
68
// first arg type: double
69
// second arg type: double
70
Callback<double, double, double>
one;
71
// build callback instance which points to cbOne function
72
one =
MakeCallback
(&CbOne);
73
// this is not a null callback
74
NS_ASSERT
(!one.
IsNull
());
75
// invoke cbOne function through callback instance
76
double
retOne;
77
retOne = one(10.0, 20.0);
78
// callback returned expected value
79
NS_ASSERT
(retOne == 10.0);
80
81
// return type: int
82
// first arg type: double
83
Callback<int, double>
two
;
84
MyCb cb;
85
// build callback instance which points to MyCb::cbTwo
86
two
=
MakeCallback
(&MyCb::CbTwo, &cb);
87
// this is not a null callback
88
NS_ASSERT
(!
two
.IsNull());
89
// invoke MyCb::cbTwo through callback instance
90
int
retTwo;
91
retTwo =
two
(10.0);
92
// callback returned expected value
93
NS_ASSERT
(retTwo == -5);
94
95
two
=
MakeNullCallback<int, double>
();
96
// invoking a null callback is just like
97
// invoking a null function pointer:
98
// it will crash.
99
// int retTwoNull = two (20.0);
100
NS_ASSERT
(
two
.IsNull());
101
102
#if 0
103
// The below type mismatch between CbOne() and callback two will fail to
104
// compile if enabled in this program.
105
two
=
MakeCallback
(&CbOne);
106
#endif
107
108
#if 0
109
// This is a slightly different example, in which the code will compile
110
// but because callbacks are type-safe, will cause a fatal error at runtime
111
// (the difference here is that Assign() is called instead of operator=)
112
Callback<void, float>
three;
113
three.
Assign
(
MakeCallback
(&CbOne));
114
#endif
115
116
return
0;
117
}
ns3::Callback
Callback template class.
Definition
callback.h:422
ns3::Callback::IsNull
bool IsNull() const
Check for null implementation.
Definition
callback.h:555
ns3::Callback::Assign
bool Assign(const CallbackBase &other)
Adopt the other's implementation, if type compatible.
Definition
callback.h:605
ns3::CommandLine
Parse command-line arguments.
Definition
command-line.h:221
NS_ASSERT
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition
assert.h:55
ns3::MakeNullCallback
Callback< R, Args... > MakeNullCallback()
Definition
callback.h:727
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::MakeCallback
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:684
two
-ray-to-three-gpp-ch-calibration
src
core
examples
main-callback.cc
Generated on Fri Nov 8 2024 13:58:59 for ns-3 by
1.11.0