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