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
traced-callback.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2005,2006,2007 INRIA
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7
*/
8
9
#ifndef TRACED_CALLBACK_H
10
#define TRACED_CALLBACK_H
11
12
#include "
callback.h
"
13
14
#include <list>
15
16
/**
17
* @file
18
* @ingroup tracing
19
* ns3::TracedCallback declaration and template implementation.
20
*/
21
22
namespace
ns3
23
{
24
25
/**
26
* @ingroup tracing
27
* @brief Forward calls to a chain of Callback
28
*
29
* A TracedCallback has almost exactly the same API as a normal
30
* Callback but instead of forwarding calls to a single function
31
* (as a Callback normally does), it forwards calls to a chain
32
* of Callback. Connect adds a Callback at the end of the chain
33
* of callbacks. Disconnect removes a Callback from the chain of callbacks.
34
*
35
* This is a functor: the chain of Callbacks is invoked by
36
* calling the \c operator() form with the appropriate
37
* number of arguments.
38
*
39
* @tparam Ts \explicit Types of the functor arguments.
40
*
41
* Inheritance graph was not generated because of its size.
42
* @hideinheritancegraph
43
*/
44
template
<
typename
... Ts>
45
class
TracedCallback
46
{
47
public
:
48
/** Constructor. */
49
TracedCallback
();
50
/**
51
* Append a Callback to the chain (without a context).
52
*
53
* @param [in] callback Callback to add to chain.
54
*/
55
void
ConnectWithoutContext
(
const
CallbackBase
& callback);
56
/**
57
* Append a Callback to the chain with a context.
58
*
59
* The context string will be provided as the first argument
60
* to the Callback.
61
*
62
* @param [in] callback Callback to add to chain.
63
* @param [in] path Context string to provide when invoking the Callback.
64
*/
65
void
Connect
(
const
CallbackBase
& callback, std::string path);
66
/**
67
* Remove from the chain a Callback which was connected without a context.
68
*
69
* @param [in] callback Callback to remove from the chain.
70
*/
71
void
DisconnectWithoutContext
(
const
CallbackBase
& callback);
72
/**
73
* Remove from the chain a Callback which was connected with a context.
74
*
75
* @param [in] callback Callback to remove from the chain.
76
* @param [in] path Context path which was used to connect the Callback.
77
*/
78
void
Disconnect
(
const
CallbackBase
& callback, std::string path);
79
/**
80
* @brief Functor which invokes the chain of Callbacks.
81
* @tparam Ts \deduced Types of the functor arguments.
82
* @param [in] args The arguments to the functor
83
*/
84
void
operator()
(Ts... args)
const
;
85
/**
86
* @brief Checks if the Callbacks list is empty.
87
* @return true if the Callbacks list is empty.
88
*/
89
bool
IsEmpty
()
const
;
90
91
/**
92
* TracedCallback signature for POD.
93
*
94
* @param [in] value Value of the traced variable.
95
* @{
96
*/
97
// Uint32Callback appears to be the only one used at the moment.
98
// Feel free to add typedef's for any other POD you need.
99
typedef
void (*
Uint32Callback
)(
const
uint32_t
value);
100
/**@}*/
101
102
private
:
103
/**
104
* Container type for holding the chain of Callbacks.
105
*
106
* @tparam Ts \deduced Types of the functor arguments.
107
*/
108
typedef
std::list<
Callback
<void, Ts...>>
CallbackList
;
109
/** The chain of Callbacks. */
110
CallbackList
m_callbackList
;
111
};
112
113
}
// namespace ns3
114
115
/********************************************************************
116
* Implementation of the templates declared above.
117
********************************************************************/
118
119
namespace
ns3
120
{
121
122
template
<
typename
... Ts>
123
TracedCallback<Ts...>::TracedCallback
()
124
:
m_callbackList
()
125
{
126
}
127
128
template
<
typename
... Ts>
129
void
130
TracedCallback<Ts...>::ConnectWithoutContext
(
const
CallbackBase
& callback)
131
{
132
Callback
<void, Ts...> cb;
133
if
(!cb.Assign(callback))
134
{
135
NS_FATAL_ERROR_NO_MSG
();
136
}
137
m_callbackList
.push_back(cb);
138
}
139
140
template
<
typename
... Ts>
141
void
142
TracedCallback<Ts...>::Connect
(
const
CallbackBase
& callback, std::string path)
143
{
144
Callback
<void, std::string, Ts...> cb;
145
if
(!cb.Assign(callback))
146
{
147
NS_FATAL_ERROR
(
"when connecting to "
<< path);
148
}
149
Callback
<void, Ts...> realCb = cb.
Bind
(path);
150
m_callbackList
.push_back(realCb);
151
}
152
153
template
<
typename
... Ts>
154
void
155
TracedCallback<Ts...>::DisconnectWithoutContext
(
const
CallbackBase
& callback)
156
{
157
for
(
auto
i =
m_callbackList
.begin(); i !=
m_callbackList
.end();
/* empty */
)
158
{
159
if
((*i).IsEqual(callback))
160
{
161
i =
m_callbackList
.erase(i);
162
}
163
else
164
{
165
i++;
166
}
167
}
168
}
169
170
template
<
typename
... Ts>
171
void
172
TracedCallback<Ts...>::Disconnect
(
const
CallbackBase
& callback, std::string path)
173
{
174
Callback
<void, std::string, Ts...> cb;
175
if
(!cb.Assign(callback))
176
{
177
NS_FATAL_ERROR
(
"when disconnecting from "
<< path);
178
}
179
Callback
<void, Ts...> realCb = cb.
Bind
(path);
180
DisconnectWithoutContext
(realCb);
181
}
182
183
template
<
typename
... Ts>
184
void
185
TracedCallback<Ts...>::operator()
(Ts... args)
const
186
{
187
for
(
auto
i =
m_callbackList
.begin(); i !=
m_callbackList
.end(); i++)
188
{
189
(*i)(args...);
190
}
191
}
192
193
template
<
typename
... Ts>
194
bool
195
TracedCallback<Ts...>::IsEmpty
()
const
196
{
197
return
m_callbackList
.empty();
198
}
199
200
}
// namespace ns3
201
202
#endif
/* TRACED_CALLBACK_H */
callback.h
Declaration of the various callback functions.
ns3::CallbackBase
Base class for Callback class.
Definition
callback.h:347
ns3::Callback
Callback template class.
Definition
callback.h:428
ns3::Callback::Bind
auto Bind(BoundArgs &&... bargs)
Bind a variable number of arguments.
Definition
callback.h:549
ns3::TracedCallback< RouteChange >::m_callbackList
CallbackList m_callbackList
Definition
traced-callback.h:110
ns3::TracedCallback::Uint32Callback
void(* Uint32Callback)(const uint32_t value)
TracedCallback signature for POD.
Definition
traced-callback.h:99
ns3::TracedCallback::Disconnect
void Disconnect(const CallbackBase &callback, std::string path)
Remove from the chain a Callback which was connected with a context.
Definition
traced-callback.h:172
ns3::TracedCallback::ConnectWithoutContext
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
Definition
traced-callback.h:130
ns3::TracedCallback::TracedCallback
TracedCallback()
Constructor.
Definition
traced-callback.h:123
ns3::TracedCallback::IsEmpty
bool IsEmpty() const
Checks if the Callbacks list is empty.
Definition
traced-callback.h:195
ns3::TracedCallback::CallbackList
std::list< Callback< void, Ts... > > CallbackList
Container type for holding the chain of Callbacks.
Definition
traced-callback.h:108
ns3::TracedCallback::operator()
void operator()(Ts... args) const
Functor which invokes the chain of Callbacks.
Definition
traced-callback.h:185
ns3::TracedCallback::DisconnectWithoutContext
void DisconnectWithoutContext(const CallbackBase &callback)
Remove from the chain a Callback which was connected without a context.
Definition
traced-callback.h:155
ns3::TracedCallback::Connect
void Connect(const CallbackBase &callback, std::string path)
Append a Callback to the chain with a context.
Definition
traced-callback.h:142
uint32_t
NS_FATAL_ERROR
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition
fatal-error.h:186
NS_FATAL_ERROR_NO_MSG
#define NS_FATAL_ERROR_NO_MSG()
Report a fatal error and terminate.
Definition
fatal-error.h:149
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
core
model
traced-callback.h
Generated on
for ns-3 by
1.15.0