A Discrete-Event Network Simulator
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
22namespace 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 */
44template <typename... Ts>
46{
47 public:
48 /** Constructor. */
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 */
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. */
111};
112
113} // namespace ns3
114
115/********************************************************************
116 * Implementation of the templates declared above.
117 ********************************************************************/
118
119namespace ns3
120{
121
122template <typename... Ts>
127
128template <typename... Ts>
129void
131{
132 Callback<void, Ts...> cb;
133 if (!cb.Assign(callback))
134 {
136 }
137 m_callbackList.push_back(cb);
138}
139
140template <typename... Ts>
141void
142TracedCallback<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
153template <typename... Ts>
154void
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
170template <typename... Ts>
171void
172TracedCallback<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);
181}
182
183template <typename... Ts>
184void
186{
187 for (auto i = m_callbackList.begin(); i != m_callbackList.end(); i++)
188 {
189 (*i)(args...);
190 }
191}
192
193template <typename... Ts>
194bool
196{
197 return m_callbackList.empty();
198}
199
200} // namespace ns3
201
202#endif /* TRACED_CALLBACK_H */
Declaration of the various callback functions.
Base class for Callback class.
Definition callback.h:347
Callback template class.
Definition callback.h:428
auto Bind(BoundArgs &&... bargs)
Bind a variable number of arguments.
Definition callback.h:549
void(* Uint32Callback)(const uint32_t value)
TracedCallback signature for POD.
void Disconnect(const CallbackBase &callback, std::string path)
Remove from the chain a Callback which was connected with a context.
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
TracedCallback()
Constructor.
bool IsEmpty() const
Checks if the Callbacks list is empty.
std::list< Callback< void, Ts... > > CallbackList
Container type for holding the chain of Callbacks.
void operator()(Ts... args) const
Functor which invokes the chain of Callbacks.
void DisconnectWithoutContext(const CallbackBase &callback)
Remove from the chain a Callback which was connected without a context.
void Connect(const CallbackBase &callback, std::string path)
Append a Callback to the chain with a context.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_FATAL_ERROR_NO_MSG()
Report a fatal error and terminate.
Every class exported by the ns3 library is enclosed in the ns3 namespace.