src/core/traced-value.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) 2005,2006,2007 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  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
    19  */
    20 #ifndef TRACED_VALUE_H
    21 #define TRACED_VALUE_H
    22 
    23 #include "traced-callback.h"
    24 #include "integer.h"
    25 #include "uinteger.h"
    26 #include "boolean.h"
    27 #include "double.h"
    28 #include "enum.h"
    29 
    30 #define TRACED_VALUE_DEBUG(x)
    31 
    32 namespace ns3 {
    33 
    34 /**
    35  * \ingroup core
    36  * \defgroup tracing Tracing
    37  */
    38 
    39 /**
    40  * \ingroup tracing
    41  *
    42  * \brief trace classes with value semantics
    43  *
    44  * If you want to trace the change of value of a class or
    45  * primitive type which have value semantics (they _must_
    46  * support operator !=), you can wrap them in an instance of
    47  * this template: this instance will behave just like
    48  * the original class (if it did not export any special method),
    49  * and will define Connect/DisconnectWithoutContext methods to work
    50  * with ns3::MakeTraceSourceAccessor.
    51  */
    52 template <typename T>
    53 class TracedValue
    54 {
    55 public:
    56   TracedValue ()
    57     : m_v () {}
    58   TracedValue (const TracedValue &o)
    59     : m_v (o.m_v) {}
    60   TracedValue (const T &v)
    61     : m_v (v) {}
    62   operator T () const {
    63     return m_v;
    64   }
    65   TracedValue &operator = (const TracedValue &o) {
    66     TRACED_VALUE_DEBUG ("x=");
    67     Set (o.m_v);
    68     return *this;
    69   }
    70   TracedValue (const IntegerValue &value) 
    71     : m_v (value.Get ()) {}
    72   operator IntegerValue () const {
    73     return IntegerValue (m_v);
    74   }
    75   TracedValue (const UintegerValue &value)
    76     : m_v (value.Get ()) {}
    77   operator UintegerValue () const {
    78     return UintegerValue (m_v);
    79   }
    80   TracedValue (const BooleanValue &value)
    81     : m_v (value.Get ()) {}
    82   operator BooleanValue () const {
    83     return BooleanValue (m_v);
    84   }
    85   TracedValue (const EnumValue &value)
    86     : m_v (value.Get ()) {}
    87   operator EnumValue () const {
    88     return EnumValue (m_v);
    89   }
    90   void ConnectWithoutContext (const CallbackBase &cb) {
    91     m_cb.ConnectWithoutContext (cb);
    92   }
    93   void Connect (const CallbackBase &cb, std::string path) {
    94     m_cb.Connect (cb, path);
    95   }
    96   void DisconnectWithoutContext (const CallbackBase &cb) {
    97     m_cb.DisconnectWithoutContext (cb);
    98   }
    99   void Disconnect (const CallbackBase &cb, std::string path) {
   100     m_cb.Disconnect (cb, path);
   101   }
   102   void Set (const T &v) {
   103     if (m_v != v)
   104       {
   105 	m_cb (m_v, v);
   106 	m_v = v;
   107       }
   108   }
   109   T Get (void) const {
   110     return m_v;
   111   }
   112   TracedValue &operator++ () {
   113     TRACED_VALUE_DEBUG ("++x");
   114     T tmp = Get ();
   115     ++tmp;
   116     Set (tmp);
   117     return *this;
   118   }
   119   TracedValue &operator-- () {
   120     TRACED_VALUE_DEBUG ("--x");
   121     T tmp = Get ();
   122     --tmp;
   123     Set (tmp);
   124     return *this;
   125   }
   126   TracedValue operator++ (int) {
   127     TRACED_VALUE_DEBUG ("x++");
   128     TracedValue old (*this);
   129     T tmp = Get ();
   130     tmp++;
   131     Set (tmp);
   132     return old;
   133   }
   134   TracedValue operator-- (int) {
   135     TRACED_VALUE_DEBUG ("x--");
   136     TracedValue old (*this);
   137     T tmp = Get ();
   138     tmp--;
   139     Set (tmp);
   140     return old;
   141   }
   142 private:
   143   T m_v;
   144   TracedCallback<T,T> m_cb;
   145 };
   146 
   147 template <typename T>
   148 std::ostream& operator << (std::ostream& os, const TracedValue<T>& rhs)
   149 {
   150   return os<<rhs.Get();
   151 }
   152 
   153 template <typename T, typename U>
   154 bool operator == (const TracedValue<T> &lhs, const TracedValue<U> &rhs)
   155 {
   156   TRACED_VALUE_DEBUG ("x==x");
   157   return lhs.Get () == rhs.Get ();
   158 }
   159 template <typename T, typename U>
   160 bool operator == (const TracedValue<T> &lhs, const U &rhs)
   161 {
   162   TRACED_VALUE_DEBUG ("x==");
   163   return lhs.Get () == rhs;
   164 }
   165 template <typename T, typename U>
   166 bool operator == (const U &lhs, const TracedValue<T> &rhs)
   167 {
   168   TRACED_VALUE_DEBUG ("==x");
   169   return lhs == rhs.Get ();
   170 }
   171 
   172 template <typename T, typename U>
   173 bool operator != (const TracedValue<T> &lhs, const TracedValue<U> &rhs)
   174 {
   175   TRACED_VALUE_DEBUG ("x!=x");
   176   return lhs.Get () != rhs.Get ();
   177 }
   178 template <typename T, typename U>
   179 bool operator != (const TracedValue<T> &lhs, const U &rhs)
   180 {
   181   TRACED_VALUE_DEBUG ("x!=");
   182   return lhs.Get () != rhs;
   183 }
   184 template <typename T, typename U>
   185 bool operator != (const U &lhs, const TracedValue<T> &rhs)
   186 {
   187   TRACED_VALUE_DEBUG ("!=x");
   188   return lhs != rhs.Get ();
   189 }
   190 
   191 template <typename T, typename U>
   192 bool operator <= (const TracedValue<T> &lhs, const TracedValue<U> &rhs)
   193 {
   194   TRACED_VALUE_DEBUG ("x<=x");
   195   return lhs.Get () <= rhs.Get ();
   196 }
   197 template <typename T, typename U>
   198 bool operator <= (const TracedValue<T> &lhs, const U &rhs)
   199 {
   200   TRACED_VALUE_DEBUG ("x<=");
   201   return lhs.Get () <= rhs;
   202 }
   203 template <typename T, typename U>
   204 bool operator <= (const U &lhs, const TracedValue<T> &rhs)
   205 {
   206   TRACED_VALUE_DEBUG ("<=x");
   207   return lhs <= rhs.Get ();
   208 }
   209 template <typename T, typename U>
   210 bool operator >= (const TracedValue<T> &lhs, const TracedValue<U> &rhs)
   211 {
   212   TRACED_VALUE_DEBUG ("x>=x");
   213   return lhs.Get () >= rhs.Get ();
   214 }
   215 template <typename T, typename U>
   216 bool operator >= (const TracedValue<T> &lhs, const U &rhs)
   217 {
   218   TRACED_VALUE_DEBUG ("x>=");
   219   return lhs.Get () >= rhs;
   220 }
   221 template <typename T, typename U>
   222 bool operator >= (const U &lhs, const TracedValue<T> &rhs)
   223 {
   224   TRACED_VALUE_DEBUG (">=x");
   225   return lhs >= rhs.Get ();
   226 }
   227 
   228 template <typename T, typename U>
   229 bool operator < (const TracedValue<T> &lhs, const TracedValue<U> &rhs)
   230 {
   231   TRACED_VALUE_DEBUG ("x<x");
   232   return lhs.Get () < rhs.Get ();
   233 }
   234 template <typename T, typename U>
   235 bool operator < (const TracedValue<T> &lhs, const U &rhs)
   236 {
   237   TRACED_VALUE_DEBUG ("x<");
   238   return lhs.Get () < rhs;
   239 }
   240 template <typename T, typename U>
   241 bool operator < (const U &lhs, const TracedValue<T> &rhs)
   242 {
   243   TRACED_VALUE_DEBUG ("<x");
   244   return lhs < rhs.Get ();
   245 }
   246 template <typename T, typename U>
   247 bool operator > (const TracedValue<T> &lhs, const TracedValue<U> &rhs)
   248 {
   249   TRACED_VALUE_DEBUG ("x>x");
   250   return lhs.Get () > rhs.Get ();
   251 }
   252 template <typename T, typename U>
   253 bool operator > (const TracedValue<T> &lhs, const U &rhs)
   254 {
   255   TRACED_VALUE_DEBUG ("x>");
   256   return lhs.Get () > rhs;
   257 }
   258 template <typename T, typename U>
   259 bool operator > (const U &lhs, const TracedValue<T> &rhs)
   260 {
   261   TRACED_VALUE_DEBUG (">x");
   262   return lhs > rhs.Get ();
   263 }
   264 template <typename T, typename U>
   265 TracedValue<T> &operator += (TracedValue<T> &lhs, const U &rhs) {
   266   TRACED_VALUE_DEBUG ("x+=");
   267   T tmp = lhs.Get ();
   268   tmp += rhs;
   269   lhs.Set (tmp);
   270   return lhs;
   271 }
   272 template <typename T, typename U>
   273 TracedValue<T> &operator -= (TracedValue<T> &lhs, const U &rhs) {
   274   TRACED_VALUE_DEBUG ("x-=");
   275   T tmp = lhs.Get ();
   276   tmp -= rhs;
   277   lhs.Set (tmp);
   278   return lhs;
   279 }
   280 template <typename T, typename U>
   281 TracedValue<T> &operator *= (TracedValue<T> &lhs, const U &rhs) {
   282   TRACED_VALUE_DEBUG ("x*=");
   283   T tmp = lhs.Get ();
   284   tmp *= rhs;
   285   lhs.Set (tmp);
   286   return lhs;
   287 }
   288 template <typename T, typename U>
   289 TracedValue<T> &operator /= (TracedValue<T> &lhs, const U &rhs) {
   290   TRACED_VALUE_DEBUG ("x/=");
   291   T tmp = lhs.Get ();
   292   tmp /= rhs;
   293   lhs.Set (tmp);
   294   return lhs;
   295 }
   296 template <typename T, typename U>
   297 TracedValue<T> &operator %= (TracedValue<T> &lhs, const U &rhs) {
   298   TRACED_VALUE_DEBUG ("x%=");
   299   T tmp = lhs.Get ();
   300   tmp %= rhs;
   301   lhs.Set (tmp);
   302   return lhs;
   303 }
   304 template <typename T, typename U>
   305 TracedValue<T> &operator <<= (TracedValue<T> &lhs, const U &rhs) {
   306   TRACED_VALUE_DEBUG ("x<<=");
   307   T tmp = lhs.Get ();
   308   tmp <<= rhs;
   309   lhs.Set (tmp);
   310   return lhs;
   311 }
   312 template <typename T, typename U>
   313 TracedValue<T> &operator >>= (TracedValue<T> &lhs, const U &rhs) {
   314   TRACED_VALUE_DEBUG ("x>>=");
   315   T tmp = lhs.Get ();
   316   tmp >>= rhs;
   317   lhs.Set (tmp);
   318   return lhs;
   319 }
   320 template <typename T, typename U>
   321 TracedValue<T> &operator &= (TracedValue<T> &lhs, const U &rhs) {
   322   TRACED_VALUE_DEBUG ("x&=");
   323   T tmp = lhs.Get ();
   324   tmp &= rhs;
   325   lhs.Set (tmp);
   326   return lhs;
   327 }
   328 template <typename T, typename U>
   329 TracedValue<T> &operator |= (TracedValue<T> &lhs, const U &rhs) {
   330   TRACED_VALUE_DEBUG ("x|=");
   331   T tmp = lhs.Get ();
   332   tmp |= rhs;
   333   lhs.Set (tmp);
   334   return lhs;
   335 }
   336 template <typename T, typename U>
   337 TracedValue<T> &operator ^= (TracedValue<T> &lhs, const U &rhs) {
   338   TRACED_VALUE_DEBUG ("x^=");
   339   T tmp = lhs.Get ();
   340   tmp ^= rhs;
   341   lhs.Set (tmp);
   342   return lhs;
   343 }
   344 template <typename T, typename U>
   345 TracedValue<T> operator + (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   346   TRACED_VALUE_DEBUG ("x+x");
   347   return TracedValue<T> (lhs.Get () + rhs.Get ());
   348 }
   349 template <typename T, typename U>
   350 TracedValue<T> operator + (const TracedValue<T> &lhs, const U &rhs) {
   351   TRACED_VALUE_DEBUG ("x+");
   352   return TracedValue<T> (lhs.Get () + rhs);
   353 }
   354 template <typename T, typename U>
   355 TracedValue<T> operator + (const U &lhs, const TracedValue<T> &rhs) {
   356   TRACED_VALUE_DEBUG ("+x");
   357   return TracedValue<T> (lhs + rhs.Get ());
   358 }
   359 
   360 template <typename T, typename U>
   361 TracedValue<T> operator - (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   362   TRACED_VALUE_DEBUG ("x-x");
   363   return TracedValue<T> (lhs.Get () - rhs.Get ());
   364 }
   365 template <typename T, typename U>
   366 TracedValue<T> operator - (const TracedValue<T> &lhs, const U &rhs) {
   367   TRACED_VALUE_DEBUG ("x-");
   368   return TracedValue<T> (lhs.Get () - rhs);
   369 }
   370 template <typename T, typename U>
   371 TracedValue<T> operator - (const U &lhs, const TracedValue<T> &rhs) {
   372   TRACED_VALUE_DEBUG ("-x");
   373   return TracedValue<T> (lhs - rhs.Get ());
   374 }
   375 
   376 template <typename T, typename U>
   377 TracedValue<T> operator * (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   378   TRACED_VALUE_DEBUG ("x*x");
   379   return TracedValue<T> (lhs.Get () * rhs.Get ());
   380 }
   381 template <typename T, typename U>
   382 TracedValue<T> operator * (const TracedValue<T> &lhs, const U &rhs) {
   383   TRACED_VALUE_DEBUG ("x*");
   384   return TracedValue<T> (lhs.Get () * rhs);
   385 }
   386 template <typename T, typename U>
   387 TracedValue<T> operator * (const U &lhs, const TracedValue<T> &rhs) {
   388   TRACED_VALUE_DEBUG ("*x");
   389   return TracedValue<T> (lhs - rhs.Get ());
   390 }
   391 
   392 template <typename T, typename U>
   393 TracedValue<T> operator / (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   394   TRACED_VALUE_DEBUG ("x/x");
   395   return TracedValue<T> (lhs.Get () / rhs.Get ());
   396 }
   397 template <typename T, typename U>
   398 TracedValue<T> operator / (const TracedValue<T> &lhs, const U &rhs) {
   399   TRACED_VALUE_DEBUG ("x/");
   400   return TracedValue<T> (lhs.Get () / rhs);
   401 }
   402 template <typename T, typename U>
   403 TracedValue<T> operator / (const U &lhs, const TracedValue<T> &rhs) {
   404   TRACED_VALUE_DEBUG ("/x");
   405   return TracedValue<T> (lhs / rhs.Get ());
   406 }
   407 
   408 template <typename T, typename U>
   409 TracedValue<T> operator % (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   410   TRACED_VALUE_DEBUG ("x%x");
   411   return TracedValue<T> (lhs.Get () % rhs.Get ());
   412 }
   413 template <typename T, typename U>
   414 TracedValue<T> operator % (const TracedValue<T> &lhs, const U &rhs) {
   415   TRACED_VALUE_DEBUG ("x%");
   416   return TracedValue<T> (lhs.Get () % rhs);
   417 }
   418 template <typename T, typename U>
   419 TracedValue<T> operator % (const U &lhs, const TracedValue<T> &rhs) {
   420   TRACED_VALUE_DEBUG ("%x");
   421   return TracedValue<T> (lhs % rhs.Get ());
   422 }
   423 
   424 template <typename T, typename U>
   425 TracedValue<T> operator ^ (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   426   TRACED_VALUE_DEBUG ("x^x");
   427   return TracedValue<T> (lhs.Get () ^ rhs.Get ());
   428 }
   429 template <typename T, typename U>
   430 TracedValue<T> operator ^ (const TracedValue<T> &lhs, const U &rhs) {
   431   TRACED_VALUE_DEBUG ("x^");
   432   return TracedValue<T> (lhs.Get () ^ rhs);
   433 }
   434 template <typename T, typename U>
   435 TracedValue<T> operator ^ (const U &lhs, const TracedValue<T> &rhs) {
   436   TRACED_VALUE_DEBUG ("^x");
   437   return TracedValue<T> (lhs ^ rhs.Get ());
   438 }
   439 
   440 template <typename T, typename U>
   441 TracedValue<T> operator | (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   442   TRACED_VALUE_DEBUG ("x|x");
   443   return TracedValue<T> (lhs.Get () | rhs.Get ());
   444 }
   445 template <typename T, typename U>
   446 TracedValue<T> operator | (const TracedValue<T> &lhs, const U &rhs) {
   447   TRACED_VALUE_DEBUG ("x|");
   448   return TracedValue<T> (lhs.Get () | rhs);
   449 }
   450 template <typename T, typename U>
   451 TracedValue<T> operator | (const U &lhs, const TracedValue<T> &rhs) {
   452   TRACED_VALUE_DEBUG ("|x");
   453   return TracedValue<T> (lhs | rhs.Get ());
   454 }
   455 
   456 template <typename T, typename U>
   457 TracedValue<T> operator & (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   458   TRACED_VALUE_DEBUG ("x&x");
   459   return TracedValue<T> (lhs.Get () & rhs.Get ());
   460 }
   461 template <typename T, typename U>
   462 TracedValue<T> operator & (const TracedValue<T> &lhs, const U &rhs) {
   463   TRACED_VALUE_DEBUG ("x&");
   464   return TracedValue<T> (lhs.Get () & rhs);
   465 }
   466 template <typename T, typename U>
   467 TracedValue<T> operator & (const U &lhs, const TracedValue<T> &rhs) {
   468   TRACED_VALUE_DEBUG ("&x");
   469   return TracedValue<T> (lhs & rhs.Get ());
   470 }
   471 
   472 template <typename T, typename U>
   473 TracedValue<T> operator << (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   474   TRACED_VALUE_DEBUG ("x<<x");
   475   return TracedValue<T> (lhs.Get () << rhs.Get ());
   476 }
   477 template <typename T, typename U>
   478 TracedValue<T> operator << (const TracedValue<T> &lhs, const U &rhs) {
   479   TRACED_VALUE_DEBUG ("x<<");
   480   return TracedValue<T> (lhs.Get () << rhs);
   481 }
   482 template <typename T, typename U>
   483 TracedValue<T> operator << (const U &lhs, const TracedValue<T> &rhs) {
   484   TRACED_VALUE_DEBUG ("<<x");
   485   return TracedValue<T> (lhs << rhs.Get ());
   486 }
   487 
   488 template <typename T, typename U>
   489 TracedValue<T> operator >> (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   490   TRACED_VALUE_DEBUG ("x>>x");
   491   return TracedValue<T> (lhs.Get () >> rhs.Get ());
   492 }
   493 template <typename T, typename U>
   494 TracedValue<T> operator >> (const TracedValue<T> &lhs, const U &rhs) {
   495   TRACED_VALUE_DEBUG ("x>>");
   496   return TracedValue<T> (lhs.Get () >> rhs);
   497 }
   498 template <typename T, typename U>
   499 TracedValue<T> operator >> (const U &lhs, const TracedValue<T> &rhs) {
   500   TRACED_VALUE_DEBUG (">>x");
   501   return TracedValue<T> (lhs >> rhs.Get ());
   502 }
   503 
   504 
   505 template <typename T>
   506 TracedValue<T> operator + (const TracedValue<T> &lhs) {
   507   TRACED_VALUE_DEBUG ("(+x)");
   508   return TracedValue<T> (+lhs.Get ());
   509 }
   510 template <typename T>
   511 TracedValue<T> operator - (const TracedValue<T> &lhs) {
   512   TRACED_VALUE_DEBUG ("(-x)");
   513   return TracedValue<T> (-lhs.Get ());
   514 }
   515 template <typename T>
   516 TracedValue<T> operator ~ (const TracedValue<T> &lhs) {
   517   TRACED_VALUE_DEBUG ("(~x)");
   518   return TracedValue<T> (~lhs.Get ());
   519 }
   520 template <typename T>
   521 TracedValue<T> operator ! (const TracedValue<T> &lhs) {
   522   TRACED_VALUE_DEBUG ("(!x)");
   523   return TracedValue<T> (!lhs.Get ());
   524 }
   525 
   526 
   527 } // namespace ns3
   528 
   529 #endif /* TRACED_VALUE_H */