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