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);
224 private:
237  typedef std::list<Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> > CallbackList;
240 };
241 
242 } // namespace ns3
243 
244 
245 /********************************************************************
246  * Implementation of the templates declared above.
247  ********************************************************************/
248 
249 namespace ns3 {
250 
251 template<typename T1, typename T2,
252  typename T3, typename T4,
253  typename T5, typename T6,
254  typename T7, typename T8>
256  : m_callbackList ()
257 {
258 }
259 template<typename T1, typename T2,
260  typename T3, typename T4,
261  typename T5, typename T6,
262  typename T7, typename T8>
263 void
265 {
267  if (!cb.Assign (callback))
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  NS_FATAL_ERROR ("when connecting to " << path);
281  Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> realCb = cb.Bind (path);
282  m_callbackList.push_back (realCb);
283 }
284 template<typename T1, typename T2,
285  typename T3, typename T4,
286  typename T5, typename T6,
287  typename T7, typename T8>
288 void
290 {
291  for (typename CallbackList::iterator i = m_callbackList.begin ();
292  i != m_callbackList.end (); /* empty */)
293  {
294  if ((*i).IsEqual (callback))
295  {
296  i = m_callbackList.erase (i);
297  }
298  else
299  {
300  i++;
301  }
302  }
303 }
304 template<typename T1, typename T2,
305  typename T3, typename T4,
306  typename T5, typename T6,
307  typename T7, typename T8>
308 void
310 {
312  if (!cb.Assign (callback))
313  NS_FATAL_ERROR ("when disconnecting from " << path);
314  Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> realCb = cb.Bind (path);
315  DisconnectWithoutContext (realCb);
316 }
317 template<typename T1, typename T2,
318  typename T3, typename T4,
319  typename T5, typename T6,
320  typename T7, typename T8>
321 void
323 {
324  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
325  i != m_callbackList.end (); i++)
326  {
327  (*i)();
328  }
329 }
330 template<typename T1, typename T2,
331  typename T3, typename T4,
332  typename T5, typename T6,
333  typename T7, typename T8>
334 void
336 {
337  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
338  i != m_callbackList.end (); i++)
339  {
340  (*i)(a1);
341  }
342 }
343 template<typename T1, typename T2,
344  typename T3, typename T4,
345  typename T5, typename T6,
346  typename T7, typename T8>
347 void
349 {
350  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
351  i != m_callbackList.end (); i++)
352  {
353  (*i)(a1, a2);
354  }
355 }
356 template<typename T1, typename T2,
357  typename T3, typename T4,
358  typename T5, typename T6,
359  typename T7, typename T8>
360 void
362 {
363  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
364  i != m_callbackList.end (); i++)
365  {
366  (*i)(a1, a2, a3);
367  }
368 }
369 template<typename T1, typename T2,
370  typename T3, typename T4,
371  typename T5, typename T6,
372  typename T7, typename T8>
373 void
375 {
376  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
377  i != m_callbackList.end (); i++)
378  {
379  (*i)(a1, a2, a3, a4);
380  }
381 }
382 template<typename T1, typename T2,
383  typename T3, typename T4,
384  typename T5, typename T6,
385  typename T7, typename T8>
386 void
387 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) const
388 {
389  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
390  i != m_callbackList.end (); i++)
391  {
392  (*i)(a1, a2, a3, a4, a5);
393  }
394 }
395 template<typename T1, typename T2,
396  typename T3, typename T4,
397  typename T5, typename T6,
398  typename T7, typename T8>
399 void
400 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6) const
401 {
402  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
403  i != m_callbackList.end (); i++)
404  {
405  (*i)(a1, a2, a3, a4, a5, a6);
406  }
407 }
408 template<typename T1, typename T2,
409  typename T3, typename T4,
410  typename T5, typename T6,
411  typename T7, typename T8>
412 void
413 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
414 {
415  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
416  i != m_callbackList.end (); i++)
417  {
418  (*i)(a1, a2, a3, a4, a5, a6, a7);
419  }
420 }
421 template<typename T1, typename T2,
422  typename T3, typename T4,
423  typename T5, typename T6,
424  typename T7, typename T8>
425 void
426 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
427 {
428  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
429  i != m_callbackList.end (); i++)
430  {
431  (*i)(a1, a2, a3, a4, a5, a6, a7, a8);
432  }
433 }
434 
435 } // namespace ns3
436 
437 #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:1221
Base class for Callback class.
Definition: callback.h:1104
void DisconnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:837
#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:1412
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:832
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:843
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:849
std::list< Callback< void, T1, T2, T3, T4, T5, T6, T7, T8 > > CallbackList
Container type for holding the chain of Callbacks.