src/core/uv-trace-source.h
changeset 1334 e8e07f44359f
parent 393 18ed386bee75
child 1335 d0e45d84f9c6
equal deleted inserted replaced
1333:c0d66de933e9 1334:e8e07f44359f
       
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
       
     2 /*
       
     3  * Copyright (c) 2006 INRIA
       
     4  * All rights reserved.
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License version 2 as
       
     8  * published by the Free Software Foundation;
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    18  *
       
    19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
       
    20  */
       
    21 
       
    22 #ifndef UV_TRACE_SOURCE_H
       
    23 #define UV_TRACE_SOURCE_H
       
    24 
       
    25 #include "callback-trace-source.h"
       
    26 #include <stdint.h>
       
    27 
       
    28 namespace ns3 {
       
    29 
       
    30 class UVTraceSourceBase {
       
    31 public:
       
    32   typedef CallbackTraceSource<uint64_t, uint64_t> ChangeNotifyCallback;
       
    33 
       
    34   UVTraceSourceBase ()
       
    35       : m_callback () {}
       
    36   /* We don't want to copy the base callback. Only setCallback on
       
    37    * a specific instance will do something to it. */
       
    38   UVTraceSourceBase (UVTraceSourceBase const &o) 
       
    39       : m_callback () {}
       
    40   UVTraceSourceBase &operator = (UVTraceSourceBase const &o) {
       
    41       return *this;
       
    42   }
       
    43   ~UVTraceSourceBase () {}
       
    44 
       
    45   void AddCallback (CallbackBase const & callback, TraceContext const & context) {
       
    46     m_callback.AddCallback (callback, context);
       
    47   }
       
    48   void RemoveCallback (CallbackBase const & callback) {
       
    49     m_callback.RemoveCallback (callback);
       
    50   }
       
    51 
       
    52 protected:
       
    53   void Notify (uint64_t oldVal, uint64_t newVal) {
       
    54       if (oldVal != newVal) 
       
    55         {
       
    56           m_callback (oldVal, newVal);
       
    57         }
       
    58   }
       
    59 private:
       
    60   ChangeNotifyCallback m_callback;
       
    61 };
       
    62 
       
    63 template <typename T>
       
    64 class SVTraceSource;
       
    65 
       
    66 
       
    67 /**
       
    68  * \brief trace variables of type "unsigned integer"
       
    69  * \ingroup lowleveltracing
       
    70  *
       
    71  * This template class implements a POD type: it
       
    72  * behaves like any other variable of type "unsigned integer"
       
    73  * except that it also reports any changes to its
       
    74  * value with its internal callback.
       
    75  *
       
    76  * To instantiate a 32-bit unsigned variable (to store
       
    77  * a TCP counter for example), you would create a variable of type
       
    78  * ns3::UVTraceSource<uint32_t> :
       
    79  \code
       
    80  #include <stdint.h>
       
    81  #include "ns3/uv-trace-source.h"
       
    82 
       
    83  ns3::UVTraceSource<uint32_t> var;
       
    84  \endcode
       
    85  * and you would use it like any other variable of type uint32_t:
       
    86  \code
       
    87  var += 12;
       
    88  var = 10;
       
    89  \endcode
       
    90  */
       
    91 template <typename T>
       
    92 class UVTraceSource : public UVTraceSourceBase {
       
    93 public:
       
    94   UVTraceSource ()
       
    95       : m_var ()
       
    96   {}
       
    97   UVTraceSource (T const &var) 
       
    98       : m_var (var)
       
    99   {}
       
   100 
       
   101   UVTraceSource &operator = (UVTraceSource const &o) {
       
   102       Assign (o.Get ());
       
   103       return *this;
       
   104   }
       
   105   template <typename TT>
       
   106   UVTraceSource &operator = (UVTraceSource<TT> const &o) {
       
   107       Assign (o.Get ());
       
   108       return *this;
       
   109   }
       
   110   template <typename TT>
       
   111   UVTraceSource &operator = (SVTraceSource<TT> const &o) {
       
   112       Assign (o.Get ());
       
   113       return *this;
       
   114   }
       
   115   UVTraceSource &operator++ () {
       
   116       Assign (Get () + 1);
       
   117       return *this;
       
   118   }
       
   119   UVTraceSource &operator-- () {
       
   120       Assign (Get () - 1);
       
   121       return *this;
       
   122   }
       
   123   UVTraceSource operator++ (int) {
       
   124       UVTraceSource old (*this);
       
   125       ++*this;
       
   126       return old;
       
   127   }
       
   128   UVTraceSource operator-- (int) {
       
   129       UVTraceSource old (*this);
       
   130       --*this;
       
   131       return old;
       
   132   }
       
   133   operator T () const {
       
   134       return Get ();
       
   135   }
       
   136 
       
   137 
       
   138   void Assign (T var) {
       
   139       Notify (m_var, var);
       
   140       m_var = var;
       
   141   }
       
   142   T Get (void) const {
       
   143       return m_var;
       
   144   }
       
   145 
       
   146 private:
       
   147   T m_var;
       
   148 };
       
   149 
       
   150 template <typename T>
       
   151 UVTraceSource<T> &operator += (UVTraceSource<T> &lhs, UVTraceSource<T> const &rhs) {
       
   152   lhs.Assign (lhs.Get () + rhs.Get ());
       
   153   return lhs;
       
   154 }
       
   155 template <typename T>
       
   156 UVTraceSource<T> &operator -= (UVTraceSource<T> &lhs, UVTraceSource<T> const &rhs) {
       
   157   lhs.Assign (lhs.Get () - rhs.Get ());
       
   158   return lhs;
       
   159 }
       
   160 template <typename T>
       
   161 UVTraceSource<T> &operator *= (UVTraceSource<T> &lhs, UVTraceSource<T> const &rhs) {
       
   162   lhs.Assign (lhs.Get () * rhs.Get ());
       
   163   return lhs;
       
   164 }
       
   165 template <typename T>
       
   166 UVTraceSource<T> &operator /= (UVTraceSource<T> &lhs, UVTraceSource<T> const &rhs) {
       
   167   lhs.Assign (lhs.Get () / rhs.Get ());
       
   168   return lhs;
       
   169 }
       
   170 template <typename T>
       
   171 UVTraceSource<T> &operator <<= (UVTraceSource<T> &lhs, UVTraceSource<T> const &rhs) {
       
   172   lhs.Assign (lhs.Get () << rhs.Get ());
       
   173   return lhs;
       
   174 }
       
   175 template <typename T>
       
   176 UVTraceSource<T> &operator >>= (UVTraceSource<T> &lhs, UVTraceSource<T> const &rhs) {
       
   177   lhs.Assign (lhs.Get () >> rhs.Get ());
       
   178   return lhs;
       
   179 }
       
   180 template <typename T>
       
   181 UVTraceSource<T> &operator &= (UVTraceSource<T> &lhs, UVTraceSource<T> const &rhs) {
       
   182   lhs.Assign (lhs.Get () & rhs.Get ());
       
   183   return lhs;
       
   184 }
       
   185 template <typename T>
       
   186 UVTraceSource<T> &operator |= (UVTraceSource<T> &lhs, UVTraceSource<T> const &rhs) {
       
   187   lhs.Assign (lhs.Get () | rhs.Get ());
       
   188   return lhs;
       
   189 }
       
   190 template <typename T>
       
   191 UVTraceSource<T> &operator ^= (UVTraceSource<T> &lhs, UVTraceSource<T> const &rhs) {
       
   192   lhs.Assign (lhs.Get () ^ rhs.Get ());
       
   193   return lhs;
       
   194 }
       
   195 
       
   196 
       
   197 template <typename T, typename U>
       
   198 UVTraceSource<T> &operator += (UVTraceSource<T> &lhs, U const &rhs) {
       
   199   lhs.Assign (lhs.Get () + rhs);
       
   200   return lhs;
       
   201 }
       
   202 template <typename T, typename U>
       
   203 UVTraceSource<T> &operator -= (UVTraceSource<T> &lhs, U const &rhs) {
       
   204   lhs.Assign (lhs.Get () - rhs);
       
   205   return lhs;
       
   206 }
       
   207 template <typename T, typename U>
       
   208 UVTraceSource<T> &operator *= (UVTraceSource<T> &lhs, U const &rhs) {
       
   209   lhs.Assign (lhs.Get () * rhs);
       
   210   return lhs;
       
   211 }
       
   212 template <typename T, typename U>
       
   213 UVTraceSource<T> &operator /= (UVTraceSource<T> &lhs, U const &rhs) {
       
   214   lhs.Assign (lhs.Get () / rhs);
       
   215   return lhs;
       
   216 }
       
   217 template <typename T, typename U>
       
   218 UVTraceSource<T> &operator <<= (UVTraceSource<T> &lhs, U const &rhs) {
       
   219   lhs.Assign (lhs.Get () << rhs);
       
   220   return lhs;
       
   221 }
       
   222 template <typename T, typename U>
       
   223 UVTraceSource<T> &operator >>= (UVTraceSource<T> &lhs, U const &rhs) {
       
   224   lhs.Assign (lhs.Get () >> rhs);
       
   225   return lhs;
       
   226 }
       
   227 template <typename T, typename U>
       
   228 UVTraceSource<T> &operator &= (UVTraceSource<T> &lhs, U const &rhs) {
       
   229   lhs.Assign (lhs.Get () & rhs);
       
   230   return lhs;
       
   231 }
       
   232 template <typename T, typename U>
       
   233 UVTraceSource<T> &operator |= (UVTraceSource<T> &lhs, U const &rhs) {
       
   234   lhs.Assign (lhs.Get () | rhs);
       
   235   return lhs;
       
   236 }
       
   237 template <typename T, typename U>
       
   238 UVTraceSource<T> &operator ^= (UVTraceSource<T> &lhs, U const &rhs) {
       
   239   lhs.Assign (lhs.Get () ^ rhs);
       
   240   return lhs;
       
   241 }
       
   242 
       
   243 }; // namespace ns3
       
   244 
       
   245 #endif /* UV_TRACE_SOURCE_H */