src/core/trace-source-accessor.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu Nov 12 13:01:01 2009 +0100 (2009-11-12)
changeset 5505 c0ac392289c3
parent 3190 51fe9001a679
permissions -rw-r--r--
replace RefCountBase with SimpleRefCount<> to avoid duplicate refcounting implementations.
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@5505
    26
#include "simple-ref-count.h"
mathieu@2463
    27
mathieu@2463
    28
namespace ns3 {
mathieu@2463
    29
mathieu@2634
    30
class ObjectBase;
mathieu@2634
    31
mathieu@2587
    32
/**
tomh@3182
    33
 * \ingroup tracing
tomh@3182
    34
 *
mathieu@2587
    35
 * \brief control access to objects' trace sources
mathieu@2587
    36
 *
mathieu@2648
    37
 * This class abstracts the kind of trace source to which we want to connect
mathieu@2648
    38
 * and provides services to Connect and Disconnect a sink to a trace source.
mathieu@2587
    39
 */
mathieu@5505
    40
class TraceSourceAccessor : public SimpleRefCount<TraceSourceAccessor>
mathieu@2463
    41
{
mathieu@2463
    42
public:
mathieu@2463
    43
  TraceSourceAccessor ();
mathieu@2463
    44
  virtual ~TraceSourceAccessor ();
mathieu@2463
    45
mathieu@2587
    46
  /**
mathieu@2587
    47
   * \param obj the object instance which contains the target trace source.
mathieu@2587
    48
   * \param cb the callback to connect to the target trace source.
mathieu@2587
    49
   */
mathieu@2594
    50
  virtual bool ConnectWithoutContext (ObjectBase *obj, const CallbackBase &cb) const = 0;
mathieu@2587
    51
  /**
mathieu@2587
    52
   * \param obj the object instance which contains the target trace source.
mathieu@2587
    53
   * \param context the context to bind to the user callback.
mathieu@2587
    54
   * \param cb the callback to connect to the target trace source.
mathieu@2587
    55
   */
mathieu@2594
    56
  virtual bool Connect (ObjectBase *obj, std::string context, const CallbackBase &cb) const = 0;
mathieu@2587
    57
  /**
mathieu@2587
    58
   * \param obj the object instance which contains the target trace source.
mathieu@2587
    59
   * \param cb the callback to disconnect from the target trace source.
mathieu@2587
    60
   */
mathieu@2594
    61
  virtual bool DisconnectWithoutContext (ObjectBase *obj, const CallbackBase &cb) const = 0;
mathieu@2587
    62
  /**
mathieu@2587
    63
   * \param obj the object instance which contains the target trace source.
mathieu@2587
    64
   * \param context the context which was bound to the user callback.
mathieu@2587
    65
   * \param cb the callback to disconnect from the target trace source.
mathieu@2587
    66
   */
mathieu@2594
    67
  virtual bool Disconnect (ObjectBase *obj, std::string context, const CallbackBase &cb) const = 0;
mathieu@2463
    68
};
mathieu@2463
    69
mathieu@2587
    70
/**
mathieu@2587
    71
 * \param a the trace source
mathieu@2587
    72
 *
mathieu@2587
    73
 * Create a TraceSourceAccessor which will control access to the underlying
mathieu@3190
    74
 * trace source. This helper template method assumes that the underlying
mathieu@3190
    75
 * type implements a statically-polymorphic set of Connect and Disconnect
mathieu@3190
    76
 * methods and creates a dynamic-polymorphic class to wrap the underlying
mathieu@3190
    77
 * static-polymorphic class.
mathieu@2587
    78
 */
mathieu@2463
    79
template <typename T>
mathieu@2463
    80
Ptr<const TraceSourceAccessor> MakeTraceSourceAccessor (T a);
mathieu@2463
    81
mathieu@2463
    82
} // namespace ns3
mathieu@2463
    83
mathieu@2463
    84
namespace ns3 {
mathieu@2463
    85
mathieu@2463
    86
template <typename T, typename SOURCE>
mathieu@2463
    87
Ptr<const TraceSourceAccessor> 
mathieu@2463
    88
DoMakeTraceSourceAccessor (SOURCE T::*a)
mathieu@2463
    89
{
mathieu@2463
    90
  struct Accessor : public TraceSourceAccessor
mathieu@2463
    91
  {
mathieu@2594
    92
    virtual bool ConnectWithoutContext (ObjectBase *obj, const CallbackBase &cb) const {
mathieu@2463
    93
      T *p = dynamic_cast<T*> (obj);
mathieu@2463
    94
      if (p == 0)
mathieu@2463
    95
	{
mathieu@2463
    96
	  return false;
mathieu@2463
    97
	}
mathieu@2594
    98
      (p->*m_source).ConnectWithoutContext (cb);
mathieu@2463
    99
      return true;
mathieu@2463
   100
    }
mathieu@2594
   101
    virtual bool Connect (ObjectBase *obj, std::string context, const CallbackBase &cb) const {
mathieu@2531
   102
      T *p = dynamic_cast<T*> (obj);
mathieu@2531
   103
      if (p == 0)
mathieu@2531
   104
	{
mathieu@2531
   105
	  return false;
mathieu@2531
   106
	}
mathieu@2594
   107
      (p->*m_source).Connect (cb, context);
mathieu@2531
   108
      return true;
mathieu@2531
   109
    }
mathieu@2594
   110
    virtual bool DisconnectWithoutContext (ObjectBase *obj, const CallbackBase &cb) const {
mathieu@2463
   111
      T *p = dynamic_cast<T*> (obj);
mathieu@2463
   112
      if (p == 0)
mathieu@2463
   113
	{
mathieu@2463
   114
	  return false;
mathieu@2463
   115
	}
mathieu@2594
   116
      (p->*m_source).DisconnectWithoutContext (cb);
mathieu@2463
   117
      return true;      
mathieu@2463
   118
    }
mathieu@2594
   119
    virtual bool Disconnect (ObjectBase *obj, std::string context, const CallbackBase &cb) const {
mathieu@2569
   120
      T *p = dynamic_cast<T*> (obj);
mathieu@2569
   121
      if (p == 0)
mathieu@2569
   122
	{
mathieu@2569
   123
	  return false;
mathieu@2569
   124
	}
mathieu@2594
   125
      (p->*m_source).Disconnect (cb, context);
mathieu@2569
   126
      return true;      
mathieu@2569
   127
    }
mathieu@2463
   128
    SOURCE T::*m_source;
mathieu@2463
   129
  } *accessor = new Accessor ();
mathieu@2463
   130
  accessor->m_source = a;
mathieu@2463
   131
  return Ptr<const TraceSourceAccessor> (accessor, false);
mathieu@2463
   132
}
mathieu@2463
   133
mathieu@2463
   134
template <typename T>
mathieu@2463
   135
Ptr<const TraceSourceAccessor> MakeTraceSourceAccessor (T a)
mathieu@2463
   136
{
mathieu@2463
   137
  return DoMakeTraceSourceAccessor (a);
mathieu@2463
   138
}
mathieu@2463
   139
mathieu@2463
   140
} // namespace ns3
mathieu@2463
   141
mathieu@2463
   142
mathieu@2463
   143
#endif /* TRACE_SOURCE_ACCESSOR_H */