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
template
<
typename
... Ts>
42
class
TracedCallback
43
{
44
public
:
45
/** Constructor. */
46
TracedCallback
();
47
/**
48
* Append a Callback to the chain (without a context).
49
*
50
* \param [in] callback Callback to add to chain.
51
*/
52
void
ConnectWithoutContext
(
const
CallbackBase
& callback);
53
/**
54
* Append a Callback to the chain with a context.
55
*
56
* The context string will be provided as the first argument
57
* to the Callback.
58
*
59
* \param [in] callback Callback to add to chain.
60
* \param [in] path Context string to provide when invoking the Callback.
61
*/
62
void
Connect
(
const
CallbackBase
& callback, std::string path);
63
/**
64
* Remove from the chain a Callback which was connected without a context.
65
*
66
* \param [in] callback Callback to remove from the chain.
67
*/
68
void
DisconnectWithoutContext
(
const
CallbackBase
& callback);
69
/**
70
* Remove from the chain a Callback which was connected with a context.
71
*
72
* \param [in] callback Callback to remove from the chain.
73
* \param [in] path Context path which was used to connect the Callback.
74
*/
75
void
Disconnect
(
const
CallbackBase
& callback, std::string path);
76
/**
77
* \brief Functor which invokes the chain of Callbacks.
78
* \tparam Ts \deduced Types of the functor arguments.
79
* \param [in] args The arguments to the functor
80
*/
81
void
operator()
(Ts... args)
const
;
82
/**
83
* \brief Checks if the Callbacks list is empty.
84
* \return true if the Callbacks list is empty.
85
*/
86
bool
IsEmpty
()
const
;
87
88
/**
89
* TracedCallback signature for POD.
90
*
91
* \param [in] value Value of the traced variable.
92
* @{
93
*/
94
// Uint32Callback appears to be the only one used at the moment.
95
// Feel free to add typedef's for any other POD you need.
96
typedef
void (*
Uint32Callback
)(
const
uint32_t
value);
97
/**@}*/
98
99
private
:
100
/**
101
* Container type for holding the chain of Callbacks.
102
*
103
* \tparam Ts \deduced Types of the functor arguments.
104
*/
105
typedef
std::list<
Callback
<void, Ts...>>
CallbackList
;
106
/** The chain of Callbacks. */
107
CallbackList
m_callbackList
;
108
};
109
110
}
// namespace ns3
111
112
/********************************************************************
113
* Implementation of the templates declared above.
114
********************************************************************/
115
116
namespace
ns3
117
{
118
119
template
<
typename
... Ts>
120
TracedCallback<Ts...>::TracedCallback
()
121
: m_callbackList()
122
{
123
}
124
125
template
<
typename
... Ts>
126
void
127
TracedCallback<Ts...>::ConnectWithoutContext
(
const
CallbackBase
& callback)
128
{
129
Callback
<void, Ts...> cb;
130
if
(!cb.Assign(callback))
131
{
132
NS_FATAL_ERROR_NO_MSG
();
133
}
134
m_callbackList.push_back(cb);
135
}
136
137
template
<
typename
... Ts>
138
void
139
TracedCallback<Ts...>::Connect
(
const
CallbackBase
& callback, std::string path)
140
{
141
Callback
<void, std::string, Ts...> cb;
142
if
(!cb.Assign(callback))
143
{
144
NS_FATAL_ERROR
(
"when connecting to "
<< path);
145
}
146
Callback
<void, Ts...> realCb = cb.
Bind
(path);
147
m_callbackList.push_back(realCb);
148
}
149
150
template
<
typename
... Ts>
151
void
152
TracedCallback<Ts...>::DisconnectWithoutContext
(
const
CallbackBase
& callback)
153
{
154
for
(
auto
i = m_callbackList.begin(); i != m_callbackList.end();
/* empty */
)
155
{
156
if
((*i).IsEqual(callback))
157
{
158
i = m_callbackList.erase(i);
159
}
160
else
161
{
162
i++;
163
}
164
}
165
}
166
167
template
<
typename
... Ts>
168
void
169
TracedCallback<Ts...>::Disconnect
(
const
CallbackBase
& callback, std::string path)
170
{
171
Callback
<void, std::string, Ts...> cb;
172
if
(!cb.Assign(callback))
173
{
174
NS_FATAL_ERROR
(
"when disconnecting from "
<< path);
175
}
176
Callback
<void, Ts...> realCb = cb.
Bind
(path);
177
DisconnectWithoutContext(realCb);
178
}
179
180
template
<
typename
... Ts>
181
void
182
TracedCallback<Ts...>::operator()
(Ts... args)
const
183
{
184
for
(
auto
i = m_callbackList.begin(); i != m_callbackList.end(); i++)
185
{
186
(*i)(args...);
187
}
188
}
189
190
template
<
typename
... Ts>
191
bool
192
TracedCallback<Ts...>::IsEmpty
()
const
193
{
194
return
m_callbackList.empty();
195
}
196
197
}
// namespace ns3
198
199
#endif
/* TRACED_CALLBACK_H */
callback.h
Declaration of the various callback functions.
ns3::CallbackBase
Base class for Callback class.
Definition
callback.h:344
ns3::Callback
Callback template class.
Definition
callback.h:422
ns3::Callback::Bind
auto Bind(BoundArgs &&... bargs)
Bind a variable number of arguments.
Definition
callback.h:543
ns3::TracedCallback
Forward calls to a chain of Callback.
Definition
traced-callback.h:43
ns3::TracedCallback::m_callbackList
CallbackList m_callbackList
The chain of Callbacks.
Definition
traced-callback.h:107
ns3::TracedCallback::Uint32Callback
void(* Uint32Callback)(const uint32_t value)
TracedCallback signature for POD.
Definition
traced-callback.h:96
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:169
ns3::TracedCallback::ConnectWithoutContext
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
Definition
traced-callback.h:127
ns3::TracedCallback::TracedCallback
TracedCallback()
Constructor.
Definition
traced-callback.h:120
ns3::TracedCallback::IsEmpty
bool IsEmpty() const
Checks if the Callbacks list is empty.
Definition
traced-callback.h:192
ns3::TracedCallback::CallbackList
std::list< Callback< void, Ts... > > CallbackList
Container type for holding the chain of Callbacks.
Definition
traced-callback.h:105
ns3::TracedCallback::operator()
void operator()(Ts... args) const
Functor which invokes the chain of Callbacks.
Definition
traced-callback.h:182
ns3::TracedCallback::DisconnectWithoutContext
void DisconnectWithoutContext(const CallbackBase &callback)
Remove from the chain a Callback which was connected without a context.
Definition
traced-callback.h:152
ns3::TracedCallback::Connect
void Connect(const CallbackBase &callback, std::string path)
Append a Callback to the chain with a context.
Definition
traced-callback.h:139
uint32_t
NS_FATAL_ERROR
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition
fatal-error.h:168
NS_FATAL_ERROR_NO_MSG
#define NS_FATAL_ERROR_NO_MSG()
Report a fatal error and terminate.
Definition
fatal-error.h:131
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
core
model
traced-callback.h
Generated on Fri Nov 8 2024 13:58:59 for ns-3 by
1.11.0