src/core/traced-callback.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 2834 1aab57845b07
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
     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 
    27 namespace ns3 {
    28 
    29 /**
    30  * \brief forward calls to a chain of Callback
    31  * \ingroup tracing
    32  *
    33  * An ns3::TracedCallback has almost exactly the same API as a normal ns3::Callback but
    34  * instead of forwarding calls to a single function (as an ns3::Callback normally does),
    35  * it forwards calls to a chain of ns3::Callback. TracedCallback::Connect adds a ns3::Callback
    36  * at the end of the chain of callbacks. TracedCallback::Disconnect removes a ns3::Callback from
    37  * the chain of callbacks.
    38  */
    39 template<typename T1 = empty, typename T2 = empty, 
    40          typename T3 = empty, typename T4 = empty,
    41          typename T5 = empty, typename T6 = empty,
    42          typename T7 = empty, typename T8 = empty>
    43 class TracedCallback 
    44 {
    45 public:
    46   TracedCallback ();
    47   /**
    48    * \param callback callback to add to chain of callbacks
    49    *
    50    * Append the input callback to the end of the internal list 
    51    * of ns3::Callback.
    52    */
    53   void ConnectWithoutContext (const CallbackBase & callback);
    54   /**
    55    * \param callback callback to add to chain of callbacks
    56    * \param path the path to send back to the user callback.
    57    *
    58    * Append the input callback to the end of the internal list 
    59    * of ns3::Callback. This method also will make sure that the
    60    * input path specified by the user will be give back to the
    61    * user's callback as its first argument. 
    62    */
    63   void Connect (const CallbackBase & callback, std::string path);
    64   /**
    65    * \param callback callback to remove from the chain of callbacks.
    66    *
    67    * Remove the input callback from the internal list 
    68    * of ns3::Callback. This method is really the symmetric
    69    * of the TracedCallback::ConnectWithoutContext method.
    70    */
    71   void DisconnectWithoutContext (const CallbackBase & callback);
    72   /**
    73    * \param callback callback to remove from the chain of callbacks.
    74    * \param path the path which is sent back to the user callback.
    75    *
    76    * Remove the input callback which has a matching path as first argument 
    77    * from the internal list of ns3::Callback. This method is really the symmetric
    78    * of the TracedCallback::Connect method.
    79    */
    80   void Disconnect (const CallbackBase & callback, std::string path);
    81   void operator() (void) const;
    82   void operator() (T1 a1) const;
    83   void operator() (T1 a1, T2 a2) const;
    84   void operator() (T1 a1, T2 a2, T3 a3) const;
    85   void operator() (T1 a1, T2 a2, T3 a3, T4 a4) const;
    86   void operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) const;
    87   void operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6) const;
    88   void operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7) const;
    89   void operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7, T8 a8) const;
    90 
    91 private:  
    92   typedef std::list<Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> > CallbackList;
    93   CallbackList m_callbackList;
    94 };
    95 
    96 } // namespace ns3
    97 
    98 // implementation below.
    99 
   100 namespace ns3 {
   101 
   102 template<typename T1, typename T2, 
   103          typename T3, typename T4,
   104          typename T5, typename T6,
   105          typename T7, typename T8>
   106 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::TracedCallback ()
   107   : m_callbackList () 
   108 {}
   109 template<typename T1, typename T2, 
   110          typename T3, typename T4,
   111          typename T5, typename T6,
   112          typename T7, typename T8>
   113 void 
   114 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::ConnectWithoutContext (const CallbackBase & callback)
   115 {
   116   Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> cb;
   117   cb.Assign (callback);
   118   m_callbackList.push_back (cb);
   119 }
   120 template<typename T1, typename T2, 
   121          typename T3, typename T4,
   122          typename T5, typename T6,
   123          typename T7, typename T8>
   124 void 
   125 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::Connect (const CallbackBase & callback, std::string path)
   126 {
   127   Callback<void,std::string,T1,T2,T3,T4,T5,T6,T7,T8> cb;
   128   cb.Assign (callback);
   129   Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> realCb = cb.Bind (path);
   130   m_callbackList.push_back (realCb);
   131 }
   132 template<typename T1, typename T2, 
   133          typename T3, typename T4,
   134          typename T5, typename T6,
   135          typename T7, typename T8>
   136 void 
   137 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::DisconnectWithoutContext (const CallbackBase & callback)
   138 {
   139   for (typename CallbackList::iterator i = m_callbackList.begin ();
   140        i != m_callbackList.end (); /* empty */)
   141     {
   142       if ((*i).IsEqual (callback))
   143 	{
   144 	  i = m_callbackList.erase (i);
   145 	}
   146       else
   147 	{
   148 	  i++;
   149 	}
   150     }
   151 }
   152 template<typename T1, typename T2, 
   153          typename T3, typename T4,
   154          typename T5, typename T6,
   155          typename T7, typename T8>
   156 void 
   157 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::Disconnect (const CallbackBase & callback, std::string path)
   158 {
   159   Callback<void,std::string,T1,T2,T3,T4,T5,T6,T7,T8> cb;
   160   cb.Assign (callback);
   161   Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> realCb = cb.Bind (path);
   162   DisconnectWithoutContext (realCb);
   163 }
   164 template<typename T1, typename T2, 
   165          typename T3, typename T4,
   166          typename T5, typename T6,
   167          typename T7, typename T8>
   168 void 
   169 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (void) const
   170 {
   171   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   172        i != m_callbackList.end (); i++)
   173     {
   174       (*i) ();
   175     }
   176 }
   177 template<typename T1, typename T2, 
   178          typename T3, typename T4,
   179          typename T5, typename T6,
   180          typename T7, typename T8>
   181 void 
   182 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1) const
   183 {
   184   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   185        i != m_callbackList.end (); i++)
   186     {
   187       (*i) (a1);
   188     }
   189 }
   190 template<typename T1, typename T2, 
   191          typename T3, typename T4,
   192          typename T5, typename T6,
   193          typename T7, typename T8>
   194 void 
   195 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2) const
   196 {
   197   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   198        i != m_callbackList.end (); i++)
   199     {
   200       (*i) (a1, a2);
   201     }
   202 }
   203 template<typename T1, typename T2, 
   204          typename T3, typename T4,
   205          typename T5, typename T6,
   206          typename T7, typename T8>
   207 void 
   208 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3) const
   209 {
   210   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   211        i != m_callbackList.end (); i++)
   212     {
   213       (*i) (a1, a2, a3);
   214     }
   215 }
   216 template<typename T1, typename T2, 
   217          typename T3, typename T4,
   218          typename T5, typename T6,
   219          typename T7, typename T8>
   220 void 
   221 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3, T4 a4) const
   222 {
   223   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   224        i != m_callbackList.end (); i++)
   225     {
   226       (*i) (a1, a2, a3, a4);
   227     }
   228 }
   229 template<typename T1, typename T2, 
   230          typename T3, typename T4,
   231          typename T5, typename T6,
   232          typename T7, typename T8>
   233 void 
   234 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) const
   235 {
   236   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   237        i != m_callbackList.end (); i++)
   238     {
   239       (*i) (a1, a2, a3, a4, a5);
   240     }
   241 }
   242 template<typename T1, typename T2, 
   243          typename T3, typename T4,
   244          typename T5, typename T6,
   245          typename T7, typename T8>
   246 void 
   247 TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6) const
   248 {
   249   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   250        i != m_callbackList.end (); i++)
   251     {
   252       (*i) (a1, a2, a3, a4, a5, a6);
   253     }
   254 }
   255 template<typename T1, typename T2, 
   256          typename T3, typename T4,
   257          typename T5, typename T6,
   258          typename T7, typename T8>
   259 void 
   260 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
   261 {
   262   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   263        i != m_callbackList.end (); i++)
   264     {
   265       (*i) (a1, a2, a3, a4, a5, a6, a7);
   266     }
   267 }
   268 template<typename T1, typename T2, 
   269          typename T3, typename T4,
   270          typename T5, typename T6,
   271          typename T7, typename T8>
   272 void 
   273 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
   274 {
   275   for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   276        i != m_callbackList.end (); i++)
   277     {
   278       (*i) (a1, a2, a3, a4, a5, a6, a7, a8);
   279     }
   280 }
   281 
   282 }//namespace ns3
   283 
   284 #endif /* TRACED_CALLBACK_H */