src/core/trace-source-accessor.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 3182 61fe7fe81ebd
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
mathieu@2587
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
mathieu@2587
     2
/*
mathieu@2587
     3
 * Copyright (c) 2008 INRIA
mathieu@2587
     4
 *
mathieu@2587
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@2587
     6
 * it under the terms of the GNU General Public License version 2 as
mathieu@2587
     7
 * published by the Free Software Foundation;
mathieu@2587
     8
 *
mathieu@2587
     9
 * This program is distributed in the hope that it will be useful,
mathieu@2587
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@2587
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@2587
    12
 * GNU General Public License for more details.
mathieu@2587
    13
 *
mathieu@2587
    14
 * You should have received a copy of the GNU General Public License
mathieu@2587
    15
 * along with this program; if not, write to the Free Software
mathieu@2587
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@2587
    17
 *
mathieu@2587
    18
 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@2587
    19
 */
mathieu@2463
    20
#ifndef TRACE_SOURCE_ACCESSOR_H
mathieu@2463
    21
#define TRACE_SOURCE_ACCESSOR_H
mathieu@2463
    22
mathieu@2463
    23
#include <stdint.h>
mathieu@2463
    24
#include "callback.h"
mathieu@2463
    25
#include "ptr.h"
mathieu@2463
    26
mathieu@2463
    27
namespace ns3 {
mathieu@2463
    28
mathieu@2634
    29
class ObjectBase;
mathieu@2634
    30
mathieu@2587
    31
/**
tomh@3182
    32
 * \ingroup tracing
tomh@3182
    33
 *
mathieu@2587
    34
 * \brief control access to objects' trace sources
mathieu@2587
    35
 *
mathieu@2648
    36
 * This class abstracts the kind of trace source to which we want to connect
mathieu@2648
    37
 * and provides services to Connect and Disconnect a sink to a trace source.
mathieu@2587
    38
 */
mathieu@2634
    39
class TraceSourceAccessor
mathieu@2463
    40
{
mathieu@2463
    41
public:
mathieu@2463
    42
  TraceSourceAccessor ();
mathieu@2463
    43
  virtual ~TraceSourceAccessor ();
mathieu@2463
    44
  void Ref (void) const;
mathieu@2463
    45
  void Unref (void) const;
mathieu@2463
    46
mathieu@2587
    47
  /**
mathieu@2587
    48
   * \param obj the object instance which contains the target trace source.
mathieu@2587
    49
   * \param cb the callback to connect to the target trace source.
mathieu@2587
    50
   */
mathieu@2594
    51
  virtual bool ConnectWithoutContext (ObjectBase *obj, const CallbackBase &cb) const = 0;
mathieu@2587
    52
  /**
mathieu@2587
    53
   * \param obj the object instance which contains the target trace source.
mathieu@2587
    54
   * \param context the context to bind to the user callback.
mathieu@2587
    55
   * \param cb the callback to connect to the target trace source.
mathieu@2587
    56
   */
mathieu@2594
    57
  virtual bool Connect (ObjectBase *obj, std::string context, const CallbackBase &cb) const = 0;
mathieu@2587
    58
  /**
mathieu@2587
    59
   * \param obj the object instance which contains the target trace source.
mathieu@2587
    60
   * \param cb the callback to disconnect from the target trace source.
mathieu@2587
    61
   */
mathieu@2594
    62
  virtual bool DisconnectWithoutContext (ObjectBase *obj, const CallbackBase &cb) const = 0;
mathieu@2587
    63
  /**
mathieu@2587
    64
   * \param obj the object instance which contains the target trace source.
mathieu@2587
    65
   * \param context the context which was bound to the user callback.
mathieu@2587
    66
   * \param cb the callback to disconnect from the target trace source.
mathieu@2587
    67
   */
mathieu@2594
    68
  virtual bool Disconnect (ObjectBase *obj, std::string context, const CallbackBase &cb) const = 0;
mathieu@2463
    69
private:
mathieu@2463
    70
  mutable uint32_t m_count;
mathieu@2463
    71
};
mathieu@2463
    72
mathieu@2587
    73
/**
mathieu@2587
    74
 * \param a the trace source
mathieu@2587
    75
 *
mathieu@2587
    76
 * Create a TraceSourceAccessor which will control access to the underlying
mathieu@3190
    77
 * trace source. This helper template method assumes that the underlying
mathieu@3190
    78
 * type implements a statically-polymorphic set of Connect and Disconnect
mathieu@3190
    79
 * methods and creates a dynamic-polymorphic class to wrap the underlying
mathieu@3190
    80
 * static-polymorphic class.
mathieu@2587
    81
 */
mathieu@2463
    82
template <typename T>
mathieu@2463
    83
Ptr<const TraceSourceAccessor> MakeTraceSourceAccessor (T a);
mathieu@2463
    84
mathieu@2463
    85
} // namespace ns3
mathieu@2463
    86
mathieu@2463
    87
namespace ns3 {
mathieu@2463
    88
mathieu@2463
    89
template <typename T, typename SOURCE>
mathieu@2463
    90
Ptr<const TraceSourceAccessor> 
mathieu@2463
    91
DoMakeTraceSourceAccessor (SOURCE T::*a)
mathieu@2463
    92
{
mathieu@2463
    93
  struct Accessor : public TraceSourceAccessor
mathieu@2463
    94
  {
mathieu@2594
    95
    virtual bool ConnectWithoutContext (ObjectBase *obj, const CallbackBase &cb) const {
mathieu@2463
    96
      T *p = dynamic_cast<T*> (obj);
mathieu@2463
    97
      if (p == 0)
mathieu@2463
    98
	{
mathieu@2463
    99
	  return false;
mathieu@2463
   100
	}
mathieu@2594
   101
      (p->*m_source).ConnectWithoutContext (cb);
mathieu@2463
   102
      return true;
mathieu@2463
   103
    }
mathieu@2594
   104
    virtual bool Connect (ObjectBase *obj, std::string context, const CallbackBase &cb) const {
mathieu@2531
   105
      T *p = dynamic_cast<T*> (obj);
mathieu@2531
   106
      if (p == 0)
mathieu@2531
   107
	{
mathieu@2531
   108
	  return false;
mathieu@2531
   109
	}
mathieu@2594
   110
      (p->*m_source).Connect (cb, context);
mathieu@2531
   111
      return true;
mathieu@2531
   112
    }
mathieu@2594
   113
    virtual bool DisconnectWithoutContext (ObjectBase *obj, const CallbackBase &cb) const {
mathieu@2463
   114
      T *p = dynamic_cast<T*> (obj);
mathieu@2463
   115
      if (p == 0)
mathieu@2463
   116
	{
mathieu@2463
   117
	  return false;
mathieu@2463
   118
	}
mathieu@2594
   119
      (p->*m_source).DisconnectWithoutContext (cb);
mathieu@2463
   120
      return true;      
mathieu@2463
   121
    }
mathieu@2594
   122
    virtual bool Disconnect (ObjectBase *obj, std::string context, const CallbackBase &cb) const {
mathieu@2569
   123
      T *p = dynamic_cast<T*> (obj);
mathieu@2569
   124
      if (p == 0)
mathieu@2569
   125
	{
mathieu@2569
   126
	  return false;
mathieu@2569
   127
	}
mathieu@2594
   128
      (p->*m_source).Disconnect (cb, context);
mathieu@2569
   129
      return true;      
mathieu@2569
   130
    }
mathieu@2463
   131
    SOURCE T::*m_source;
mathieu@2463
   132
  } *accessor = new Accessor ();
mathieu@2463
   133
  accessor->m_source = a;
mathieu@2463
   134
  return Ptr<const TraceSourceAccessor> (accessor, false);
mathieu@2463
   135
}
mathieu@2463
   136
mathieu@2463
   137
template <typename T>
mathieu@2463
   138
Ptr<const TraceSourceAccessor> MakeTraceSourceAccessor (T a)
mathieu@2463
   139
{
mathieu@2463
   140
  return DoMakeTraceSourceAccessor (a);
mathieu@2463
   141
}
mathieu@2463
   142
mathieu@2463
   143
} // namespace ns3
mathieu@2463
   144
mathieu@2463
   145
mathieu@2463
   146
#endif /* TRACE_SOURCE_ACCESSOR_H */