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
     1 #ifndef TRACE_SOURCE_ACCESSOR_H
     2 #define TRACE_SOURCE_ACCESSOR_H
     3 
     4 #include <stdint.h>
     5 #include "object-base.h"
     6 #include "callback.h"
     7 #include "ptr.h"
     8 
     9 namespace ns3 {
    10 
    11 class TraceSourceAccessor : public ObjectBase
    12 {
    13 public:
    14   TraceSourceAccessor ();
    15   virtual ~TraceSourceAccessor ();
    16   void Ref (void) const;
    17   void Unref (void) const;
    18 
    19   virtual bool Connect (ObjectBase *obj, const CallbackBase &cb) const = 0;
    20   virtual bool ConnectWithContext (ObjectBase *obj, std::string context, const CallbackBase &cb) const = 0;
    21   virtual bool Disconnect (ObjectBase *obj, const CallbackBase &cb) const = 0;
    22 private:
    23   mutable uint32_t m_count;
    24 };
    25 
    26 template <typename T>
    27 Ptr<const TraceSourceAccessor> MakeTraceSourceAccessor (T a);
    28 
    29 } // namespace ns3
    30 
    31 namespace ns3 {
    32 
    33 template <typename T, typename SOURCE>
    34 Ptr<const TraceSourceAccessor> 
    35 DoMakeTraceSourceAccessor (SOURCE T::*a)
    36 {
    37   struct Accessor : public TraceSourceAccessor
    38   {
    39     virtual bool Connect (ObjectBase *obj, const CallbackBase &cb) const {
    40       T *p = dynamic_cast<T*> (obj);
    41       if (p == 0)
    42 	{
    43 	  return false;
    44 	}
    45       (p->*m_source).Connect (cb);
    46       return true;
    47     }
    48     virtual bool ConnectWithContext (ObjectBase *obj, std::string context, const CallbackBase &cb) const {
    49       T *p = dynamic_cast<T*> (obj);
    50       if (p == 0)
    51 	{
    52 	  return false;
    53 	}
    54       (p->*m_source).ConnectWithContext (cb, context);
    55       return true;
    56     }
    57     virtual bool Disconnect (ObjectBase *obj, const CallbackBase &cb) const {
    58       T *p = dynamic_cast<T*> (obj);
    59       if (p == 0)
    60 	{
    61 	  return false;
    62 	}
    63       (p->*m_source).Disconnect (cb);
    64       return true;      
    65     }
    66     SOURCE T::*m_source;
    67   } *accessor = new Accessor ();
    68   accessor->m_source = a;
    69   return Ptr<const TraceSourceAccessor> (accessor, false);
    70 }
    71 
    72 template <typename T>
    73 Ptr<const TraceSourceAccessor> MakeTraceSourceAccessor (T a)
    74 {
    75   return DoMakeTraceSourceAccessor (a);
    76 }
    77 
    78 } // namespace ns3
    79 
    80 
    81 #endif /* TRACE_SOURCE_ACCESSOR_H */