src/core/traced-callback.h
changeset 2482 adbc284a5849
parent 2466 8e4ee14cc574
child 2531 b451b5fc8b57
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/core/traced-callback.h	Tue Feb 26 19:51:33 2008 +0100
     1.3 @@ -0,0 +1,153 @@
     1.4 +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     1.5 +/*
     1.6 + * Copyright (c) 2005,2006,2007 INRIA
     1.7 + * All rights reserved.
     1.8 + *
     1.9 + * This program is free software; you can redistribute it and/or modify
    1.10 + * it under the terms of the GNU General Public License version 2 as
    1.11 + * published by the Free Software Foundation;
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program; if not, write to the Free Software
    1.20 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 + *
    1.22 + * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    1.23 + */
    1.24 +
    1.25 +#ifndef TRACED_CALLBACK_H
    1.26 +#define TRACED_CALLBACK_H
    1.27 +
    1.28 +#include <list>
    1.29 +#include "callback.h"
    1.30 +
    1.31 +namespace ns3 {
    1.32 +
    1.33 +/**
    1.34 + * \brief log arbitrary number of parameters to a matching ns3::Callback
    1.35 + * \ingroup tracing
    1.36 + *
    1.37 + * Whenever operator () is invoked on this class, the call and its arguments
    1.38 + * are forwarded to the internal matching ns3::Callback.
    1.39 + */
    1.40 +template<typename T1 = empty, typename T2 = empty, 
    1.41 +         typename T3 = empty, typename T4 = empty>
    1.42 +class TracedCallback 
    1.43 +{
    1.44 +public:
    1.45 +  TracedCallback ();
    1.46 +  void Connect (const CallbackBase & callback);
    1.47 +  void Disconnect (const CallbackBase & callback);
    1.48 +  void operator() (void) const;
    1.49 +  void operator() (T1 a1) const;
    1.50 +  void operator() (T1 a1, T2 a2) const;
    1.51 +  void operator() (T1 a1, T2 a2, T3 a3) const;
    1.52 +  void operator() (T1 a1, T2 a2, T3 a3, T4 a4) const;
    1.53 +
    1.54 +private:
    1.55 +  typedef std::list<Callback<void,T1,T2,T3,T4> > CallbackList;
    1.56 +  CallbackList m_callbackList;
    1.57 +};
    1.58 +
    1.59 +} // namespace ns3
    1.60 +
    1.61 +// implementation below.
    1.62 +
    1.63 +namespace ns3 {
    1.64 +
    1.65 +
    1.66 +template<typename T1, typename T2, 
    1.67 +         typename T3, typename T4>
    1.68 +TracedCallback<T1,T2,T3,T4>::TracedCallback ()
    1.69 +  : m_callbackList () 
    1.70 +{}
    1.71 +template<typename T1, typename T2, 
    1.72 +         typename T3, typename T4>
    1.73 +void 
    1.74 +TracedCallback<T1,T2,T3,T4>::Connect (const CallbackBase & callback)
    1.75 +{
    1.76 +  Callback<void,T1,T2,T3,T4> cb;
    1.77 +  cb.Assign (callback);
    1.78 +  m_callbackList.push_back (cb);
    1.79 +}
    1.80 +template<typename T1, typename T2, 
    1.81 +         typename T3, typename T4>
    1.82 +void 
    1.83 +TracedCallback<T1,T2,T3,T4>::Disconnect (const CallbackBase & callback)
    1.84 +{
    1.85 +  for (typename CallbackList::iterator i = m_callbackList.begin ();
    1.86 +       i != m_callbackList.end (); /* empty */)
    1.87 +    {
    1.88 +      if ((*i).IsEqual (callback))
    1.89 +	{
    1.90 +	  i = m_callbackList.erase (i);
    1.91 +	}
    1.92 +      else
    1.93 +	{
    1.94 +	  i++;
    1.95 +	}
    1.96 +    }
    1.97 +}
    1.98 +template<typename T1, typename T2, 
    1.99 +         typename T3, typename T4>
   1.100 +void 
   1.101 +TracedCallback<T1,T2,T3,T4>::operator() (void) const
   1.102 +{
   1.103 +  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   1.104 +       i != m_callbackList.end (); i++)
   1.105 +    {
   1.106 +      (*i) ();
   1.107 +    }
   1.108 +}
   1.109 +template<typename T1, typename T2, 
   1.110 +         typename T3, typename T4>
   1.111 +void 
   1.112 +TracedCallback<T1,T2,T3,T4>::operator() (T1 a1) const
   1.113 +{
   1.114 +  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   1.115 +       i != m_callbackList.end (); i++)
   1.116 +    {
   1.117 +      (*i) (a1);
   1.118 +    }
   1.119 +}
   1.120 +template<typename T1, typename T2, 
   1.121 +         typename T3, typename T4>
   1.122 +void 
   1.123 +TracedCallback<T1,T2,T3,T4>::operator() (T1 a1, T2 a2) const
   1.124 +{
   1.125 +  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   1.126 +       i != m_callbackList.end (); i++)
   1.127 +    {
   1.128 +      (*i) (a1, a2);
   1.129 +    }
   1.130 +}
   1.131 +template<typename T1, typename T2, 
   1.132 +         typename T3, typename T4>
   1.133 +void 
   1.134 +TracedCallback<T1,T2,T3,T4>::operator() (T1 a1, T2 a2, T3 a3) const
   1.135 +{
   1.136 +  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   1.137 +       i != m_callbackList.end (); i++)
   1.138 +    {
   1.139 +      (*i) (a1, a2, a3);
   1.140 +    }
   1.141 +}
   1.142 +template<typename T1, typename T2, 
   1.143 +         typename T3, typename T4>
   1.144 +void 
   1.145 +TracedCallback<T1,T2,T3,T4>::operator() (T1 a1, T2 a2, T3 a3, T4 a4) const
   1.146 +{
   1.147 +  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
   1.148 +       i != m_callbackList.end (); i++)
   1.149 +    {
   1.150 +      (*i) (a1, a2, a3, a4);
   1.151 +    }
   1.152 +}
   1.153 +
   1.154 +}//namespace ns3
   1.155 +
   1.156 +#endif /* TRACED_CALLBACK_H */