src/core/trace-source-accessor.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sun, 02 Mar 2008 21:00:37 +0100
changeset 2531 b451b5fc8b57
parent 2463 c77e43117673
child 2569 d5cff2968984
permissions -rw-r--r--
implement context-based trace connection
mathieu@2463
     1
#ifndef TRACE_SOURCE_ACCESSOR_H
mathieu@2463
     2
#define TRACE_SOURCE_ACCESSOR_H
mathieu@2463
     3
mathieu@2463
     4
#include <stdint.h>
mathieu@2463
     5
#include "object-base.h"
mathieu@2463
     6
#include "callback.h"
mathieu@2463
     7
#include "ptr.h"
mathieu@2463
     8
mathieu@2463
     9
namespace ns3 {
mathieu@2463
    10
mathieu@2463
    11
class TraceSourceAccessor : public ObjectBase
mathieu@2463
    12
{
mathieu@2463
    13
public:
mathieu@2463
    14
  TraceSourceAccessor ();
mathieu@2463
    15
  virtual ~TraceSourceAccessor ();
mathieu@2463
    16
  void Ref (void) const;
mathieu@2463
    17
  void Unref (void) const;
mathieu@2463
    18
mathieu@2463
    19
  virtual bool Connect (ObjectBase *obj, const CallbackBase &cb) const = 0;
mathieu@2531
    20
  virtual bool ConnectWithContext (ObjectBase *obj, std::string context, const CallbackBase &cb) const = 0;
mathieu@2463
    21
  virtual bool Disconnect (ObjectBase *obj, const CallbackBase &cb) const = 0;
mathieu@2463
    22
private:
mathieu@2463
    23
  mutable uint32_t m_count;
mathieu@2463
    24
};
mathieu@2463
    25
mathieu@2463
    26
template <typename T>
mathieu@2463
    27
Ptr<const TraceSourceAccessor> MakeTraceSourceAccessor (T a);
mathieu@2463
    28
mathieu@2463
    29
} // namespace ns3
mathieu@2463
    30
mathieu@2463
    31
namespace ns3 {
mathieu@2463
    32
mathieu@2463
    33
template <typename T, typename SOURCE>
mathieu@2463
    34
Ptr<const TraceSourceAccessor> 
mathieu@2463
    35
DoMakeTraceSourceAccessor (SOURCE T::*a)
mathieu@2463
    36
{
mathieu@2463
    37
  struct Accessor : public TraceSourceAccessor
mathieu@2463
    38
  {
mathieu@2463
    39
    virtual bool Connect (ObjectBase *obj, const CallbackBase &cb) const {
mathieu@2463
    40
      T *p = dynamic_cast<T*> (obj);
mathieu@2463
    41
      if (p == 0)
mathieu@2463
    42
	{
mathieu@2463
    43
	  return false;
mathieu@2463
    44
	}
mathieu@2463
    45
      (p->*m_source).Connect (cb);
mathieu@2463
    46
      return true;
mathieu@2463
    47
    }
mathieu@2531
    48
    virtual bool ConnectWithContext (ObjectBase *obj, std::string context, const CallbackBase &cb) const {
mathieu@2531
    49
      T *p = dynamic_cast<T*> (obj);
mathieu@2531
    50
      if (p == 0)
mathieu@2531
    51
	{
mathieu@2531
    52
	  return false;
mathieu@2531
    53
	}
mathieu@2531
    54
      (p->*m_source).ConnectWithContext (cb, context);
mathieu@2531
    55
      return true;
mathieu@2531
    56
    }
mathieu@2463
    57
    virtual bool Disconnect (ObjectBase *obj, const CallbackBase &cb) const {
mathieu@2463
    58
      T *p = dynamic_cast<T*> (obj);
mathieu@2463
    59
      if (p == 0)
mathieu@2463
    60
	{
mathieu@2463
    61
	  return false;
mathieu@2463
    62
	}
mathieu@2463
    63
      (p->*m_source).Disconnect (cb);
mathieu@2463
    64
      return true;      
mathieu@2463
    65
    }
mathieu@2463
    66
    SOURCE T::*m_source;
mathieu@2463
    67
  } *accessor = new Accessor ();
mathieu@2463
    68
  accessor->m_source = a;
mathieu@2463
    69
  return Ptr<const TraceSourceAccessor> (accessor, false);
mathieu@2463
    70
}
mathieu@2463
    71
mathieu@2463
    72
template <typename T>
mathieu@2463
    73
Ptr<const TraceSourceAccessor> MakeTraceSourceAccessor (T a)
mathieu@2463
    74
{
mathieu@2463
    75
  return DoMakeTraceSourceAccessor (a);
mathieu@2463
    76
}
mathieu@2463
    77
mathieu@2463
    78
} // namespace ns3
mathieu@2463
    79
mathieu@2463
    80
mathieu@2463
    81
#endif /* TRACE_SOURCE_ACCESSOR_H */