A Discrete-Event Network Simulator
API
traced-callback.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006,2007 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #ifndef TRACED_CALLBACK_H
22 #define TRACED_CALLBACK_H
23 
24 #include <list>
25 #include "callback.h"
26 
33 namespace ns3 {
34 
58 template<typename T1 = empty, typename T2 = empty,
59  typename T3 = empty, typename T4 = empty,
60  typename T5 = empty, typename T6 = empty,
61  typename T7 = empty, typename T8 = empty>
63 {
64 public:
66  TracedCallback ();
72  void ConnectWithoutContext (const CallbackBase & callback);
82  void Connect (const CallbackBase & callback, std::string path);
88  void DisconnectWithoutContext (const CallbackBase & callback);
95  void Disconnect (const CallbackBase & callback, std::string path);
105  void operator() (void) const;
111  void operator() (T1 a1) const;
119  void operator() (T1 a1, T2 a2) const;
129  void operator() (T1 a1, T2 a2, T3 a3) const;
141  void operator() (T1 a1, T2 a2, T3 a3, T4 a4) const;
155  void operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) const;
171  void operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6) const;
189  void operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7) const;
209  void operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7, T8 a8) const;
218  // Uint32Callback appears to be the only one used at the moment.
219  // Feel free to add typedef's for any other POD you need.
220  typedef void (* Uint32Callback)(const uint32_t value);
223 private:
236  typedef std::list<Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> > CallbackList;
239 };
240 
241 } // namespace ns3
242 
243 
244 /********************************************************************
245  * Implementation of the templates declared above.
246  ********************************************************************/
247 
248 namespace ns3 {
249 
250 template<typename T1, typename T2,
251  typename T3, typename T4,
252  typename T5, typename T6,
253  typename T7, typename T8>
255  : m_callbackList ()
256 {}
257 template<typename T1, typename T2,
258  typename T3, typename T4,
259  typename T5, typename T6,
260  typename T7, typename T8>
261 void
263 {
265  if (!cb.Assign (callback))
266  {
268  }
269  m_callbackList.push_back (cb);
270 }
271 template<typename T1, typename T2,
272  typename T3, typename T4,
273  typename T5, typename T6,
274  typename T7, typename T8>
275 void
277 {
279  if (!cb.Assign (callback))
280  {
281  NS_FATAL_ERROR ("when connecting to " << path);
282  }
283  Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> realCb = cb.Bind (path);
284  m_callbackList.push_back (realCb);
285 }
286 template<typename T1, typename T2,
287  typename T3, typename T4,
288  typename T5, typename T6,
289  typename T7, typename T8>
290 void
292 {
293  for (typename CallbackList::iterator i = m_callbackList.begin ();
294  i != m_callbackList.end (); /* empty */)
295  {
296  if ((*i).IsEqual (callback))
297  {
298  i = m_callbackList.erase (i);
299  }
300  else
301  {
302  i++;
303  }
304  }
305 }
306 template<typename T1, typename T2,
307  typename T3, typename T4,
308  typename T5, typename T6,
309  typename T7, typename T8>
310 void
312 {
314  if (!cb.Assign (callback))
315  {
316  NS_FATAL_ERROR ("when disconnecting from " << path);
317  }
318  Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> realCb = cb.Bind (path);
319  DisconnectWithoutContext (realCb);
320 }
321 template<typename T1, typename T2,
322  typename T3, typename T4,
323  typename T5, typename T6,
324  typename T7, typename T8>
325 void
327 {
328  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
329  i != m_callbackList.end (); i++)
330  {
331  (*i)();
332  }
333 }
334 template<typename T1, typename T2,
335  typename T3, typename T4,
336  typename T5, typename T6,
337  typename T7, typename T8>
338 void
340 {
341  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
342  i != m_callbackList.end (); i++)
343  {
344  (*i)(a1);
345  }
346 }
347 template<typename T1, typename T2,
348  typename T3, typename T4,
349  typename T5, typename T6,
350  typename T7, typename T8>
351 void
353 {
354  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
355  i != m_callbackList.end (); i++)
356  {
357  (*i)(a1, a2);
358  }
359 }
360 template<typename T1, typename T2,
361  typename T3, typename T4,
362  typename T5, typename T6,
363  typename T7, typename T8>
364 void
366 {
367  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
368  i != m_callbackList.end (); i++)
369  {
370  (*i)(a1, a2, a3);
371  }
372 }
373 template<typename T1, typename T2,
374  typename T3, typename T4,
375  typename T5, typename T6,
376  typename T7, typename T8>
377 void
379 {
380  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
381  i != m_callbackList.end (); i++)
382  {
383  (*i)(a1, a2, a3, a4);
384  }
385 }
386 template<typename T1, typename T2,
387  typename T3, typename T4,
388  typename T5, typename T6,
389  typename T7, typename T8>
390 void
391 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) const
392 {
393  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
394  i != m_callbackList.end (); i++)
395  {
396  (*i)(a1, a2, a3, a4, a5);
397  }
398 }
399 template<typename T1, typename T2,
400  typename T3, typename T4,
401  typename T5, typename T6,
402  typename T7, typename T8>
403 void
404 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6) const
405 {
406  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
407  i != m_callbackList.end (); i++)
408  {
409  (*i)(a1, a2, a3, a4, a5, a6);
410  }
411 }
412 template<typename T1, typename T2,
413  typename T3, typename T4,
414  typename T5, typename T6,
415  typename T7, typename T8>
416 void
417 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7) const
418 {
419  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
420  i != m_callbackList.end (); i++)
421  {
422  (*i)(a1, a2, a3, a4, a5, a6, a7);
423  }
424 }
425 template<typename T1, typename T2,
426  typename T3, typename T4,
427  typename T5, typename T6,
428  typename T7, typename T8>
429 void
430 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7, T8 a8) const
431 {
432  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
433  i != m_callbackList.end (); i++)
434  {
435  (*i)(a1, a2, a3, a4, a5, a6, a7, a8);
436  }
437 }
438 
439 } // namespace ns3
440 
441 #endif /* TRACED_CALLBACK_H */
Forward calls to a chain of Callback.
Callback< R, T2, T3, T4, T5, T6, T7, T8, T9 > Bind(T a)
Bind the first arguments.
Definition: callback.h:1320
Base class for Callback class.
Definition: callback.h:1194
void DisconnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:867
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
bool Assign(const CallbackBase &other)
Adopt the other&#39;s implementation, if type compatible.
Definition: callback.h:1528
Declaration of the various callback functions.
void(* Uint32Callback)(const uint32_t value)
TracedCallback signature for POD.
void DisconnectWithoutContext(const CallbackBase &callback)
Remove from the chain a Callback which was connected without a context.
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:862
void Disconnect(const CallbackBase &callback, std::string path)
Remove from the chain a Callback which was connected with a context.
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:873
Every class exported by the ns3 library is enclosed in the ns3 namespace.
#define NS_FATAL_ERROR_NO_MSG()
Report a fatal error and terminate.
Definition: fatal-error.h:125
void Connect(const CallbackBase &callback, std::string path)
Append a Callback to the chain with a context.
CallbackList m_callbackList
The chain of Callbacks.
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
void operator()(void) const
Functor which invokes the chain of Callbacks.
TracedCallback()
Constructor.
void Disconnect(std::string path, const CallbackBase &cb)
Definition: config.cc:879
std::list< Callback< void, T1, T2, T3, T4, T5, T6, T7, T8 > > CallbackList
Container type for holding the chain of Callbacks.