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
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
 *
tomh@345
     5
 * This program is free software; you can redistribute it and/or modify
tomh@345
     6
 * it under the terms of the GNU General Public License version 2 as
tomh@345
     7
 * published by the Free Software Foundation;
tomh@345
     8
 *
tomh@345
     9
 * This program is distributed in the hope that it will be useful,
tomh@345
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
tomh@345
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
tomh@345
    12
 * GNU General Public License for more details.
tomh@345
    13
 *
tomh@345
    14
 * You should have received a copy of the GNU General Public License
tomh@345
    15
 * along with this program; if not, write to the Free Software
tomh@345
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
tomh@345
    17
 *
tomh@345
    18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
tomh@345
    19
 */
tomh@345
    20
mathieu@2482
    21
#ifndef TRACED_CALLBACK_H
mathieu@2482
    22
#define TRACED_CALLBACK_H
tomh@345
    23
tomh@345
    24
#include <list>
mathieu@1335
    25
#include "callback.h"
tomh@345
    26
tomh@345
    27
namespace ns3 {
tomh@345
    28
tomh@345
    29
/**
mathieu@2779
    30
 * \brief forward calls to a chain of Callback
mathieu@1389
    31
 * \ingroup tracing
tomh@345
    32
 *
mathieu@2779
    33
 * An ns3::TracedCallback has almost exactly the same API as a normal ns3::Callback but
mathieu@2779
    34
 * instead of forwarding calls to a single function (as an ns3::Callback normally does),
mathieu@2779
    35
 * it forwards calls to a chain of ns3::Callback. TracedCallback::Connect adds a ns3::Callback
mathieu@2779
    36
 * at the end of the chain of callbacks. TracedCallback::Disconnect removes a ns3::Callback from
mathieu@2779
    37
 * the chain of callbacks.
tomh@345
    38
 */
tomh@345
    39
template<typename T1 = empty, typename T2 = empty, 
Providence@4072
    40
         typename T3 = empty, typename T4 = empty,
Providence@4072
    41
         typename T5 = empty, typename T6 = empty,
Providence@4072
    42
         typename T7 = empty, typename T8 = empty>
mathieu@2482
    43
class TracedCallback 
mathieu@2462
    44
{
tomh@345
    45
public:
mathieu@2482
    46
  TracedCallback ();
mathieu@2779
    47
  /**
mathieu@2779
    48
   * \param callback callback to add to chain of callbacks
mathieu@2779
    49
   *
mathieu@2779
    50
   * Append the input callback to the end of the internal list 
mathieu@2779
    51
   * of ns3::Callback.
mathieu@2779
    52
   */
mathieu@2594
    53
  void ConnectWithoutContext (const CallbackBase & callback);
mathieu@2779
    54
  /**
mathieu@2779
    55
   * \param callback callback to add to chain of callbacks
mathieu@2779
    56
   * \param path the path to send back to the user callback.
mathieu@2779
    57
   *
mathieu@2779
    58
   * Append the input callback to the end of the internal list 
mathieu@2779
    59
   * of ns3::Callback. This method also will make sure that the
mathieu@2779
    60
   * input path specified by the user will be give back to the
mathieu@2779
    61
   * user's callback as its first argument. 
mathieu@2779
    62
   */
mathieu@2594
    63
  void Connect (const CallbackBase & callback, std::string path);
mathieu@2779
    64
  /**
mathieu@2779
    65
   * \param callback callback to remove from the chain of callbacks.
mathieu@2779
    66
   *
mathieu@2779
    67
   * Remove the input callback from the internal list 
mathieu@2779
    68
   * of ns3::Callback. This method is really the symmetric
mathieu@2779
    69
   * of the TracedCallback::ConnectWithoutContext method.
mathieu@2779
    70
   */
mathieu@2594
    71
  void DisconnectWithoutContext (const CallbackBase & callback);
mathieu@2779
    72
  /**
mathieu@2779
    73
   * \param callback callback to remove from the chain of callbacks.
mathieu@2779
    74
   * \param path the path which is sent back to the user callback.
mathieu@2779
    75
   *
mathieu@2779
    76
   * Remove the input callback which has a matching path as first argument 
mathieu@2779
    77
   * from the internal list of ns3::Callback. This method is really the symmetric
mathieu@2779
    78
   * of the TracedCallback::Connect method.
mathieu@2779
    79
   */
mathieu@2594
    80
  void Disconnect (const CallbackBase & callback, std::string path);
mathieu@1343
    81
  void operator() (void) const;
mathieu@1343
    82
  void operator() (T1 a1) const;
mathieu@1343
    83
  void operator() (T1 a1, T2 a2) const;
mathieu@1343
    84
  void operator() (T1 a1, T2 a2, T3 a3) const;
mathieu@1343
    85
  void operator() (T1 a1, T2 a2, T3 a3, T4 a4) const;
Providence@4072
    86
  void operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) const;
Providence@4072
    87
  void operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6) const;
Providence@4072
    88
  void operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7) const;
Providence@4072
    89
  void operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7, T8 a8) const;
tomh@345
    90
mathieu@2531
    91
private:  
Providence@4072
    92
  typedef std::list<Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> > CallbackList;
tomh@345
    93
  CallbackList m_callbackList;
tomh@345
    94
};
tomh@345
    95
mathieu@2462
    96
} // namespace ns3
tomh@345
    97
tomh@345
    98
// implementation below.
tomh@345
    99
tomh@345
   100
namespace ns3 {
tomh@345
   101
mathieu@1419
   102
template<typename T1, typename T2, 
Providence@4072
   103
         typename T3, typename T4,
Providence@4072
   104
         typename T5, typename T6,
Providence@4072
   105
         typename T7, typename T8>
Providence@4072
   106
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::TracedCallback ()
tomh@345
   107
  : m_callbackList () 
tomh@345
   108
{}
tomh@345
   109
template<typename T1, typename T2, 
Providence@4072
   110
         typename T3, typename T4,
Providence@4072
   111
         typename T5, typename T6,
Providence@4072
   112
         typename T7, typename T8>
tomh@345
   113
void 
Providence@4072
   114
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::ConnectWithoutContext (const CallbackBase & callback)
tomh@345
   115
{
Providence@4072
   116
  Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> cb;
mathieu@529
   117
  cb.Assign (callback);
tomh@345
   118
  m_callbackList.push_back (cb);
tomh@345
   119
}
tomh@345
   120
template<typename T1, typename T2, 
Providence@4072
   121
         typename T3, typename T4,
Providence@4072
   122
         typename T5, typename T6,
Providence@4072
   123
         typename T7, typename T8>
tomh@345
   124
void 
Providence@4072
   125
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::Connect (const CallbackBase & callback, std::string path)
mathieu@2531
   126
{
Providence@4072
   127
  Callback<void,std::string,T1,T2,T3,T4,T5,T6,T7,T8> cb;
mathieu@2531
   128
  cb.Assign (callback);
Providence@4072
   129
  Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> realCb = cb.Bind (path);
mathieu@2531
   130
  m_callbackList.push_back (realCb);
mathieu@2531
   131
}
mathieu@2531
   132
template<typename T1, typename T2, 
Providence@4072
   133
         typename T3, typename T4,
Providence@4072
   134
         typename T5, typename T6,
Providence@4072
   135
         typename T7, typename T8>
mathieu@2531
   136
void 
Providence@4072
   137
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::DisconnectWithoutContext (const CallbackBase & callback)
tomh@345
   138
{
tomh@345
   139
  for (typename CallbackList::iterator i = m_callbackList.begin ();
tomh@345
   140
       i != m_callbackList.end (); /* empty */)
tomh@345
   141
    {
tomh@345
   142
      if ((*i).IsEqual (callback))
tomh@345
   143
	{
tomh@345
   144
	  i = m_callbackList.erase (i);
tomh@345
   145
	}
tomh@345
   146
      else
tomh@345
   147
	{
tomh@345
   148
	  i++;
tomh@345
   149
	}
tomh@345
   150
    }
tomh@345
   151
}
tomh@345
   152
template<typename T1, typename T2, 
Providence@4072
   153
         typename T3, typename T4,
Providence@4072
   154
         typename T5, typename T6,
Providence@4072
   155
         typename T7, typename T8>
tomh@345
   156
void 
Providence@4072
   157
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::Disconnect (const CallbackBase & callback, std::string path)
mathieu@2569
   158
{
Providence@4072
   159
  Callback<void,std::string,T1,T2,T3,T4,T5,T6,T7,T8> cb;
mathieu@2569
   160
  cb.Assign (callback);
Providence@4072
   161
  Callback<void,T1,T2,T3,T4,T5,T6,T7,T8> realCb = cb.Bind (path);
mathieu@2594
   162
  DisconnectWithoutContext (realCb);
mathieu@2569
   163
}
mathieu@2569
   164
template<typename T1, typename T2, 
Providence@4072
   165
         typename T3, typename T4,
Providence@4072
   166
         typename T5, typename T6,
Providence@4072
   167
         typename T7, typename T8>
mathieu@2569
   168
void 
Providence@4072
   169
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (void) const
tomh@345
   170
{
mathieu@1343
   171
  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
tomh@345
   172
       i != m_callbackList.end (); i++)
tomh@345
   173
    {
mathieu@2462
   174
      (*i) ();
tomh@345
   175
    }
tomh@345
   176
}
tomh@345
   177
template<typename T1, typename T2, 
Providence@4072
   178
         typename T3, typename T4,
Providence@4072
   179
         typename T5, typename T6,
Providence@4072
   180
         typename T7, typename T8>
tomh@345
   181
void 
Providence@4072
   182
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1) const
tomh@345
   183
{
mathieu@1343
   184
  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
tomh@345
   185
       i != m_callbackList.end (); i++)
tomh@345
   186
    {
mathieu@2462
   187
      (*i) (a1);
tomh@345
   188
    }
tomh@345
   189
}
tomh@345
   190
template<typename T1, typename T2, 
Providence@4072
   191
         typename T3, typename T4,
Providence@4072
   192
         typename T5, typename T6,
Providence@4072
   193
         typename T7, typename T8>
tomh@345
   194
void 
Providence@4072
   195
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2) const
tomh@345
   196
{
mathieu@1343
   197
  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
tomh@345
   198
       i != m_callbackList.end (); i++)
tomh@345
   199
    {
mathieu@2462
   200
      (*i) (a1, a2);
tomh@345
   201
    }
tomh@345
   202
}
tomh@345
   203
template<typename T1, typename T2, 
Providence@4072
   204
         typename T3, typename T4,
Providence@4072
   205
         typename T5, typename T6,
Providence@4072
   206
         typename T7, typename T8>
tomh@345
   207
void 
Providence@4072
   208
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3) const
tomh@345
   209
{
mathieu@1343
   210
  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
tomh@345
   211
       i != m_callbackList.end (); i++)
tomh@345
   212
    {
mathieu@2462
   213
      (*i) (a1, a2, a3);
tomh@345
   214
    }
tomh@345
   215
}
tomh@345
   216
template<typename T1, typename T2, 
Providence@4072
   217
         typename T3, typename T4,
Providence@4072
   218
         typename T5, typename T6,
Providence@4072
   219
         typename T7, typename T8>
tomh@345
   220
void 
Providence@4072
   221
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3, T4 a4) const
tomh@345
   222
{
mathieu@1343
   223
  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
tomh@345
   224
       i != m_callbackList.end (); i++)
tomh@345
   225
    {
mathieu@2462
   226
      (*i) (a1, a2, a3, a4);
tomh@345
   227
    }
tomh@345
   228
}
Providence@4072
   229
template<typename T1, typename T2, 
Providence@4072
   230
         typename T3, typename T4,
Providence@4072
   231
         typename T5, typename T6,
Providence@4072
   232
         typename T7, typename T8>
Providence@4072
   233
void 
Providence@4072
   234
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) const
Providence@4072
   235
{
Providence@4072
   236
  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
Providence@4072
   237
       i != m_callbackList.end (); i++)
Providence@4072
   238
    {
Providence@4072
   239
      (*i) (a1, a2, a3, a4, a5);
Providence@4072
   240
    }
Providence@4072
   241
}
Providence@4072
   242
template<typename T1, typename T2, 
Providence@4072
   243
         typename T3, typename T4,
Providence@4072
   244
         typename T5, typename T6,
Providence@4072
   245
         typename T7, typename T8>
Providence@4072
   246
void 
Providence@4072
   247
TracedCallback<T1,T2,T3,T4,T5,T6,T7,T8>::operator() (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6) const
Providence@4072
   248
{
Providence@4072
   249
  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
Providence@4072
   250
       i != m_callbackList.end (); i++)
Providence@4072
   251
    {
Providence@4072
   252
      (*i) (a1, a2, a3, a4, a5, a6);
Providence@4072
   253
    }
Providence@4072
   254
}
Providence@4072
   255
template<typename T1, typename T2, 
Providence@4072
   256
         typename T3, typename T4,
Providence@4072
   257
         typename T5, typename T6,
Providence@4072
   258
         typename T7, typename T8>
Providence@4072
   259
void 
Providence@4072
   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
Providence@4072
   261
{
Providence@4072
   262
  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
Providence@4072
   263
       i != m_callbackList.end (); i++)
Providence@4072
   264
    {
Providence@4072
   265
      (*i) (a1, a2, a3, a4, a5, a6, a7);
Providence@4072
   266
    }
Providence@4072
   267
}
Providence@4072
   268
template<typename T1, typename T2, 
Providence@4072
   269
         typename T3, typename T4,
Providence@4072
   270
         typename T5, typename T6,
Providence@4072
   271
         typename T7, typename T8>
Providence@4072
   272
void 
Providence@4072
   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
Providence@4072
   274
{
Providence@4072
   275
  for (typename CallbackList::const_iterator i = m_callbackList.begin ();
Providence@4072
   276
       i != m_callbackList.end (); i++)
Providence@4072
   277
    {
Providence@4072
   278
      (*i) (a1, a2, a3, a4, a5, a6, a7, a8);
Providence@4072
   279
    }
Providence@4072
   280
}
tomh@345
   281
tomh@345
   282
}//namespace ns3
tomh@345
   283
mathieu@2482
   284
#endif /* TRACED_CALLBACK_H */