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