src/core/event-trace-source.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Fri, 22 Feb 2008 00:22:16 +0100
changeset 2466 8e4ee14cc574
parent 2462 93028d4474ba
permissions -rw-r--r--
make EventTraceSource acceptable to the MakeTraceSourceAccessor function
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2005,2006,2007 INRIA
     4  * All rights reserved.
     5  *
     6  * This program is free software; you can redistribute it and/or modify
     7  * it under the terms of the GNU General Public License version 2 as
     8  * published by the Free Software Foundation;
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program; if not, write to the Free Software
    17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18  *
    19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    20  */
    21 
    22 #ifndef EVENT_TRACE_SOURCE_H
    23 #define EVENT_TRACE_SOURCE_H
    24 
    25 #include <list>
    26 #include "callback.h"
    27 
    28 namespace ns3 {
    29 
    30 /**
    31  * \brief log arbitrary number of parameters to a matching ns3::Callback
    32  * \ingroup tracing
    33  *
    34  * Whenever operator () is invoked on this class, the call and its arguments
    35  * are forwarded to the internal matching ns3::Callback.
    36  */
    37 template<typename T1 = empty, typename T2 = empty, 
    38          typename T3 = empty, typename T4 = empty>
    39 class EventTraceSource 
    40 {
    41 public:
    42   EventTraceSource ();
    43   void Connect (const CallbackBase & callback);
    44   void Disconnect (const CallbackBase & callback);
    45   void operator() (void) const;
    46   void operator() (T1 a1) const;
    47   void operator() (T1 a1, T2 a2) const;
    48   void operator() (T1 a1, T2 a2, T3 a3) const;
    49   void operator() (T1 a1, T2 a2, T3 a3, T4 a4) const;
    50 
    51 private:
    52   typedef std::list<Callback<void,T1,T2,T3,T4> > CallbackList;
    53   CallbackList m_callbackList;
    54 };
    55 
    56 } // namespace ns3
    57 
    58 // implementation below.
    59 
    60 namespace ns3 {
    61 
    62 
    63 template<typename T1, typename T2, 
    64          typename T3, typename T4>
    65 EventTraceSource<T1,T2,T3,T4>::EventTraceSource ()
    66   : m_callbackList () 
    67 {}
    68 template<typename T1, typename T2, 
    69          typename T3, typename T4>
    70 void 
    71 EventTraceSource<T1,T2,T3,T4>::Connect (const CallbackBase & callback)
    72 {
    73   Callback<void,T1,T2,T3,T4> cb;
    74   cb.Assign (callback);
    75   m_callbackList.push_back (cb);
    76 }
    77 template<typename T1, typename T2, 
    78          typename T3, typename T4>
    79 void 
    80 EventTraceSource<T1,T2,T3,T4>::Disconnect (const CallbackBase & callback)
    81 {
    82   for (typename CallbackList::iterator i = m_callbackList.begin ();
    83        i != m_callbackList.end (); /* empty */)
    84     {
    85       if ((*i).IsEqual (callback))
    86 	{
    87 	  i = m_callbackList.erase (i);
    88 	}
    89       else
    90 	{
    91 	  i++;
    92 	}
    93     }
    94 }
    95 template<typename T1, typename T2, 
    96          typename T3, typename T4>
    97 void 
    98 EventTraceSource<T1,T2,T3,T4>::operator() (void) const
    99 {
   100   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   101        i != m_callbackList.end (); i++)
   102     {
   103       (*i) ();
   104     }
   105 }
   106 template<typename T1, typename T2, 
   107          typename T3, typename T4>
   108 void 
   109 EventTraceSource<T1,T2,T3,T4>::operator() (T1 a1) const
   110 {
   111   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   112        i != m_callbackList.end (); i++)
   113     {
   114       (*i) (a1);
   115     }
   116 }
   117 template<typename T1, typename T2, 
   118          typename T3, typename T4>
   119 void 
   120 EventTraceSource<T1,T2,T3,T4>::operator() (T1 a1, T2 a2) const
   121 {
   122   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   123        i != m_callbackList.end (); i++)
   124     {
   125       (*i) (a1, a2);
   126     }
   127 }
   128 template<typename T1, typename T2, 
   129          typename T3, typename T4>
   130 void 
   131 EventTraceSource<T1,T2,T3,T4>::operator() (T1 a1, T2 a2, T3 a3) const
   132 {
   133   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   134        i != m_callbackList.end (); i++)
   135     {
   136       (*i) (a1, a2, a3);
   137     }
   138 }
   139 template<typename T1, typename T2, 
   140          typename T3, typename T4>
   141 void 
   142 EventTraceSource<T1,T2,T3,T4>::operator() (T1 a1, T2 a2, T3 a3, T4 a4) const
   143 {
   144   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   145        i != m_callbackList.end (); i++)
   146     {
   147       (*i) (a1, a2, a3, a4);
   148     }
   149 }
   150 
   151 }//namespace ns3
   152 
   153 #endif /* CALLBACK_TRACE_H */