src/core/type-id.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 4318 e753d14618a3
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) 2008 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 #include "type-id.h"
    21 #include "singleton.h"
    22 #include "trace-source-accessor.h"
    23 #include <vector>
    24 #include <sstream>
    25 
    26 /*********************************************************************
    27  *         Helper code
    28  *********************************************************************/
    29 
    30 namespace {
    31 
    32 class IidManager
    33 {
    34 public:
    35   IidManager ();
    36   uint16_t AllocateUid (std::string name);
    37   void SetParent (uint16_t uid, uint16_t parent);
    38   void SetGroupName (uint16_t uid, std::string groupName);
    39   void AddConstructor (uint16_t uid, ns3::Callback<ns3::ObjectBase *> callback);
    40   void HideFromDocumentation (uint16_t uid);
    41   uint16_t GetUid (std::string name) const;
    42   std::string GetName (uint16_t uid) const;
    43   uint16_t GetParent (uint16_t uid) const;
    44   std::string GetGroupName (uint16_t uid) const;
    45   ns3::Callback<ns3::ObjectBase *> GetConstructor (uint16_t uid) const;
    46   bool HasConstructor (uint16_t uid) const;
    47   uint32_t GetRegisteredN (void) const;
    48   uint16_t GetRegistered (uint32_t i) const;
    49   void AddAttribute (uint16_t uid, 
    50                      std::string name,
    51                      std::string help, 
    52                      uint32_t flags,
    53                      ns3::Ptr<const ns3::AttributeValue> initialValue,
    54                      ns3::Ptr<const ns3::AttributeAccessor> spec,
    55                      ns3::Ptr<const ns3::AttributeChecker> checker);
    56   uint32_t GetAttributeN (uint16_t uid) const;
    57   std::string GetAttributeName (uint16_t uid, uint32_t i) const;
    58   std::string GetAttributeHelp (uint16_t uid, uint32_t i) const;
    59   uint32_t GetAttributeFlags (uint16_t uid, uint32_t i) const;
    60   ns3::Ptr<const ns3::AttributeValue> GetAttributeInitialValue (uint16_t uid, uint32_t i) const;
    61   ns3::Ptr<const ns3::AttributeAccessor> GetAttributeAccessor (uint16_t uid, uint32_t i) const;
    62   ns3::Ptr<const ns3::AttributeChecker> GetAttributeChecker (uint16_t uid, uint32_t i) const;
    63   void AddTraceSource (uint16_t uid,
    64                        std::string name, 
    65                        std::string help,
    66                        ns3::Ptr<const ns3::TraceSourceAccessor> accessor);
    67   uint32_t GetTraceSourceN (uint16_t uid) const;
    68   std::string GetTraceSourceName (uint16_t uid, uint32_t i) const;
    69   std::string GetTraceSourceHelp (uint16_t uid, uint32_t i) const;
    70   ns3::Ptr<const ns3::TraceSourceAccessor> GetTraceSourceAccessor (uint16_t uid, uint32_t i) const;
    71   bool MustHideFromDocumentation (uint16_t uid) const;
    72 
    73 private:
    74   bool HasTraceSource (uint16_t uid, std::string name);
    75   bool HasAttribute (uint16_t uid, std::string name);
    76 
    77   struct AttributeInformation {
    78     std::string name;
    79     std::string help;
    80     uint32_t flags;
    81     ns3::Ptr<const ns3::AttributeValue> initialValue;
    82     ns3::Ptr<const ns3::AttributeAccessor> param;
    83     ns3::Ptr<const ns3::AttributeChecker> checker;
    84   };
    85   struct TraceSourceInformation {
    86     std::string name;
    87     std::string help;
    88     ns3::Ptr<const ns3::TraceSourceAccessor> accessor;
    89   };
    90   struct IidInformation {
    91     std::string name;
    92     uint16_t parent;
    93     std::string groupName;
    94     bool hasConstructor;
    95     ns3::Callback<ns3::ObjectBase *> constructor;
    96     bool mustHideFromDocumentation;
    97     std::vector<struct AttributeInformation> attributes;
    98     std::vector<struct TraceSourceInformation> traceSources;
    99   };
   100   typedef std::vector<struct IidInformation>::const_iterator Iterator;
   101 
   102   struct IidManager::IidInformation *LookupInformation (uint16_t uid) const;
   103 
   104   std::vector<struct IidInformation> m_information;
   105 };
   106 
   107 IidManager::IidManager ()
   108 {}
   109 
   110 uint16_t 
   111 IidManager::AllocateUid (std::string name)
   112 {
   113   uint16_t j = 1;
   114   for (Iterator i = m_information.begin (); i != m_information.end (); i++)
   115     {
   116       if (i->name == name)
   117         {
   118           NS_FATAL_ERROR ("Trying to allocate twice the same uid: " << name);
   119           return 0;
   120         }
   121       j++;
   122     }
   123   struct IidInformation information;
   124   information.name = name;
   125   information.parent = 0;
   126   information.groupName = "";
   127   information.hasConstructor = false;
   128   information.mustHideFromDocumentation = false;
   129   m_information.push_back (information);
   130   uint32_t uid = m_information.size ();
   131   NS_ASSERT (uid <= 0xffff);
   132   return uid;
   133 }
   134 
   135 struct IidManager::IidInformation *
   136 IidManager::LookupInformation (uint16_t uid) const
   137 {
   138   NS_ASSERT (uid <= m_information.size () && uid != 0);
   139   return const_cast<struct IidInformation *> (&m_information[uid-1]);
   140 }
   141 
   142 void 
   143 IidManager::SetParent (uint16_t uid, uint16_t parent)
   144 {
   145   NS_ASSERT (parent <= m_information.size ());
   146   struct IidInformation *information = LookupInformation (uid);
   147   information->parent = parent;
   148 }
   149 void 
   150 IidManager::SetGroupName (uint16_t uid, std::string groupName)
   151 {
   152   struct IidInformation *information = LookupInformation (uid);
   153   information->groupName = groupName;
   154 }
   155 void
   156 IidManager::HideFromDocumentation (uint16_t uid)
   157 {
   158   struct IidInformation *information = LookupInformation (uid);
   159   information->mustHideFromDocumentation = true;
   160 }
   161 
   162 void 
   163 IidManager::AddConstructor (uint16_t uid, ns3::Callback<ns3::ObjectBase *> callback)
   164 {
   165   struct IidInformation *information = LookupInformation (uid);
   166   if (information->hasConstructor)
   167     {
   168       NS_FATAL_ERROR (information->name<<" already has a constructor.");
   169     }
   170   information->hasConstructor = true;
   171   information->constructor = callback;
   172 }
   173 
   174 uint16_t 
   175 IidManager::GetUid (std::string name) const
   176 {
   177   uint32_t j = 1;
   178   for (Iterator i = m_information.begin (); i != m_information.end (); i++)
   179     {
   180       if (i->name == name)
   181         {
   182           NS_ASSERT (j <= 0xffff);
   183           return j;
   184         }
   185       j++;
   186     }
   187   return 0;
   188 }
   189 std::string 
   190 IidManager::GetName (uint16_t uid) const
   191 {
   192   struct IidInformation *information = LookupInformation (uid);
   193   return information->name;
   194 }
   195 uint16_t 
   196 IidManager::GetParent (uint16_t uid) const
   197 {
   198   struct IidInformation *information = LookupInformation (uid);
   199   return information->parent;
   200 }
   201 std::string 
   202 IidManager::GetGroupName (uint16_t uid) const
   203 {
   204   struct IidInformation *information = LookupInformation (uid);
   205   return information->groupName;
   206 }
   207 
   208 ns3::Callback<ns3::ObjectBase *> 
   209 IidManager::GetConstructor (uint16_t uid) const
   210 {
   211   struct IidInformation *information = LookupInformation (uid);
   212   if (!information->hasConstructor)
   213     {
   214       NS_FATAL_ERROR ("Requested constructor for "<<information->name<<" but it does not have one.");
   215     }
   216   return information->constructor;
   217 }
   218 
   219 bool 
   220 IidManager::HasConstructor (uint16_t uid) const
   221 {
   222   struct IidInformation *information = LookupInformation (uid);
   223   return information->hasConstructor;
   224 }
   225 
   226 uint32_t 
   227 IidManager::GetRegisteredN (void) const
   228 {
   229   return m_information.size ();
   230 }
   231 uint16_t 
   232 IidManager::GetRegistered (uint32_t i) const
   233 {
   234   return i + 1;
   235 }
   236 
   237 bool
   238 IidManager::HasAttribute (uint16_t uid,
   239                           std::string name)
   240 {
   241   struct IidInformation *information  = LookupInformation (uid);
   242   while (true)
   243     {
   244       for (std::vector<struct AttributeInformation>::const_iterator i = information->attributes.begin ();
   245            i != information->attributes.end (); ++i)
   246         {
   247           if (i->name == name)
   248             {
   249               return true;
   250             }
   251         }
   252       struct IidInformation *parent = LookupInformation (information->parent);
   253       if (parent == information)
   254         {
   255           // top of inheritance tree
   256           return false;
   257         }
   258       // check parent
   259       information = parent;
   260     }
   261   return false;
   262 }
   263 
   264 void 
   265 IidManager::AddAttribute (uint16_t uid, 
   266                           std::string name,
   267                           std::string help, 
   268                           uint32_t flags,
   269                           ns3::Ptr<const ns3::AttributeValue> initialValue,
   270                           ns3::Ptr<const ns3::AttributeAccessor> spec,
   271                           ns3::Ptr<const ns3::AttributeChecker> checker)
   272 {
   273   struct IidInformation *information = LookupInformation (uid);
   274   if (HasAttribute (uid, name))
   275     {
   276       NS_FATAL_ERROR ("Attribute \"" << name << "\" already registered on tid=\"" << 
   277                       information->name << "\"");
   278     }
   279   struct AttributeInformation param;
   280   param.name = name;
   281   param.help = help;
   282   param.flags = flags;
   283   param.initialValue = initialValue;
   284   param.param = spec;
   285   param.checker = checker;
   286   information->attributes.push_back (param);
   287 }
   288 
   289 
   290 uint32_t 
   291 IidManager::GetAttributeN (uint16_t uid) const
   292 {
   293   struct IidInformation *information = LookupInformation (uid);
   294   return information->attributes.size ();
   295 }
   296 std::string 
   297 IidManager::GetAttributeName (uint16_t uid, uint32_t i) const
   298 {
   299   struct IidInformation *information = LookupInformation (uid);
   300   NS_ASSERT (i < information->attributes.size ());
   301   return information->attributes[i].name;
   302 }
   303 std::string 
   304 IidManager::GetAttributeHelp (uint16_t uid, uint32_t i) const
   305 {
   306   struct IidInformation *information = LookupInformation (uid);
   307   NS_ASSERT (i < information->attributes.size ());
   308   return information->attributes[i].help;
   309 }
   310 uint32_t
   311 IidManager::GetAttributeFlags (uint16_t uid, uint32_t i) const
   312 {
   313   struct IidInformation *information = LookupInformation (uid);
   314   NS_ASSERT (i < information->attributes.size ());
   315   return information->attributes[i].flags;
   316 }
   317 ns3::Ptr<const ns3::AttributeValue> 
   318 IidManager::GetAttributeInitialValue (uint16_t uid, uint32_t i) const
   319 {
   320   struct IidInformation *information = LookupInformation (uid);
   321   NS_ASSERT (i < information->attributes.size ());
   322   return information->attributes[i].initialValue;
   323 }
   324 ns3::Ptr<const ns3::AttributeAccessor>
   325 IidManager::GetAttributeAccessor (uint16_t uid, uint32_t i) const
   326 {
   327   struct IidInformation *information = LookupInformation (uid);
   328   NS_ASSERT (i < information->attributes.size ());
   329   return information->attributes[i].param;
   330 }
   331 ns3::Ptr<const ns3::AttributeChecker>
   332 IidManager::GetAttributeChecker (uint16_t uid, uint32_t i) const
   333 {
   334   struct IidInformation *information = LookupInformation (uid);
   335   NS_ASSERT (i < information->attributes.size ());
   336   return information->attributes[i].checker;
   337 }
   338 
   339 bool
   340 IidManager::HasTraceSource (uint16_t uid,
   341                             std::string name)
   342 {
   343   struct IidInformation *information  = LookupInformation (uid);
   344   while (true)
   345     {
   346       for (std::vector<struct TraceSourceInformation>::const_iterator i = information->traceSources.begin ();
   347            i != information->traceSources.end (); ++i)
   348         {
   349           if (i->name == name)
   350             {
   351               return true;
   352             }
   353         }
   354       struct IidInformation *parent = LookupInformation (information->parent);
   355       if (parent == information)
   356         {
   357           // top of inheritance tree
   358           return false;
   359         }
   360       // check parent
   361       information = parent;
   362     }
   363   return false;
   364 }
   365 
   366 void 
   367 IidManager::AddTraceSource (uint16_t uid,
   368                             std::string name, 
   369                             std::string help,
   370                             ns3::Ptr<const ns3::TraceSourceAccessor> accessor)
   371 {
   372   struct IidInformation *information  = LookupInformation (uid);
   373   if (HasTraceSource (uid, name))
   374     {
   375       NS_FATAL_ERROR ("Trace source \"" << name << "\" already registered on tid=\"" << 
   376                       information->name << "\"");
   377     }
   378   struct TraceSourceInformation source;
   379   source.name = name;
   380   source.help = help;
   381   source.accessor = accessor;
   382   information->traceSources.push_back (source);
   383 }
   384 uint32_t 
   385 IidManager::GetTraceSourceN (uint16_t uid) const
   386 {
   387   struct IidInformation *information = LookupInformation (uid);
   388   return information->traceSources.size ();
   389 }
   390 std::string 
   391 IidManager::GetTraceSourceName (uint16_t uid, uint32_t i) const
   392 {
   393   struct IidInformation *information = LookupInformation (uid);
   394   NS_ASSERT (i < information->traceSources.size ());
   395   return information->traceSources[i].name;
   396 }
   397 std::string 
   398 IidManager::GetTraceSourceHelp (uint16_t uid, uint32_t i) const
   399 {
   400   struct IidInformation *information = LookupInformation (uid);
   401   NS_ASSERT (i < information->traceSources.size ());
   402   return information->traceSources[i].help;
   403 }
   404 ns3::Ptr<const ns3::TraceSourceAccessor> 
   405 IidManager::GetTraceSourceAccessor (uint16_t uid, uint32_t i) const
   406 {
   407   struct IidInformation *information = LookupInformation (uid);
   408   NS_ASSERT (i < information->traceSources.size ());
   409   return information->traceSources[i].accessor;
   410 }
   411 bool 
   412 IidManager::MustHideFromDocumentation (uint16_t uid) const
   413 {
   414   struct IidInformation *information = LookupInformation (uid);
   415   return information->mustHideFromDocumentation;
   416 }
   417 
   418 } // anonymous namespace
   419 
   420 namespace ns3 {
   421 
   422 /*********************************************************************
   423  *         The TypeId class
   424  *********************************************************************/
   425 
   426 TypeId::TypeId (const char *name)
   427 {
   428   uint16_t uid = Singleton<IidManager>::Get ()->AllocateUid (name);
   429   NS_ASSERT (uid != 0);
   430   m_tid = uid;
   431 }
   432 
   433 
   434 TypeId::TypeId (uint16_t tid)
   435   : m_tid (tid)
   436 {}
   437 TypeId 
   438 TypeId::LookupByName (std::string name)
   439 {
   440   uint16_t uid = Singleton<IidManager>::Get ()->GetUid (name);
   441   NS_ASSERT_MSG (uid != 0, "Assert in TypeId::LookupByName: " << name << " not found");
   442   return TypeId (uid);
   443 }
   444 bool
   445 TypeId::LookupByNameFailSafe (std::string name, TypeId *tid)
   446 {
   447   uint16_t uid = Singleton<IidManager>::Get ()->GetUid (name);
   448   if (uid == 0)
   449     {
   450       return false;
   451     }
   452   *tid = TypeId (uid);
   453   return true;
   454 }
   455 
   456 bool
   457 TypeId::LookupAttributeByFullName (std::string fullName, struct TypeId::AttributeInfo *info)
   458 {
   459   std::string::size_type pos = fullName.rfind ("::");
   460   if (pos == std::string::npos)
   461     {
   462       return 0;
   463     }
   464   std::string tidName = fullName.substr (0, pos);
   465   std::string paramName = fullName.substr (pos+2, fullName.size () - (pos+2));
   466   TypeId tid;
   467   bool ok = LookupByNameFailSafe (tidName, &tid);
   468   if (!ok)
   469     {
   470       return false;
   471     }
   472   return tid.LookupAttributeByName (paramName, info);
   473 }
   474 uint32_t 
   475 TypeId::GetRegisteredN (void)
   476 {
   477   return Singleton<IidManager>::Get ()->GetRegisteredN ();
   478 }
   479 TypeId 
   480 TypeId::GetRegistered (uint32_t i)
   481 {
   482   return TypeId (Singleton<IidManager>::Get ()->GetRegistered (i));
   483 }
   484 
   485 bool
   486 TypeId::LookupAttributeByName (std::string name, struct TypeId::AttributeInfo *info) const
   487 {
   488   TypeId tid;
   489   TypeId nextTid = *this;
   490   do {
   491     tid = nextTid;
   492     for (uint32_t i = 0; i < tid.GetAttributeN (); i++)
   493       {
   494         std::string paramName = tid.GetAttributeName (i);
   495         if (paramName == name)
   496           {
   497             info->accessor = tid.GetAttributeAccessor (i);
   498             info->flags = tid.GetAttributeFlags (i);
   499             info->initialValue = tid.GetAttributeInitialValue (i);
   500             info->checker = tid.GetAttributeChecker (i);
   501             return true;
   502           }
   503       }
   504     nextTid = tid.GetParent ();
   505   } while (nextTid != tid);
   506   return false;
   507 }
   508 
   509 TypeId 
   510 TypeId::SetParent (TypeId tid)
   511 {
   512   Singleton<IidManager>::Get ()->SetParent (m_tid, tid.m_tid);
   513   return *this;
   514 }
   515 TypeId 
   516 TypeId::SetGroupName (std::string groupName)
   517 {
   518   Singleton<IidManager>::Get ()->SetGroupName (m_tid, groupName);
   519   return *this;
   520 }
   521 TypeId 
   522 TypeId::GetParent (void) const
   523 {
   524   uint16_t parent = Singleton<IidManager>::Get ()->GetParent (m_tid);
   525   return TypeId (parent);
   526 }
   527 bool 
   528 TypeId::HasParent (void) const
   529 {
   530   uint16_t parent = Singleton<IidManager>::Get ()->GetParent (m_tid);
   531   return parent != m_tid;
   532 }
   533 bool 
   534 TypeId::IsChildOf (TypeId other) const
   535 {
   536   TypeId tmp = *this;
   537   while (tmp != other && tmp != tmp.GetParent ())
   538     {
   539       tmp = tmp.GetParent ();
   540     }
   541   return tmp == other && *this != other;
   542 }
   543 std::string 
   544 TypeId::GetGroupName (void) const
   545 {
   546   std::string groupName = Singleton<IidManager>::Get ()->GetGroupName (m_tid);
   547   return groupName;
   548 }
   549 
   550 std::string 
   551 TypeId::GetName (void) const
   552 {
   553   std::string name = Singleton<IidManager>::Get ()->GetName (m_tid);
   554   return name;
   555 }
   556 
   557 bool 
   558 TypeId::HasConstructor (void) const
   559 {
   560   bool hasConstructor = Singleton<IidManager>::Get ()->HasConstructor (m_tid);
   561   return hasConstructor;
   562 }
   563 
   564 void
   565 TypeId::DoAddConstructor (Callback<ObjectBase *> cb)
   566 {
   567   Singleton<IidManager>::Get ()->AddConstructor (m_tid, cb);
   568 }
   569 
   570 TypeId 
   571 TypeId::AddAttribute (std::string name,
   572                       std::string help, 
   573                       const AttributeValue &initialValue,
   574                       Ptr<const AttributeAccessor> param,
   575                       Ptr<const AttributeChecker> checker)
   576 {
   577   Singleton<IidManager>::Get ()->AddAttribute (m_tid, name, help, ATTR_SGC, initialValue.Copy (), param, checker);
   578   return *this;
   579 }
   580 
   581 TypeId 
   582 TypeId::AddAttribute (std::string name,
   583                       std::string help, 
   584                       uint32_t flags,
   585                       const AttributeValue &initialValue,
   586                       Ptr<const AttributeAccessor> param,
   587                       Ptr<const AttributeChecker> checker)
   588 {
   589   Singleton<IidManager>::Get ()->AddAttribute (m_tid, name, help, flags, initialValue.Copy (), param, checker);
   590   return *this;
   591 }
   592 
   593 Callback<ObjectBase *> 
   594 TypeId::GetConstructor (void) const
   595 {
   596   Callback<ObjectBase *>  cb = Singleton<IidManager>::Get ()->GetConstructor (m_tid);
   597   return cb;
   598 }
   599 
   600 bool 
   601 TypeId::MustHideFromDocumentation (void) const
   602 {
   603   bool mustHide = Singleton<IidManager>::Get ()->MustHideFromDocumentation (m_tid);
   604   return mustHide;
   605 }
   606 
   607 uint32_t 
   608 TypeId::GetAttributeN (void) const
   609 {
   610   uint32_t n = Singleton<IidManager>::Get ()->GetAttributeN (m_tid);
   611   return n;
   612 }
   613 std::string 
   614 TypeId::GetAttributeName (uint32_t i) const
   615 {
   616   std::string name = Singleton<IidManager>::Get ()->GetAttributeName (m_tid, i);
   617   return name;
   618 }
   619 std::string 
   620 TypeId::GetAttributeHelp (uint32_t i) const
   621 {
   622   std::string help = Singleton<IidManager>::Get ()->GetAttributeHelp (m_tid, i);
   623   return help;
   624 }
   625 std::string 
   626 TypeId::GetAttributeFullName (uint32_t i) const
   627 {
   628   return GetName () + "::" + GetAttributeName (i);
   629 }
   630 Ptr<const AttributeValue>
   631 TypeId::GetAttributeInitialValue (uint32_t i) const
   632 {
   633   Ptr<const AttributeValue> value = Singleton<IidManager>::Get ()->GetAttributeInitialValue (m_tid, i);
   634   return value;
   635 }
   636 Ptr<const AttributeAccessor>
   637 TypeId::GetAttributeAccessor (uint32_t i) const
   638 {
   639   // Used exclusively by the Object class.
   640   Ptr<const AttributeAccessor> param = Singleton<IidManager>::Get ()->GetAttributeAccessor (m_tid, i);
   641   return param;
   642 }
   643 uint32_t 
   644 TypeId::GetAttributeFlags (uint32_t i) const
   645 {
   646   // Used exclusively by the Object class.
   647   uint32_t flags = Singleton<IidManager>::Get ()->GetAttributeFlags (m_tid, i);
   648   return flags;
   649 }
   650 Ptr<const AttributeChecker>
   651 TypeId::GetAttributeChecker (uint32_t i) const
   652 {
   653   // Used exclusively by the Object class.
   654   Ptr<const AttributeChecker> checker = Singleton<IidManager>::Get ()->GetAttributeChecker (m_tid, i);
   655   return checker;
   656 }
   657 
   658 uint32_t 
   659 TypeId::GetTraceSourceN (void) const
   660 {
   661   return Singleton<IidManager>::Get ()->GetTraceSourceN (m_tid);
   662 }
   663 std::string 
   664 TypeId::GetTraceSourceName (uint32_t i) const
   665 {
   666   return Singleton<IidManager>::Get ()->GetTraceSourceName (m_tid, i);
   667 }
   668 std::string 
   669 TypeId::GetTraceSourceHelp (uint32_t i) const
   670 {
   671   return Singleton<IidManager>::Get ()->GetTraceSourceHelp (m_tid, i);
   672 }
   673 Ptr<const TraceSourceAccessor> 
   674 TypeId::GetTraceSourceAccessor (uint32_t i) const
   675 {
   676   return Singleton<IidManager>::Get ()->GetTraceSourceAccessor (m_tid, i);
   677 }
   678 
   679 TypeId 
   680 TypeId::AddTraceSource (std::string name,
   681                         std::string help,
   682                         Ptr<const TraceSourceAccessor> accessor)
   683 {
   684   Singleton<IidManager>::Get ()->AddTraceSource (m_tid, name, help, accessor);
   685   return *this;
   686 }
   687 
   688 TypeId 
   689 TypeId::HideFromDocumentation (void)
   690 {
   691   Singleton<IidManager>::Get ()->HideFromDocumentation (m_tid);
   692   return *this;
   693 }
   694 
   695 
   696 Ptr<const TraceSourceAccessor> 
   697 TypeId::LookupTraceSourceByName (std::string name) const
   698 {
   699   TypeId tid;
   700   TypeId nextTid = *this;
   701   do {
   702     tid = nextTid;
   703     for (uint32_t i = 0; i < tid.GetTraceSourceN (); i++)
   704       {
   705         std::string srcName = tid.GetTraceSourceName (i);
   706         if (srcName == name)
   707           {
   708             return tid.GetTraceSourceAccessor (i);
   709           }
   710       }
   711     nextTid = tid.GetParent ();
   712   } while (nextTid != tid);
   713   return 0;
   714 }
   715 
   716 uint16_t 
   717 TypeId::GetUid (void) const
   718 {
   719   return m_tid;
   720 }
   721 void 
   722 TypeId::SetUid (uint16_t tid)
   723 {
   724   m_tid = tid;
   725 }
   726 
   727 std::ostream & operator << (std::ostream &os, TypeId tid)
   728 {
   729   os << tid.GetName ();
   730   return os;
   731 }
   732 std::istream & operator >> (std::istream &is, TypeId &tid)
   733 {
   734   std::string tidString;
   735   is >> tidString;
   736   bool ok = TypeId::LookupByNameFailSafe (tidString, &tid);
   737   if (!ok)
   738     {
   739       is.setstate (std::ios_base::badbit);
   740     }
   741   return is;
   742 }
   743 
   744 
   745 ATTRIBUTE_HELPER_CPP (TypeId);
   746 
   747 bool operator < (TypeId a, TypeId b)
   748 {
   749   return a.m_tid < b.m_tid;
   750 }
   751 
   752 } // namespace ns3