src/core/traced-callback.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sun, 02 Mar 2008 21:00:37 +0100
changeset 2531 b451b5fc8b57
parent 2482 adbc284a5849
child 2569 d5cff2968984
permissions -rw-r--r--
implement context-based trace connection
     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 TRACED_CALLBACK_H
    23 #define TRACED_CALLBACK_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 TracedCallback 
    40 {
    41 public:
    42   TracedCallback ();
    43   void Connect (const CallbackBase & callback);
    44   void ConnectWithContext (const CallbackBase & callback, std::string path);
    45   void Disconnect (const CallbackBase & callback);
    46   void operator() (void) const;
    47   void operator() (T1 a1) const;
    48   void operator() (T1 a1, T2 a2) const;
    49   void operator() (T1 a1, T2 a2, T3 a3) const;
    50   void operator() (T1 a1, T2 a2, T3 a3, T4 a4) const;
    51 
    52 private:  
    53   typedef std::list<Callback<void,T1,T2,T3,T4> > CallbackList;
    54   CallbackList m_callbackList;
    55 };
    56 
    57 } // namespace ns3
    58 
    59 // implementation below.
    60 
    61 namespace ns3 {
    62 
    63 template<typename T1, typename T2, 
    64          typename T3, typename T4>
    65 TracedCallback<T1,T2,T3,T4>::TracedCallback ()
    66   : m_callbackList () 
    67 {}
    68 template<typename T1, typename T2, 
    69          typename T3, typename T4>
    70 void 
    71 TracedCallback<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 TracedCallback<T1,T2,T3,T4>::ConnectWithContext (const CallbackBase & callback, std::string path)
    81 {
    82   Callback<void,std::string,T1,T2,T3,T4> cb;
    83   cb.Assign (callback);
    84   Callback<void,T1,T2,T3,T4> realCb = cb.Bind (path);
    85   m_callbackList.push_back (realCb);
    86 }
    87 template<typename T1, typename T2, 
    88          typename T3, typename T4>
    89 void 
    90 TracedCallback<T1,T2,T3,T4>::Disconnect (const CallbackBase & callback)
    91 {
    92   for (typename CallbackList::iterator i = m_callbackList.begin ();
    93        i != m_callbackList.end (); /* empty */)
    94     {
    95       if ((*i).IsEqual (callback))
    96 	{
    97 	  i = m_callbackList.erase (i);
    98 	}
    99       else
   100 	{
   101 	  i++;
   102 	}
   103     }
   104 }
   105 template<typename T1, typename T2, 
   106          typename T3, typename T4>
   107 void 
   108 TracedCallback<T1,T2,T3,T4>::operator() (void) const
   109 {
   110   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   111        i != m_callbackList.end (); i++)
   112     {
   113       (*i) ();
   114     }
   115 }
   116 template<typename T1, typename T2, 
   117          typename T3, typename T4>
   118 void 
   119 TracedCallback<T1,T2,T3,T4>::operator() (T1 a1) const
   120 {
   121   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   122        i != m_callbackList.end (); i++)
   123     {
   124       (*i) (a1);
   125     }
   126 }
   127 template<typename T1, typename T2, 
   128          typename T3, typename T4>
   129 void 
   130 TracedCallback<T1,T2,T3,T4>::operator() (T1 a1, T2 a2) const
   131 {
   132   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   133        i != m_callbackList.end (); i++)
   134     {
   135       (*i) (a1, a2);
   136     }
   137 }
   138 template<typename T1, typename T2, 
   139          typename T3, typename T4>
   140 void 
   141 TracedCallback<T1,T2,T3,T4>::operator() (T1 a1, T2 a2, T3 a3) const
   142 {
   143   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   144        i != m_callbackList.end (); i++)
   145     {
   146       (*i) (a1, a2, a3);
   147     }
   148 }
   149 template<typename T1, typename T2, 
   150          typename T3, typename T4>
   151 void 
   152 TracedCallback<T1,T2,T3,T4>::operator() (T1 a1, T2 a2, T3 a3, T4 a4) const
   153 {
   154   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   155        i != m_callbackList.end (); i++)
   156     {
   157       (*i) (a1, a2, a3, a4);
   158     }
   159 }
   160 
   161 }//namespace ns3
   162 
   163 #endif /* TRACED_CALLBACK_H */