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