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 */
41template <typename... Ts>
43{
44 public:
45 /** Constructor. */
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 */
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. */
108};
109
110} // namespace ns3
111
112/********************************************************************
113 * Implementation of the templates declared above.
114 ********************************************************************/
115
116namespace ns3
117{
118
119template <typename... Ts>
121 : m_callbackList()
122{
123}
124
125template <typename... Ts>
126void
128{
129 Callback<void, Ts...> cb;
130 if (!cb.Assign(callback))
131 {
133 }
134 m_callbackList.push_back(cb);
135}
136
137template <typename... Ts>
138void
139TracedCallback<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
150template <typename... Ts>
151void
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
167template <typename... Ts>
168void
169TracedCallback<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
180template <typename... Ts>
181void
183{
184 for (auto i = m_callbackList.begin(); i != m_callbackList.end(); i++)
185 {
186 (*i)(args...);
187 }
188}
189
190template <typename... Ts>
191bool
193{
194 return m_callbackList.empty();
195}
196
197} // namespace ns3
198
199#endif /* TRACED_CALLBACK_H */
Declaration of the various callback functions.
Base class for Callback class.
Definition callback.h:344
Callback template class.
Definition callback.h:422
auto Bind(BoundArgs &&... bargs)
Bind a variable number of arguments.
Definition callback.h:543
Forward calls to a chain of Callback.
CallbackList m_callbackList
The chain of Callbacks.
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.