src/core/traced-value.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Fri, 07 Mar 2008 12:18:34 -0800
changeset 2569 d5cff2968984
parent 2531 b451b5fc8b57
child 2588 c6722f33bb59
permissions -rw-r--r--
make Disconnect work with trace contexts.
     1 #ifndef TRACED_VALUE_H
     2 #define TRACED_VALUE_H
     3 
     4 #include "traced-callback.h"
     5 #include "integer.h"
     6 #include "uinteger.h"
     7 #include "boolean.h"
     8 #include "double.h"
     9 #include "enum.h"
    10 
    11 #define TRACED_VALUE_DEBUG(x)
    12 
    13 namespace ns3 {
    14 
    15 template <typename T>
    16 class TracedValue
    17 {
    18 public:
    19   TracedValue ()
    20     : m_v () {}
    21   TracedValue (const TracedValue &o)
    22     : m_v (o.m_v) {}
    23   TracedValue (const T &v)
    24     : m_v (v) {}
    25   operator T () const {
    26     return m_v;
    27   }
    28   TracedValue &operator = (const TracedValue &o) {
    29     TRACED_VALUE_DEBUG ("x=");
    30     Set (o.m_v);
    31     return *this;
    32   }
    33   TracedValue (const Integer &value) 
    34     : m_v (value.Get ()) {}
    35   operator Integer () const {
    36     return Integer (m_v);
    37   }
    38   TracedValue (const Uinteger &value)
    39     : m_v (value.Get ()) {}
    40   operator Uinteger () const {
    41     return Uinteger (m_v);
    42   }
    43   TracedValue (const Boolean &value)
    44     : m_v (value.Get ()) {}
    45   operator Boolean () const {
    46     return Boolean (m_v);
    47   }
    48   TracedValue (const Enum &value)
    49     : m_v (value.Get ()) {}
    50   operator Enum () const {
    51     return Enum (m_v);
    52   }
    53   void Connect (const CallbackBase &cb) {
    54     m_cb.Connect (cb);
    55   }
    56   void ConnectWithContext (const CallbackBase &cb, std::string path) {
    57     m_cb.ConnectWithContext (cb, path);
    58   }
    59   void Disconnect (const CallbackBase &cb) {
    60     m_cb.Disconnect (cb);
    61   }
    62   void DisconnectWithContext (const CallbackBase &cb, std::string path) {
    63     m_cb.DisconnectWithContext (cb, path);
    64   }
    65   void Set (const T &v) {
    66     if (m_v != v)
    67       {
    68 	m_cb (m_v, v);
    69 	m_v = v;
    70       }
    71   }
    72   T Get (void) const {
    73     return m_v;
    74   }
    75   TracedValue &operator++ () {
    76     TRACED_VALUE_DEBUG ("++x");
    77     T tmp = Get ();
    78     ++tmp;
    79     Set (tmp);
    80     return *this;
    81   }
    82   TracedValue &operator-- () {
    83     TRACED_VALUE_DEBUG ("--x");
    84     T tmp = Get ();
    85     --tmp;
    86     Set (tmp);
    87     return *this;
    88   }
    89   TracedValue operator++ (int) {
    90     TRACED_VALUE_DEBUG ("x++");
    91     TracedValue old (*this);
    92     T tmp = Get ();
    93     tmp++;
    94     Set (tmp);
    95     return old;
    96   }
    97   TracedValue operator-- (int) {
    98     TRACED_VALUE_DEBUG ("x--");
    99     TracedValue old (*this);
   100     T tmp = Get ();
   101     tmp--;
   102     Set (tmp);
   103     return old;
   104   }
   105 private:
   106   T m_v;
   107   TracedCallback<T,T> m_cb;
   108 };
   109 
   110 template <typename T, typename U>
   111 bool operator == (const TracedValue<T> &lhs, const TracedValue<U> &rhs)
   112 {
   113   TRACED_VALUE_DEBUG ("x==x");
   114   return lhs.Get () == rhs.Get ();
   115 }
   116 template <typename T, typename U>
   117 bool operator == (const TracedValue<T> &lhs, const U &rhs)
   118 {
   119   TRACED_VALUE_DEBUG ("x==");
   120   return lhs.Get () == rhs;
   121 }
   122 template <typename T, typename U>
   123 bool operator == (const U &lhs, const TracedValue<T> &rhs)
   124 {
   125   TRACED_VALUE_DEBUG ("==x");
   126   return lhs == rhs.Get ();
   127 }
   128 
   129 template <typename T, typename U>
   130 bool operator != (const TracedValue<T> &lhs, const TracedValue<U> &rhs)
   131 {
   132   TRACED_VALUE_DEBUG ("x!=x");
   133   return lhs.Get () != rhs.Get ();
   134 }
   135 template <typename T, typename U>
   136 bool operator != (const TracedValue<T> &lhs, const U &rhs)
   137 {
   138   TRACED_VALUE_DEBUG ("x!=");
   139   return lhs.Get () != rhs;
   140 }
   141 template <typename T, typename U>
   142 bool operator != (const U &lhs, const TracedValue<T> &rhs)
   143 {
   144   TRACED_VALUE_DEBUG ("!=x");
   145   return lhs != rhs.Get ();
   146 }
   147 
   148 template <typename T, typename U>
   149 bool operator <= (const TracedValue<T> &lhs, const TracedValue<U> &rhs)
   150 {
   151   TRACED_VALUE_DEBUG ("x<=x");
   152   return lhs.Get () <= rhs.Get ();
   153 }
   154 template <typename T, typename U>
   155 bool operator <= (const TracedValue<T> &lhs, const U &rhs)
   156 {
   157   TRACED_VALUE_DEBUG ("x<=");
   158   return lhs.Get () <= rhs;
   159 }
   160 template <typename T, typename U>
   161 bool operator <= (const U &lhs, const TracedValue<T> &rhs)
   162 {
   163   TRACED_VALUE_DEBUG ("<=x");
   164   return lhs <= rhs.Get ();
   165 }
   166 template <typename T, typename U>
   167 bool operator >= (const TracedValue<T> &lhs, const TracedValue<U> &rhs)
   168 {
   169   TRACED_VALUE_DEBUG ("x>=x");
   170   return lhs.Get () >= rhs.Get ();
   171 }
   172 template <typename T, typename U>
   173 bool operator >= (const TracedValue<T> &lhs, const U &rhs)
   174 {
   175   TRACED_VALUE_DEBUG ("x>=");
   176   return lhs.Get () >= rhs;
   177 }
   178 template <typename T, typename U>
   179 bool operator >= (const U &lhs, const TracedValue<T> &rhs)
   180 {
   181   TRACED_VALUE_DEBUG (">=x");
   182   return lhs >= rhs.Get ();
   183 }
   184 
   185 template <typename T, typename U>
   186 bool operator < (const TracedValue<T> &lhs, const TracedValue<U> &rhs)
   187 {
   188   TRACED_VALUE_DEBUG ("x<x");
   189   return lhs.Get () < rhs.Get ();
   190 }
   191 template <typename T, typename U>
   192 bool operator < (const TracedValue<T> &lhs, const U &rhs)
   193 {
   194   TRACED_VALUE_DEBUG ("x<");
   195   return lhs.Get () < rhs;
   196 }
   197 template <typename T, typename U>
   198 bool operator < (const U &lhs, const TracedValue<T> &rhs)
   199 {
   200   TRACED_VALUE_DEBUG ("<x");
   201   return lhs < rhs.Get ();
   202 }
   203 template <typename T, typename U>
   204 bool operator > (const TracedValue<T> &lhs, const TracedValue<U> &rhs)
   205 {
   206   TRACED_VALUE_DEBUG ("x>x");
   207   return lhs.Get () > rhs.Get ();
   208 }
   209 template <typename T, typename U>
   210 bool operator > (const TracedValue<T> &lhs, const U &rhs)
   211 {
   212   TRACED_VALUE_DEBUG ("x>");
   213   return lhs.Get () > rhs;
   214 }
   215 template <typename T, typename U>
   216 bool operator > (const U &lhs, const TracedValue<T> &rhs)
   217 {
   218   TRACED_VALUE_DEBUG (">x");
   219   return lhs > rhs.Get ();
   220 }
   221 template <typename T, typename U>
   222 TracedValue<T> &operator += (TracedValue<T> &lhs, const U &rhs) {
   223   TRACED_VALUE_DEBUG ("x+=");
   224   T tmp = lhs.Get ();
   225   tmp += rhs;
   226   lhs.Set (tmp);
   227   return lhs;
   228 }
   229 template <typename T, typename U>
   230 TracedValue<T> &operator -= (TracedValue<T> &lhs, const U &rhs) {
   231   TRACED_VALUE_DEBUG ("x-=");
   232   T tmp = lhs.Get ();
   233   tmp -= rhs;
   234   lhs.Set (tmp);
   235   return lhs;
   236 }
   237 template <typename T, typename U>
   238 TracedValue<T> &operator *= (TracedValue<T> &lhs, const U &rhs) {
   239   TRACED_VALUE_DEBUG ("x*=");
   240   T tmp = lhs.Get ();
   241   tmp *= rhs;
   242   lhs.Set (tmp);
   243   return lhs;
   244 }
   245 template <typename T, typename U>
   246 TracedValue<T> &operator /= (TracedValue<T> &lhs, const U &rhs) {
   247   TRACED_VALUE_DEBUG ("x/=");
   248   T tmp = lhs.Get ();
   249   tmp /= rhs;
   250   lhs.Set (tmp);
   251   return lhs;
   252 }
   253 template <typename T, typename U>
   254 TracedValue<T> &operator %= (TracedValue<T> &lhs, const U &rhs) {
   255   TRACED_VALUE_DEBUG ("x%=");
   256   T tmp = lhs.Get ();
   257   tmp %= rhs;
   258   lhs.Set (tmp);
   259   return lhs;
   260 }
   261 template <typename T, typename U>
   262 TracedValue<T> &operator <<= (TracedValue<T> &lhs, const U &rhs) {
   263   TRACED_VALUE_DEBUG ("x<<=");
   264   T tmp = lhs.Get ();
   265   tmp <<= rhs;
   266   lhs.Set (tmp);
   267   return lhs;
   268 }
   269 template <typename T, typename U>
   270 TracedValue<T> &operator >>= (TracedValue<T> &lhs, const U &rhs) {
   271   TRACED_VALUE_DEBUG ("x>>=");
   272   T tmp = lhs.Get ();
   273   tmp >>= rhs;
   274   lhs.Set (tmp);
   275   return lhs;
   276 }
   277 template <typename T, typename U>
   278 TracedValue<T> &operator &= (TracedValue<T> &lhs, const U &rhs) {
   279   TRACED_VALUE_DEBUG ("x&=");
   280   T tmp = lhs.Get ();
   281   tmp &= rhs;
   282   lhs.Set (tmp);
   283   return lhs;
   284 }
   285 template <typename T, typename U>
   286 TracedValue<T> &operator |= (TracedValue<T> &lhs, const U &rhs) {
   287   TRACED_VALUE_DEBUG ("x|=");
   288   T tmp = lhs.Get ();
   289   tmp |= rhs;
   290   lhs.Set (tmp);
   291   return lhs;
   292 }
   293 template <typename T, typename U>
   294 TracedValue<T> &operator ^= (TracedValue<T> &lhs, const U &rhs) {
   295   TRACED_VALUE_DEBUG ("x^=");
   296   T tmp = lhs.Get ();
   297   tmp ^= rhs;
   298   lhs.Set (tmp);
   299   return lhs;
   300 }
   301 template <typename T, typename U>
   302 TracedValue<T> operator + (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   303   TRACED_VALUE_DEBUG ("x+x");
   304   return TracedValue<T> (lhs.Get () + rhs.Get ());
   305 }
   306 template <typename T, typename U>
   307 TracedValue<T> operator + (const TracedValue<T> &lhs, const U &rhs) {
   308   TRACED_VALUE_DEBUG ("x+");
   309   return TracedValue<T> (lhs.Get () + rhs);
   310 }
   311 template <typename T, typename U>
   312 TracedValue<T> operator + (const U &lhs, const TracedValue<T> &rhs) {
   313   TRACED_VALUE_DEBUG ("+x");
   314   return TracedValue<T> (lhs + rhs.Get ());
   315 }
   316 
   317 template <typename T, typename U>
   318 TracedValue<T> operator - (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   319   TRACED_VALUE_DEBUG ("x-x");
   320   return TracedValue<T> (lhs.Get () - rhs.Get ());
   321 }
   322 template <typename T, typename U>
   323 TracedValue<T> operator - (const TracedValue<T> &lhs, const U &rhs) {
   324   TRACED_VALUE_DEBUG ("x-");
   325   return TracedValue<T> (lhs.Get () - rhs);
   326 }
   327 template <typename T, typename U>
   328 TracedValue<T> operator - (const U &lhs, const TracedValue<T> &rhs) {
   329   TRACED_VALUE_DEBUG ("-x");
   330   return TracedValue<T> (lhs - rhs.Get ());
   331 }
   332 
   333 template <typename T, typename U>
   334 TracedValue<T> operator * (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   335   TRACED_VALUE_DEBUG ("x*x");
   336   return TracedValue<T> (lhs.Get () * rhs.Get ());
   337 }
   338 template <typename T, typename U>
   339 TracedValue<T> operator * (const TracedValue<T> &lhs, const U &rhs) {
   340   TRACED_VALUE_DEBUG ("x*");
   341   return TracedValue<T> (lhs.Get () * rhs);
   342 }
   343 template <typename T, typename U>
   344 TracedValue<T> operator * (const U &lhs, const TracedValue<T> &rhs) {
   345   TRACED_VALUE_DEBUG ("*x");
   346   return TracedValue<T> (lhs - rhs.Get ());
   347 }
   348 
   349 template <typename T, typename U>
   350 TracedValue<T> operator / (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   351   TRACED_VALUE_DEBUG ("x/x");
   352   return TracedValue<T> (lhs.Get () / rhs.Get ());
   353 }
   354 template <typename T, typename U>
   355 TracedValue<T> operator / (const TracedValue<T> &lhs, const U &rhs) {
   356   TRACED_VALUE_DEBUG ("x/");
   357   return TracedValue<T> (lhs.Get () / rhs);
   358 }
   359 template <typename T, typename U>
   360 TracedValue<T> operator / (const U &lhs, const TracedValue<T> &rhs) {
   361   TRACED_VALUE_DEBUG ("/x");
   362   return TracedValue<T> (lhs / rhs.Get ());
   363 }
   364 
   365 template <typename T, typename U>
   366 TracedValue<T> operator % (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   367   TRACED_VALUE_DEBUG ("x%x");
   368   return TracedValue<T> (lhs.Get () % rhs.Get ());
   369 }
   370 template <typename T, typename U>
   371 TracedValue<T> operator % (const TracedValue<T> &lhs, const U &rhs) {
   372   TRACED_VALUE_DEBUG ("x%");
   373   return TracedValue<T> (lhs.Get () % rhs);
   374 }
   375 template <typename T, typename U>
   376 TracedValue<T> operator % (const U &lhs, const TracedValue<T> &rhs) {
   377   TRACED_VALUE_DEBUG ("%x");
   378   return TracedValue<T> (lhs % rhs.Get ());
   379 }
   380 
   381 template <typename T, typename U>
   382 TracedValue<T> operator ^ (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   383   TRACED_VALUE_DEBUG ("x^x");
   384   return TracedValue<T> (lhs.Get () ^ rhs.Get ());
   385 }
   386 template <typename T, typename U>
   387 TracedValue<T> operator ^ (const TracedValue<T> &lhs, const U &rhs) {
   388   TRACED_VALUE_DEBUG ("x^");
   389   return TracedValue<T> (lhs.Get () ^ rhs);
   390 }
   391 template <typename T, typename U>
   392 TracedValue<T> operator ^ (const U &lhs, const TracedValue<T> &rhs) {
   393   TRACED_VALUE_DEBUG ("^x");
   394   return TracedValue<T> (lhs ^ rhs.Get ());
   395 }
   396 
   397 template <typename T, typename U>
   398 TracedValue<T> operator | (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   399   TRACED_VALUE_DEBUG ("x|x");
   400   return TracedValue<T> (lhs.Get () | rhs.Get ());
   401 }
   402 template <typename T, typename U>
   403 TracedValue<T> operator | (const TracedValue<T> &lhs, const U &rhs) {
   404   TRACED_VALUE_DEBUG ("x|");
   405   return TracedValue<T> (lhs.Get () | rhs);
   406 }
   407 template <typename T, typename U>
   408 TracedValue<T> operator | (const U &lhs, const TracedValue<T> &rhs) {
   409   TRACED_VALUE_DEBUG ("|x");
   410   return TracedValue<T> (lhs | rhs.Get ());
   411 }
   412 
   413 template <typename T, typename U>
   414 TracedValue<T> operator & (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   415   TRACED_VALUE_DEBUG ("x&x");
   416   return TracedValue<T> (lhs.Get () & rhs.Get ());
   417 }
   418 template <typename T, typename U>
   419 TracedValue<T> operator & (const TracedValue<T> &lhs, const U &rhs) {
   420   TRACED_VALUE_DEBUG ("x&");
   421   return TracedValue<T> (lhs.Get () & rhs);
   422 }
   423 template <typename T, typename U>
   424 TracedValue<T> operator & (const U &lhs, const TracedValue<T> &rhs) {
   425   TRACED_VALUE_DEBUG ("&x");
   426   return TracedValue<T> (lhs & rhs.Get ());
   427 }
   428 
   429 template <typename T, typename U>
   430 TracedValue<T> operator << (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   431   TRACED_VALUE_DEBUG ("x<<x");
   432   return TracedValue<T> (lhs.Get () << rhs.Get ());
   433 }
   434 template <typename T, typename U>
   435 TracedValue<T> operator << (const TracedValue<T> &lhs, const U &rhs) {
   436   TRACED_VALUE_DEBUG ("x<<");
   437   return TracedValue<T> (lhs.Get () << rhs);
   438 }
   439 template <typename T, typename U>
   440 TracedValue<T> operator << (const U &lhs, const TracedValue<T> &rhs) {
   441   TRACED_VALUE_DEBUG ("<<x");
   442   return TracedValue<T> (lhs << rhs.Get ());
   443 }
   444 
   445 template <typename T, typename U>
   446 TracedValue<T> operator >> (const TracedValue<T> &lhs, const TracedValue<U> &rhs) {
   447   TRACED_VALUE_DEBUG ("x>>x");
   448   return TracedValue<T> (lhs.Get () >> rhs.Get ());
   449 }
   450 template <typename T, typename U>
   451 TracedValue<T> operator >> (const TracedValue<T> &lhs, const U &rhs) {
   452   TRACED_VALUE_DEBUG ("x>>");
   453   return TracedValue<T> (lhs.Get () >> rhs);
   454 }
   455 template <typename T, typename U>
   456 TracedValue<T> operator >> (const U &lhs, const TracedValue<T> &rhs) {
   457   TRACED_VALUE_DEBUG (">>x");
   458   return TracedValue<T> (lhs >> rhs.Get ());
   459 }
   460 
   461 
   462 template <typename T>
   463 TracedValue<T> operator + (const TracedValue<T> &lhs) {
   464   TRACED_VALUE_DEBUG ("(+x)");
   465   return TracedValue<T> (+lhs.Get ());
   466 }
   467 template <typename T>
   468 TracedValue<T> operator - (const TracedValue<T> &lhs) {
   469   TRACED_VALUE_DEBUG ("(-x)");
   470   return TracedValue<T> (-lhs.Get ());
   471 }
   472 template <typename T>
   473 TracedValue<T> operator ~ (const TracedValue<T> &lhs) {
   474   TRACED_VALUE_DEBUG ("(~x)");
   475   return TracedValue<T> (~lhs.Get ());
   476 }
   477 template <typename T>
   478 TracedValue<T> operator ! (const TracedValue<T> &lhs) {
   479   TRACED_VALUE_DEBUG ("(!x)");
   480   return TracedValue<T> (!lhs.Get ());
   481 }
   482 
   483 
   484 } // namespace ns3
   485 
   486 #endif /* TRACED_VALUE_H */