src/common/sv-trace-source.h
changeset 345 47b41507a45a
child 393 18ed386bee75
equal deleted inserted replaced
344:b547ec7dbbc1 345:47b41507a45a
       
     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 SV_TRACE_SOURCE_H
       
    23 #define SV_TRACE_SOURCE_H
       
    24 
       
    25 #include "callback-trace-source.h"
       
    26 #include <stdint.h>
       
    27 
       
    28 namespace ns3 {
       
    29 
       
    30 class SVTraceSourceBase {
       
    31 public:
       
    32   typedef CallbackTraceSource<int64_t, int64_t> ChangeNotifyCallback;
       
    33 
       
    34   SVTraceSourceBase () {}
       
    35   SVTraceSourceBase (SVTraceSourceBase const &o) {}
       
    36   SVTraceSourceBase &operator = (SVTraceSourceBase const &o) {
       
    37       return *this;
       
    38   }
       
    39 
       
    40   ~SVTraceSourceBase () {}
       
    41 
       
    42   void AddCallback (CallbackBase const & callback, TraceContext const & context) {
       
    43     m_callback.AddCallback (callback, context);
       
    44   }
       
    45   void RemoveCallback (CallbackBase const & callback) {
       
    46     m_callback.RemoveCallback (callback);
       
    47   }
       
    48 protected:
       
    49   void Notify (int64_t oldVal, int64_t newVal) {
       
    50       if (oldVal != newVal) 
       
    51         {
       
    52           m_callback (oldVal, newVal);
       
    53         }
       
    54   }
       
    55 private:
       
    56   ChangeNotifyCallback m_callback;
       
    57 };
       
    58 
       
    59 template <typename T>
       
    60 class UVTraceSource;
       
    61 
       
    62 
       
    63 /**
       
    64  * \brief trace variables of type "signed integer"
       
    65  * \ingroup tracing
       
    66  *
       
    67  * This template class implements a POD type: it
       
    68  * behaves like any other variable of type "signed integer"
       
    69  * except that it also reports any changes to its
       
    70  * value with its internal callback.
       
    71  *
       
    72  * To instantiate a 32-bit signed variable (to store
       
    73  * a TCP counter for example), you would create a variable of type
       
    74  * ns3::UVTraceSource<int32_t> :
       
    75  \code
       
    76  #include <stdint.h>
       
    77  #include "ns3/sv-trace-source.h"
       
    78 
       
    79  ns3::SVTraceSource<uint16_t> var;
       
    80  \endcode
       
    81  * and you would use it like any other variable of type int32_t:
       
    82  \code
       
    83  var += 12;
       
    84  var = 10;
       
    85  var = -10;
       
    86  \endcode
       
    87  */
       
    88 template <typename T>
       
    89 class SVTraceSource : public SVTraceSourceBase {
       
    90 public:
       
    91   SVTraceSource ()
       
    92       : m_var (0)
       
    93   {}
       
    94   SVTraceSource (T const &var) 
       
    95       : m_var (var)
       
    96   {}
       
    97 
       
    98   SVTraceSource &operator = (SVTraceSource const &o) {
       
    99       Assign (o.Get ());
       
   100       return *this;
       
   101   }
       
   102   template <typename TT>
       
   103   SVTraceSource &operator = (SVTraceSource<TT> const &o) {
       
   104       Assign (o.Get ());
       
   105       return *this;
       
   106   }
       
   107   template <typename TT>
       
   108   SVTraceSource &operator = (UVTraceSource<TT> const &o) {
       
   109       Assign (o.Get ());
       
   110       return *this;
       
   111   }
       
   112   SVTraceSource &operator++ () {
       
   113       Assign (Get () + 1);
       
   114       return *this;
       
   115   }
       
   116   SVTraceSource &operator-- () {
       
   117       Assign (Get () - 1);
       
   118       return *this;
       
   119   }
       
   120   SVTraceSource operator++ (int) {
       
   121       SVTraceSource old (*this);
       
   122       ++*this;
       
   123       return old;
       
   124   }
       
   125   SVTraceSource operator-- (int) {
       
   126       SVTraceSource old (*this);
       
   127       --*this;
       
   128       return old;
       
   129   }
       
   130   operator T () const {
       
   131       return Get ();
       
   132   }
       
   133 
       
   134 
       
   135   void Assign (T var) {
       
   136       Notify (m_var, var);
       
   137       m_var = var;
       
   138   }
       
   139   T Get (void) const {
       
   140       return m_var;
       
   141   }
       
   142 
       
   143 private:
       
   144   T m_var;
       
   145 };
       
   146 
       
   147 template <typename T>
       
   148 SVTraceSource<T> &operator += (SVTraceSource<T> &lhs, SVTraceSource<T> const &rhs) {
       
   149   lhs.Assign (lhs.Get () + rhs.Get ());
       
   150   return lhs;
       
   151 }
       
   152 template <typename T>
       
   153 SVTraceSource<T> &operator -= (SVTraceSource<T> &lhs, SVTraceSource<T> const &rhs) {
       
   154   lhs.Assign (lhs.Get () - rhs.Get ());
       
   155   return lhs;
       
   156 }
       
   157 template <typename T>
       
   158 SVTraceSource<T> &operator *= (SVTraceSource<T> &lhs, SVTraceSource<T> const &rhs) {
       
   159   lhs.Assign (lhs.Get () * rhs.Get ());
       
   160   return lhs;
       
   161 }
       
   162 template <typename T>
       
   163 SVTraceSource<T> &operator /= (SVTraceSource<T> &lhs, SVTraceSource<T> const &rhs) {
       
   164   lhs.Assign (lhs.Get () / rhs.Get ());
       
   165   return lhs;
       
   166 }
       
   167 template <typename T>
       
   168 SVTraceSource<T> &operator <<= (SVTraceSource<T> &lhs, SVTraceSource<T> const &rhs) {
       
   169   lhs.Assign (lhs.Get () << rhs.Get ());
       
   170   return lhs;
       
   171 }
       
   172 template <typename T>
       
   173 SVTraceSource<T> &operator >>= (SVTraceSource<T> &lhs, SVTraceSource<T> const &rhs) {
       
   174   lhs.Assign (lhs.Get () >> rhs.Get ());
       
   175   return lhs;
       
   176 }
       
   177 template <typename T>
       
   178 SVTraceSource<T> &operator &= (SVTraceSource<T> &lhs, SVTraceSource<T> const &rhs) {
       
   179   lhs.Assign (lhs.Get () & rhs.Get ());
       
   180   return lhs;
       
   181 }
       
   182 template <typename T>
       
   183 SVTraceSource<T> &operator |= (SVTraceSource<T> &lhs, SVTraceSource<T> const &rhs) {
       
   184   lhs.Assign (lhs.Get () | rhs.Get ());
       
   185   return lhs;
       
   186 }
       
   187 template <typename T>
       
   188 SVTraceSource<T> &operator ^= (SVTraceSource<T> &lhs, SVTraceSource<T> const &rhs) {
       
   189   lhs.Assign (lhs.Get () ^ rhs.Get ());
       
   190   return lhs;
       
   191 }
       
   192 
       
   193 
       
   194 template <typename T, typename U>
       
   195 SVTraceSource<T> &operator += (SVTraceSource<T> &lhs, U const &rhs) {
       
   196   lhs.Assign (lhs.Get () + rhs);
       
   197   return lhs;
       
   198 }
       
   199 template <typename T, typename U>
       
   200 SVTraceSource<T> &operator -= (SVTraceSource<T> &lhs, U const &rhs) {
       
   201   lhs.Assign (lhs.Get () - rhs);
       
   202   return lhs;
       
   203 }
       
   204 template <typename T, typename U>
       
   205 SVTraceSource<T> &operator *= (SVTraceSource<T> &lhs, U const &rhs) {
       
   206   lhs.Assign (lhs.Get () * rhs);
       
   207   return lhs;
       
   208 }
       
   209 template <typename T, typename U>
       
   210 SVTraceSource<T> &operator /= (SVTraceSource<T> &lhs, U const &rhs) {
       
   211   lhs.Assign (lhs.Get () / rhs);
       
   212   return lhs;
       
   213 }
       
   214 template <typename T, typename U>
       
   215 SVTraceSource<T> &operator <<= (SVTraceSource<T> &lhs, U const &rhs) {
       
   216   lhs.Assign (lhs.Get () << rhs);
       
   217   return lhs;
       
   218 }
       
   219 template <typename T, typename U>
       
   220 SVTraceSource<T> &operator >>= (SVTraceSource<T> &lhs, U const &rhs) {
       
   221   lhs.Assign (lhs.Get () >> rhs);
       
   222   return lhs;
       
   223 }
       
   224 template <typename T, typename U>
       
   225 SVTraceSource<T> &operator &= (SVTraceSource<T> &lhs, U const &rhs) {
       
   226   lhs.Assign (lhs.Get () & rhs);
       
   227   return lhs;
       
   228 }
       
   229 template <typename T, typename U>
       
   230 SVTraceSource<T> &operator |= (SVTraceSource<T> &lhs, U const &rhs) {
       
   231   lhs.Assign (lhs.Get () | rhs);
       
   232   return lhs;
       
   233 }
       
   234 template <typename T, typename U>
       
   235 SVTraceSource<T> &operator ^= (SVTraceSource<T> &lhs, U const &rhs) {
       
   236   lhs.Assign (lhs.Get () ^ rhs);
       
   237   return lhs;
       
   238 }
       
   239 
       
   240 }; // namespace ns3
       
   241 
       
   242 #endif /* SV_TRACE_SOURCE_H */