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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19
20#ifndef TRACED_CALLBACK_H
21#define TRACED_CALLBACK_H
22
23#include "callback.h"
24
25#include <list>
26
33namespace ns3
34{
35
52template <typename... Ts>
54{
55 public:
63 void ConnectWithoutContext(const CallbackBase& callback);
73 void Connect(const CallbackBase& callback, std::string path);
86 void Disconnect(const CallbackBase& callback, std::string path);
92 void operator()(Ts... args) const;
97 bool IsEmpty() const;
98
105 // Uint32Callback appears to be the only one used at the moment.
106 // Feel free to add typedef's for any other POD you need.
107 typedef void (*Uint32Callback)(const uint32_t value);
110 private:
116 typedef std::list<Callback<void, Ts...>> CallbackList;
119};
120
121} // namespace ns3
122
123/********************************************************************
124 * Implementation of the templates declared above.
125 ********************************************************************/
126
127namespace ns3
128{
129
130template <typename... Ts>
132 : m_callbackList()
133{
134}
135
136template <typename... Ts>
137void
139{
140 Callback<void, Ts...> cb;
141 if (!cb.Assign(callback))
142 {
144 }
145 m_callbackList.push_back(cb);
146}
147
148template <typename... Ts>
149void
150TracedCallback<Ts...>::Connect(const CallbackBase& callback, std::string path)
151{
152 Callback<void, std::string, Ts...> cb;
153 if (!cb.Assign(callback))
154 {
155 NS_FATAL_ERROR("when connecting to " << path);
156 }
157 Callback<void, Ts...> realCb = cb.Bind(path);
158 m_callbackList.push_back(realCb);
159}
160
161template <typename... Ts>
162void
164{
165 for (auto i = m_callbackList.begin(); i != m_callbackList.end(); /* empty */)
166 {
167 if ((*i).IsEqual(callback))
168 {
169 i = m_callbackList.erase(i);
170 }
171 else
172 {
173 i++;
174 }
175 }
176}
177
178template <typename... Ts>
179void
180TracedCallback<Ts...>::Disconnect(const CallbackBase& callback, std::string path)
181{
182 Callback<void, std::string, Ts...> cb;
183 if (!cb.Assign(callback))
184 {
185 NS_FATAL_ERROR("when disconnecting from " << path);
186 }
187 Callback<void, Ts...> realCb = cb.Bind(path);
188 DisconnectWithoutContext(realCb);
189}
190
191template <typename... Ts>
192void
194{
195 for (auto i = m_callbackList.begin(); i != m_callbackList.end(); i++)
196 {
197 (*i)(args...);
198 }
199}
200
201template <typename... Ts>
202bool
204{
205 return m_callbackList.empty();
206}
207
208} // namespace ns3
209
210#endif /* TRACED_CALLBACK_H */
Declaration of the various callback functions.
Base class for Callback class.
Definition: callback.h:360
Callback template class.
Definition: callback.h:438
auto Bind(BoundArgs &&... bargs)
Bind a variable number of arguments.
Definition: callback.h:557
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.
Definition: fatal-error.h:179
#define NS_FATAL_ERROR_NO_MSG()
Report a fatal error and terminate.
Definition: fatal-error.h:142
Every class exported by the ns3 library is enclosed in the ns3 namespace.