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 (typename CallbackList::iterator i = m_callbackList.begin(); i != m_callbackList.end();
166 /* empty */)
167 {
168 if ((*i).IsEqual(callback))
169 {
170 i = m_callbackList.erase(i);
171 }
172 else
173 {
174 i++;
175 }
176 }
177}
178
179template <typename... Ts>
180void
181TracedCallback<Ts...>::Disconnect(const CallbackBase& callback, std::string path)
182{
183 Callback<void, std::string, Ts...> cb;
184 if (!cb.Assign(callback))
185 {
186 NS_FATAL_ERROR("when disconnecting from " << path);
187 }
188 Callback<void, Ts...> realCb = cb.Bind(path);
189 DisconnectWithoutContext(realCb);
190}
191
192template <typename... Ts>
193void
195{
196 for (typename CallbackList::const_iterator i = m_callbackList.begin();
197 i != m_callbackList.end();
198 i++)
199 {
200 (*i)(args...);
201 }
202}
203
204template <typename... Ts>
205bool
207{
208 return m_callbackList.empty();
209}
210
211} // namespace ns3
212
213#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:555
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.