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