This documentation is not the
Latest Release
.
A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
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
69
Callback<double, double, double>
one;
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
82
Callback<int, double>
two;
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
}
ns3::Callback
Callback template class.
Definition:
callback.h:1164
ns3::Callback::IsNull
bool IsNull(void) const
Check for null implementation.
Definition:
callback.h:1258
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:67
ns3::Callback::Assign
bool Assign(const CallbackBase &other)
Adopt the other's implementation, if type compatible.
Definition:
callback.h:1399
CbOne
static double CbOne(double a, double b)
Example Callback function.
Definition:
main-callback.cc:42
ns3::MakeCallback
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition:
callback.h:1480
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
MyCb
Example Callback class.
Definition:
main-callback.cc:49
MyCb::CbTwo
int CbTwo(double a)
Example Callback class method.
Definition:
main-callback.cc:57
src
core
examples
main-callback.cc
Generated on Wed Nov 11 2015 20:00:26 for ns-3 by
1.8.9.1