A Discrete-Event Network Simulator
API
type-id.cc
Go to the documentation of this file.
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 "log.h" // NS_ASSERT and NS_LOG
21 #include "hash.h"
22 #include "type-id.h"
23 #include "singleton.h"
24 #include "trace-source-accessor.h"
25 
26 #include <map>
27 #include <vector>
28 #include <sstream>
29 #include <iomanip>
30 
38 /*********************************************************************
39  * Helper code
40  *********************************************************************/
41 
42 namespace ns3 {
43 
44 NS_LOG_COMPONENT_DEFINE ("TypeId");
45 
46 // IidManager needs to be in ns3 namespace for NS_ASSERT and NS_LOG
47 // to find g_log
48 
78 class IidManager : public Singleton<IidManager>
79 {
80 public:
86  uint16_t AllocateUid (std::string name);
92  void SetParent (uint16_t uid, uint16_t parent);
98  void SetGroupName (uint16_t uid, std::string groupName);
104  void SetSize (uint16_t uid, std::size_t size);
110  void AddConstructor (uint16_t uid, Callback<ObjectBase *> callback);
115  void HideFromDocumentation (uint16_t uid);
121  uint16_t GetUid (std::string name) const;
127  uint16_t GetUid (TypeId::hash_t hash) const;
133  std::string GetName (uint16_t uid) const;
139  TypeId::hash_t GetHash (uint16_t uid) const;
145  uint16_t GetParent (uint16_t uid) const;
151  std::string GetGroupName (uint16_t uid) const;
157  std::size_t GetSize (uint16_t uid) const;
163  Callback<ObjectBase *> GetConstructor (uint16_t uid) const;
169  bool HasConstructor (uint16_t uid) const;
174  uint32_t GetRegisteredN (void) const;
184  uint16_t GetRegistered (uint32_t i) const;
201  void AddAttribute (uint16_t uid,
202  std::string name,
203  std::string help,
204  uint32_t flags,
205  Ptr<const AttributeValue> initialValue,
209  const std::string &supportMsg = "");
216  void SetAttributeInitialValue(uint16_t uid,
217  uint32_t i,
218  Ptr<const AttributeValue> initialValue);
224  uint32_t GetAttributeN (uint16_t uid) const;
231  struct TypeId::AttributeInformation GetAttribute(uint16_t uid, uint32_t i) const;
247  void AddTraceSource (uint16_t uid,
248  std::string name,
249  std::string help,
251  std::string callback,
253  const std::string &supportMsg = "");
259  uint32_t GetTraceSourceN (uint16_t uid) const;
266  struct TypeId::TraceSourceInformation GetTraceSource(uint16_t uid, uint32_t i) const;
272  bool MustHideFromDocumentation (uint16_t uid) const;
273 
274 private:
281  bool HasTraceSource (uint16_t uid, std::string name);
288  bool HasAttribute (uint16_t uid, std::string name);
294  static TypeId::hash_t Hasher (const std::string name);
295 
297  struct IidInformation {
299  std::string name;
303  uint16_t parent;
305  std::string groupName;
307  std::size_t size;
315  std::vector<struct TypeId::AttributeInformation> attributes;
317  std::vector<struct TypeId::TraceSourceInformation> traceSources;
321  std::string supportMsg;
322  };
324  typedef std::vector<struct IidInformation>::const_iterator Iterator;
325 
331  struct IidManager::IidInformation *LookupInformation (uint16_t uid) const;
332 
334  std::vector<struct IidInformation> m_information;
335 
337  typedef std::map<std::string, uint16_t> namemap_t;
339  namemap_t m_namemap;
340 
342  typedef std::map<TypeId::hash_t, uint16_t> hashmap_t;
344  hashmap_t m_hashmap;
345 
346 
348  enum {
355  HashChainFlag = 0x80000000
356  };
357 };
358 
359 
360 //static
362 IidManager::Hasher (const std::string name)
363 {
364  static ns3::Hasher hasher ( Create<Hash::Function::Murmur3> () );
365  return hasher.clear ().GetHash32 (name);
366 }
367 
368 #define IID "IidManager"
369 #define IIDL IID << ": "
370 
371 uint16_t
372 IidManager::AllocateUid (std::string name)
373 {
374  NS_LOG_FUNCTION (IID << name);
375  // Type names are definitive: equal names are equal types
376  NS_ASSERT_MSG (m_namemap.count (name) == 0,
377  "Trying to allocate twice the same uid: " << name);
378 
379  TypeId::hash_t hash = Hasher (name) & (~HashChainFlag);
380  if (m_hashmap.count (hash) == 1) {
381  NS_LOG_ERROR ("Hash chaining TypeId for '" << name << "'. "
382  << "This is not a bug, but is extremely unlikely. "
383  << "Please contact the ns3 developers.");
384  // ns3 developer contacted about this message:
385  // You have four options (in order of difficulty):
386  // 1. Let it ride, and play the odds that a third collision
387  // never appears.
388  // 2. Change the name of the new (or old) tag, even trivially, to
389  // remove the collision.
390  // 3. Switch to 64-bit hashes.
391  // 4. Implement 2-bit (or higher) chaining.
392  //
393  // Oh, by the way, I owe you a beer, since I bet Mathieu that
394  // this would never happen.. -- Peter Barnes, LLNL
395 
396  NS_ASSERT_MSG (m_hashmap.count (hash | HashChainFlag) == 0,
397  "Triplicate hash detected while chaining TypeId for '"
398  << name
399  << "'. Please contact the ns3 developers for assistance.");
400  // ns3 developer contacted about this message:
401  // You have three options: #2-4 above.
402  //
403  // Oh, by the way, I have no idea how this crazy hashing idea got
404  // into ns3. -- Peter Barnes, LLNL
405 
406  // Alphabetize the two types, so it's deterministic
407  struct IidInformation * hinfo = LookupInformation (GetUid (hash));
408  if (name > hinfo->name)
409  { // new type gets chained
410  NS_LOG_LOGIC (IIDL << "New TypeId '" << name << "' getting chained.");
411  hash = hash | HashChainFlag;
412  }
413  else
414  { // chain old type
415  NS_LOG_LOGIC (IIDL << "Old TypeId '" << hinfo->name << "' getting chained.");
416  uint32_t oldUid = GetUid (hinfo->hash);
417  m_hashmap.erase (m_hashmap.find (hinfo->hash));
418  hinfo->hash = hash | HashChainFlag;
419  m_hashmap.insert (std::make_pair (hinfo->hash, oldUid));
420  // leave new hash unchained
421  }
422  }
423 
424  struct IidInformation information;
425  information.name = name;
426  information.hash = hash;
427  information.parent = 0;
428  information.groupName = "";
429  information.size = (std::size_t)(-1);
430  information.hasConstructor = false;
431  information.mustHideFromDocumentation = false;
432  m_information.push_back (information);
433  uint32_t uid = m_information.size ();
434  NS_ASSERT (uid <= 0xffff);
435 
436  // Add to both maps:
437  m_namemap.insert (std::make_pair (name, uid));
438  m_hashmap.insert (std::make_pair (hash, uid));
439  NS_LOG_LOGIC (IIDL << uid);
440  return uid;
441 }
442 
444 IidManager::LookupInformation (uint16_t uid) const
445 {
446  NS_LOG_FUNCTION (IID << uid);
447  NS_ASSERT (uid <= m_information.size () && uid != 0);
448  NS_LOG_LOGIC (IIDL << m_information[uid-1].name);
449  return const_cast<struct IidInformation *> (&m_information[uid-1]);
450 }
451 
452 void
453 IidManager::SetParent (uint16_t uid, uint16_t parent)
454 {
455  NS_LOG_FUNCTION (IID << uid << parent);
456  NS_ASSERT (parent <= m_information.size ());
457  struct IidInformation *information = LookupInformation (uid);
458  information->parent = parent;
459 }
460 void
461 IidManager::SetGroupName (uint16_t uid, std::string groupName)
462 {
463  NS_LOG_FUNCTION (IID << uid << groupName);
464  struct IidInformation *information = LookupInformation (uid);
465  information->groupName = groupName;
466 }
467 void
468 IidManager::SetSize (uint16_t uid, std::size_t size)
469 {
470  NS_LOG_FUNCTION (IID << uid << size);
471  struct IidInformation *information = LookupInformation (uid);
472  information->size = size;
473 }
474 void
476 {
477  NS_LOG_FUNCTION (IID << uid);
478  struct IidInformation *information = LookupInformation (uid);
479  information->mustHideFromDocumentation = true;
480 }
481 
482 void
484 {
485  NS_LOG_FUNCTION (IID << uid << &callback);
486  struct IidInformation *information = LookupInformation (uid);
487  if (information->hasConstructor)
488  {
489  NS_FATAL_ERROR (information->name<<" already has a constructor.");
490  }
491  information->hasConstructor = true;
492  information->constructor = callback;
493 }
494 
495 uint16_t
496 IidManager::GetUid (std::string name) const
497 {
498  NS_LOG_FUNCTION (IID << name);
499  uint16_t uid = 0;
500  namemap_t::const_iterator it = m_namemap.find (name);
501  if (it != m_namemap.end ())
502  {
503  uid = it->second;
504  }
505  NS_LOG_LOGIC (IIDL << uid);
506  return uid;
507 }
508 uint16_t
510 {
511  NS_LOG_FUNCTION (IID << hash);
512  hashmap_t::const_iterator it = m_hashmap.find (hash);
513  uint16_t uid = 0;
514  if (it != m_hashmap.end ())
515  {
516  uid = it->second;
517  }
518  NS_LOG_LOGIC (IIDL << uid);
519  return uid;
520 }
521 std::string
522 IidManager::GetName (uint16_t uid) const
523 {
524  NS_LOG_FUNCTION (IID << uid);
525  struct IidInformation *information = LookupInformation (uid);
526  NS_LOG_LOGIC (IIDL << information->name);
527  return information->name;
528 }
530 IidManager::GetHash (uint16_t uid) const
531 {
532  NS_LOG_FUNCTION (IID << uid);
533  struct IidInformation *information = LookupInformation (uid);
534  TypeId::hash_t hash = information->hash;
535  NS_LOG_LOGIC (IIDL << hash);
536  return hash;
537 }
538 uint16_t
539 IidManager::GetParent (uint16_t uid) const
540 {
541  NS_LOG_FUNCTION (IID << uid);
542  struct IidInformation *information = LookupInformation (uid);
543  uint16_t pid = information->parent;
544  NS_LOG_LOGIC (IIDL << pid);
545  return pid;
546 }
547 std::string
548 IidManager::GetGroupName (uint16_t uid) const
549 {
550  NS_LOG_FUNCTION (IID << uid);
551  struct IidInformation *information = LookupInformation (uid);
552  NS_LOG_LOGIC (IIDL << information->groupName);
553  return information->groupName;
554 }
555 std::size_t
556 IidManager::GetSize (uint16_t uid) const
557 {
558  NS_LOG_FUNCTION (IID << uid);
559  struct IidInformation *information = LookupInformation (uid);
560  std::size_t size = information->size;
561  NS_LOG_LOGIC (IIDL << size);
562  return size;
563 }
564 
566 IidManager::GetConstructor (uint16_t uid) const
567 {
568  NS_LOG_FUNCTION (IID << uid);
569  struct IidInformation *information = LookupInformation (uid);
570  if (!information->hasConstructor)
571  {
572  NS_FATAL_ERROR ("Requested constructor for "<<information->name<<" but it does not have one.");
573  }
574  return information->constructor;
575 }
576 
577 bool
578 IidManager::HasConstructor (uint16_t uid) const
579 {
580  NS_LOG_FUNCTION (IID << uid);
581  struct IidInformation *information = LookupInformation (uid);
582  bool hasC = information->hasConstructor;
583  NS_LOG_LOGIC (IIDL << hasC);
584  return hasC;
585 }
586 
587 uint32_t
589 {
590  NS_LOG_FUNCTION (IID << m_information.size ());
591  return m_information.size ();
592 }
593 uint16_t
594 IidManager::GetRegistered (uint32_t i) const
595 {
596  NS_LOG_FUNCTION (IID << i);
597  return i + 1;
598 }
599 
600 bool
602  std::string name)
603 {
604  NS_LOG_FUNCTION (IID << uid << name);
605  struct IidInformation *information = LookupInformation (uid);
606  while (true)
607  {
608  for (std::vector<struct TypeId::AttributeInformation>::const_iterator i = information->attributes.begin ();
609  i != information->attributes.end (); ++i)
610  {
611  if (i->name == name)
612  {
613  NS_LOG_LOGIC (IIDL << true);
614  return true;
615  }
616  }
617  struct IidInformation *parent = LookupInformation (information->parent);
618  if (parent == information)
619  {
620  // top of inheritance tree
621  NS_LOG_LOGIC (IIDL << false);
622  return false;
623  }
624  // check parent
625  information = parent;
626  }
627  NS_LOG_LOGIC (IIDL << false);
628  return false;
629 }
630 
631 void
633  std::string name,
634  std::string help,
635  uint32_t flags,
636  Ptr<const AttributeValue> initialValue,
639  TypeId::SupportLevel supportLevel,
640  const std::string &supportMsg)
641 {
642  NS_LOG_FUNCTION (IID << uid << name << help << flags
643  << initialValue << accessor << checker
644  << supportLevel << supportMsg);
645  struct IidInformation *information = LookupInformation (uid);
646  if (name.find (' ') != std::string::npos)
647  {
648  NS_FATAL_ERROR ("Attribute name \"" << name << "\" may not contain spaces ' ', "
649  << "encountered when registering TypeId \""
650  << information->name << "\"");
651  }
652  if (HasAttribute (uid, name))
653  {
654  NS_FATAL_ERROR ("Attribute \"" << name << "\" already registered on tid=\"" <<
655  information->name << "\"");
656  }
657  struct TypeId::AttributeInformation info;
658  info.name = name;
659  info.help = help;
660  info.flags = flags;
661  info.initialValue = initialValue;
662  info.originalInitialValue = initialValue;
663  info.accessor = accessor;
664  info.checker = checker;
665  info.supportLevel = supportLevel;
666  info.supportMsg = supportMsg;
667  information->attributes.push_back (info);
668  NS_LOG_LOGIC (IIDL << information->attributes.size () - 1);
669 }
670 void
672  uint32_t i,
673  Ptr<const AttributeValue> initialValue)
674 {
675  NS_LOG_FUNCTION (IID << uid << i << initialValue);
676  struct IidInformation *information = LookupInformation (uid);
677  NS_ASSERT (i < information->attributes.size ());
678  information->attributes[i].initialValue = initialValue;
679 }
680 
681 
682 
683 uint32_t
684 IidManager::GetAttributeN (uint16_t uid) const
685 {
686  NS_LOG_FUNCTION (IID << uid);
687  struct IidInformation *information = LookupInformation (uid);
688  uint32_t size = information->attributes.size ();
689  NS_LOG_LOGIC (IIDL << size);
690  return size;
691 }
693 IidManager::GetAttribute(uint16_t uid, uint32_t i) const
694 {
695  NS_LOG_FUNCTION (IID << uid << i);
696  struct IidInformation *information = LookupInformation (uid);
697  NS_ASSERT (i < information->attributes.size ());
698  NS_LOG_LOGIC (IIDL << information->name);
699  return information->attributes[i];
700 }
701 
702 bool
704  std::string name)
705 {
706  NS_LOG_FUNCTION (IID << uid << name);
707  struct IidInformation *information = LookupInformation (uid);
708  while (true)
709  {
710  for (std::vector<struct TypeId::TraceSourceInformation>::const_iterator i = information->traceSources.begin ();
711  i != information->traceSources.end (); ++i)
712  {
713  if (i->name == name)
714  {
715  NS_LOG_LOGIC (IIDL << true);
716  return true ;
717  }
718  }
719  struct IidInformation *parent = LookupInformation (information->parent);
720  if (parent == information)
721  {
722  // top of inheritance tree
723  NS_LOG_LOGIC (IIDL << false);
724  return false;
725  }
726  // check parent
727  information = parent;
728  }
729  NS_LOG_LOGIC (IIDL << false);
730  return false;
731 }
732 
733 void
735  std::string name,
736  std::string help,
738  std::string callback,
739  TypeId::SupportLevel supportLevel,
740  const std::string &supportMsg)
741 {
742  NS_LOG_FUNCTION (IID << uid << name << help
743  << accessor << callback
744  << supportLevel << supportMsg);
745  struct IidInformation *information = LookupInformation (uid);
746  if (HasTraceSource (uid, name))
747  {
748  NS_FATAL_ERROR ("Trace source \"" << name << "\" already registered on tid=\"" <<
749  information->name << "\"");
750  }
751  struct TypeId::TraceSourceInformation source;
752  source.name = name;
753  source.help = help;
754  source.accessor = accessor;
755  source.callback = callback;
756  source.supportLevel = supportLevel;
757  source.supportMsg = supportMsg;
758  information->traceSources.push_back (source);
759  NS_LOG_LOGIC (IIDL << information->traceSources.size () - 1);
760 }
761 uint32_t
762 IidManager::GetTraceSourceN (uint16_t uid) const
763 {
764  NS_LOG_FUNCTION (IID << uid);
765  struct IidInformation *information = LookupInformation (uid);
766  uint32_t size = information->traceSources.size ();
767  NS_LOG_LOGIC (IIDL << size);
768  return size;
769 }
771 IidManager::GetTraceSource(uint16_t uid, uint32_t i) const
772 {
773  NS_LOG_FUNCTION (IID << uid << i);
774  struct IidInformation *information = LookupInformation (uid);
775  NS_ASSERT (i < information->traceSources.size ());
776  NS_LOG_LOGIC (IIDL << information->name);
777  return information->traceSources[i];
778 }
779 bool
781 {
782  NS_LOG_FUNCTION (IID << uid);
783  struct IidInformation *information = LookupInformation (uid);
784  bool hide = information->mustHideFromDocumentation;
785  NS_LOG_LOGIC (IIDL << hide);
786  return hide;
787 }
788 
789 } // namespace ns3
790 
791 namespace ns3 {
792 
793 /*********************************************************************
794  * The TypeId class
795  *********************************************************************/
796 
797 TypeId::TypeId (const char *name)
798 {
799  NS_LOG_FUNCTION (this << name);
800  uint16_t uid = IidManager::Get ()->AllocateUid (name);
801  NS_LOG_LOGIC (uid);
802  NS_ASSERT (uid != 0);
803  m_tid = uid;
804 }
805 
806 
807 TypeId::TypeId (uint16_t tid)
808  : m_tid (tid)
809 {
810  NS_LOG_FUNCTION (this << tid);
811 }
812 TypeId
813 TypeId::LookupByName (std::string name)
814 {
815  NS_LOG_FUNCTION (name);
816  uint16_t uid = IidManager::Get ()->GetUid (name);
817  NS_ASSERT_MSG (uid != 0, "Assert in TypeId::LookupByName: " << name << " not found");
818  return TypeId (uid);
819 }
820 bool
821 TypeId::LookupByNameFailSafe (std::string name, TypeId *tid)
822 {
823  NS_LOG_FUNCTION (name << tid->GetUid ());
824  uint16_t uid = IidManager::Get ()->GetUid (name);
825  if (uid == 0)
826  {
827  return false;
828  }
829  *tid = TypeId (uid);
830  return true;
831 }
832 TypeId
834 {
835  uint16_t uid = IidManager::Get ()->GetUid (hash);
836  NS_ASSERT_MSG (uid != 0, "Assert in TypeId::LookupByHash: 0x"
837  << std::hex << hash << std::dec << " not found");
838  return TypeId (uid);
839 }
840 bool
842 {
843  uint16_t uid = IidManager::Get ()->GetUid (hash);
844  if (uid == 0)
845  {
846  return false;
847  }
848  *tid = TypeId (uid);
849  return true;
850 }
851 
852 uint32_t
854 {
856  return IidManager::Get ()->GetRegisteredN ();
857 }
858 TypeId
860 {
861  NS_LOG_FUNCTION (i);
862  return TypeId (IidManager::Get ()->GetRegistered (i));
863 }
864 
865 bool
866 TypeId::LookupAttributeByName (std::string name, struct TypeId::AttributeInformation *info) const
867 {
868  NS_LOG_FUNCTION (this << name << info);
869  TypeId tid;
870  TypeId nextTid = *this;
871  do {
872  tid = nextTid;
873  for (uint32_t i = 0; i < tid.GetAttributeN (); i++)
874  {
875  struct TypeId::AttributeInformation tmp = tid.GetAttribute(i);
876  if (tmp.name == name)
877  {
878  if (tmp.supportLevel == TypeId::SUPPORTED)
879  {
880  *info = tmp;
881  return true;
882  }
883  else if (tmp.supportLevel == TypeId::DEPRECATED)
884  {
885  std::cerr << "Attribute '" << name << "' is deprecated: "
886  << tmp.supportMsg << std::endl;
887  *info = tmp;
888  return true;
889  }
890  else if (tmp.supportLevel == TypeId::OBSOLETE)
891  {
892  NS_FATAL_ERROR ("Attribute '" << name
893  << "' is obsolete, with no fallback: "
894  << tmp.supportMsg);
895  }
896  }
897  }
898  nextTid = tid.GetParent ();
899  } while (nextTid != tid);
900  return false;
901 }
902 
903 TypeId
905 {
906  NS_LOG_FUNCTION (this << tid.GetUid ());
908  return *this;
909 }
910 TypeId
911 TypeId::SetGroupName (std::string groupName)
912 {
913  NS_LOG_FUNCTION (this << groupName);
914  IidManager::Get ()->SetGroupName (m_tid, groupName);
915  return *this;
916 }
917 TypeId
918 TypeId::SetSize (std::size_t size)
919 {
920  NS_LOG_FUNCTION (this << size);
921  IidManager::Get ()->SetSize (m_tid, size);
922  return *this;
923 }
924 TypeId
925 TypeId::GetParent (void) const
926 {
927  NS_LOG_FUNCTION (this);
928  uint16_t parent = IidManager::Get ()->GetParent (m_tid);
929  return TypeId (parent);
930 }
931 bool
932 TypeId::HasParent (void) const
933 {
934  NS_LOG_FUNCTION (this);
935  uint16_t parent = IidManager::Get ()->GetParent (m_tid);
936  return parent != m_tid;
937 }
938 bool
940 {
941  NS_LOG_FUNCTION (this << other.GetUid ());
942  TypeId tmp = *this;
943  while (tmp != other && tmp != tmp.GetParent ())
944  {
945  tmp = tmp.GetParent ();
946  }
947  return tmp == other && *this != other;
948 }
949 std::string
951 {
952  NS_LOG_FUNCTION (this);
953  std::string groupName = IidManager::Get ()->GetGroupName (m_tid);
954  return groupName;
955 }
956 
957 std::string
958 TypeId::GetName (void) const
959 {
960  NS_LOG_FUNCTION (this);
961  std::string name = IidManager::Get ()->GetName (m_tid);
962  return name;
963 }
964 
966 TypeId::GetHash (void) const
967 {
968  hash_t hash = IidManager::Get ()->GetHash (m_tid);
969  return hash;
970 }
971 std::size_t
972 TypeId::GetSize (void) const
973 {
974  NS_LOG_FUNCTION (this);
975  std::size_t size = IidManager::Get ()->GetSize (m_tid);
976  return size;
977 }
978 
979 bool
981 {
982  NS_LOG_FUNCTION (this);
983  bool hasConstructor = IidManager::Get ()->HasConstructor (m_tid);
984  return hasConstructor;
985 }
986 
987 void
989 {
990  NS_LOG_FUNCTION (this << &cb);
992 }
993 
994 TypeId
995 TypeId::AddAttribute (std::string name,
996  std::string help,
997  const AttributeValue &initialValue,
1000  SupportLevel supportLevel,
1001  const std::string &supportMsg)
1002 {
1003  NS_LOG_FUNCTION (this << name << help
1004  << &initialValue << accessor << checker
1005  << supportLevel << supportMsg);
1006  IidManager::Get ()->AddAttribute (m_tid, name, help, ATTR_SGC,
1007  initialValue.Copy (), accessor, checker,
1008  supportLevel, supportMsg);
1009  return *this;
1010 }
1011 
1012 TypeId
1013 TypeId::AddAttribute (std::string name,
1014  std::string help,
1015  uint32_t flags,
1016  const AttributeValue &initialValue,
1019  SupportLevel supportLevel,
1020  const std::string &supportMsg)
1021 {
1022  NS_LOG_FUNCTION (this << name << help << flags
1023  << &initialValue << accessor << checker
1024  << supportLevel << supportMsg);
1025  IidManager::Get ()->AddAttribute (m_tid, name, help, flags,
1026  initialValue.Copy (), accessor, checker,
1027  supportLevel, supportMsg);
1028  return *this;
1029 }
1030 
1031 bool
1033  Ptr<const AttributeValue> initialValue)
1034 {
1035  NS_LOG_FUNCTION (this << i << initialValue);
1036  IidManager::Get ()->SetAttributeInitialValue (m_tid, i, initialValue);
1037  return true;
1038 }
1039 
1040 
1043 {
1044  NS_LOG_FUNCTION (this);
1046  return cb;
1047 }
1048 
1049 bool
1051 {
1052  NS_LOG_FUNCTION (this);
1053  bool mustHide = IidManager::Get ()->MustHideFromDocumentation (m_tid);
1054  return mustHide;
1055 }
1056 
1057 uint32_t
1059 {
1060  NS_LOG_FUNCTION (this);
1061  uint32_t n = IidManager::Get ()->GetAttributeN (m_tid);
1062  return n;
1063 }
1065 TypeId::GetAttribute(uint32_t i) const
1066 {
1067  NS_LOG_FUNCTION (this << i);
1068  return IidManager::Get ()->GetAttribute(m_tid, i);
1069 }
1070 std::string
1072 {
1073  NS_LOG_FUNCTION (this << i);
1074  struct TypeId::AttributeInformation info = GetAttribute(i);
1075  return GetName () + "::" + info.name;
1076 }
1077 
1078 uint32_t
1080 {
1081  NS_LOG_FUNCTION (this);
1082  return IidManager::Get ()->GetTraceSourceN (m_tid);
1083 }
1085 TypeId::GetTraceSource(uint32_t i) const
1086 {
1087  NS_LOG_FUNCTION (this << i);
1088  return IidManager::Get ()->GetTraceSource(m_tid, i);
1089 }
1090 
1091 TypeId
1092 TypeId::AddTraceSource (std::string name,
1093  std::string help,
1095 {
1096  return AddTraceSource (name, help, accessor, "(not yet documented)");
1097 }
1098 
1099 TypeId
1100 TypeId::AddTraceSource (std::string name,
1101  std::string help,
1103  std::string callback,
1104  SupportLevel supportLevel,
1105  const std::string &supportMsg)
1106 {
1107  NS_LOG_FUNCTION (this << name << help
1108  << accessor << callback
1109  << supportLevel << supportMsg);
1110  IidManager::Get ()->AddTraceSource (m_tid, name, help,
1111  accessor, callback,
1112  supportLevel, supportMsg);
1113  return *this;
1114 }
1115 
1116 TypeId
1118 {
1119  NS_LOG_FUNCTION (this);
1121  return *this;
1122 }
1123 
1126  struct TraceSourceInformation *info) const
1127 {
1128  NS_LOG_FUNCTION (this << name);
1129  TypeId tid;
1130  TypeId nextTid = *this;
1131  struct TypeId::TraceSourceInformation tmp;
1132  do {
1133  tid = nextTid;
1134  for (uint32_t i = 0; i < tid.GetTraceSourceN (); i++)
1135  {
1136  tmp = tid.GetTraceSource (i);
1137  if (tmp.name == name)
1138  {
1139  if (tmp.supportLevel == TypeId::SUPPORTED)
1140  {
1141  *info = tmp;
1142  return tmp.accessor;
1143  }
1144  else if (tmp.supportLevel == TypeId::DEPRECATED)
1145  {
1146  std::cerr << "TraceSource '" << name << "' is deprecated: "
1147  << tmp.supportMsg << std::endl;
1148  *info = tmp;
1149  return tmp.accessor;
1150  }
1151  else if (tmp.supportLevel == TypeId::OBSOLETE)
1152  {
1153  NS_FATAL_ERROR ("TraceSource '" << name
1154  << "' is obsolete, with no fallback: "
1155  << tmp.supportMsg);
1156  }
1157  }
1158  }
1159  nextTid = tid.GetParent ();
1160  } while (nextTid != tid);
1161  return 0;
1162 }
1163 
1165 TypeId::LookupTraceSourceByName (std::string name) const
1166 {
1167  struct TraceSourceInformation info;
1168  return LookupTraceSourceByName (name, &info);
1169 }
1170 
1171 uint16_t
1172 TypeId::GetUid (void) const
1173 {
1174  NS_LOG_FUNCTION (this);
1175  return m_tid;
1176 }
1177 void
1178 TypeId::SetUid (uint16_t uid)
1179 {
1180  NS_LOG_FUNCTION (this << uid);
1181  m_tid = uid;
1182 }
1183 
1184 std::ostream & operator << (std::ostream &os, TypeId tid)
1185 {
1186  os << tid.GetName ();
1187  return os;
1188 }
1189 std::istream & operator >> (std::istream &is, TypeId &tid)
1190 {
1191  std::string tidString;
1192  is >> tidString;
1193  bool ok = TypeId::LookupByNameFailSafe (tidString, &tid);
1194  if (!ok)
1195  {
1196  is.setstate (std::ios_base::badbit);
1197  }
1198  return is;
1199 }
1200 
1201 
1203 
1205 {
1206  return a.m_tid < b.m_tid;
1207 }
1208 
1209 } // namespace ns3
std::vector< struct TypeId::TraceSourceInformation > traceSources
The container of TraceSources.
Definition: type-id.cc:317
#define IIDL
Definition: type-id.cc:369
TypeId::SupportLevel supportLevel
Support level/deprecation.
Definition: type-id.h:107
uint32_t GetAttributeN(void) const
Get the number of attributes.
Definition: type-id.cc:1058
static TypeId::hash_t Hasher(const std::string name)
Hashing function.
Definition: type-id.cc:362
uint32_t hash_t
Type of hash values.
Definition: type-id.h:113
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:48
Attribute or trace source is currently used.
Definition: type-id.h:71
bool HasAttribute(uint16_t uid, std::string name)
Check if a type id has a given Attribute.
Definition: type-id.cc:601
Callback< ObjectBase * > GetConstructor(uint16_t uid) const
Get the constructor Callback of a type id.
Definition: type-id.cc:566
TypeId AddAttribute(std::string name, std::string help, const AttributeValue &initialValue, Ptr< const AttributeAccessor > accessor, Ptr< const AttributeChecker > checker, SupportLevel supportLevel=SUPPORTED, const std::string &supportMsg="")
Definition: type-id.cc:995
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
ns3::Singleton declaration and template implementation.
std::string help
Trace help string.
Definition: type-id.h:101
Callback template class.
Definition: callback.h:1176
bool HasTraceSource(uint16_t uid, std::string name)
Check if a type id has a given TraceSource.
Definition: type-id.cc:703
TypeId SetParent(void)
Set the parent TypeId.
Definition: type-id.h:645
std::vector< struct IidInformation > m_information
The container of all type id records.
Definition: type-id.cc:334
std::string GetGroupName(uint16_t uid) const
Get the group name of a type id.
Definition: type-id.cc:548
static TypeId LookupByHash(hash_t hash)
Get a TypeId by hash.
Definition: type-id.cc:833
hashmap_t m_hashmap
The by-hash index.
Definition: type-id.cc:344
namemap_t m_namemap
The by-name index.
Definition: type-id.cc:339
std::map< TypeId::hash_t, uint16_t > hashmap_t
Type of the by-hash index.
Definition: type-id.cc:342
Hold a value for an Attribute.
Definition: attribute.h:68
struct TypeId::AttributeInformation GetAttribute(uint16_t uid, uint32_t i) const
Get Attribute information by index.
Definition: type-id.cc:693
#define ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type.
Ptr< const AttributeValue > originalInitialValue
Default initial value.
Definition: type-id.h:84
Ptr< const TraceSourceAccessor > accessor
Trace accessor.
Definition: type-id.h:105
void SetGroupName(uint16_t uid, std::string groupName)
Set the group name of a type id.
Definition: type-id.cc:461
TraceSource implementation.
Definition: type-id.h:97
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
std::string groupName
The group name.
Definition: type-id.cc:305
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
TypeId GetParent(void) const
Get the parent of this TypeId.
Definition: type-id.cc:925
std::string supportMsg
Support message.
Definition: type-id.h:109
bool mustHideFromDocumentation
true if this type should be omitted from documentation.
Definition: type-id.cc:313
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
static bool LookupByNameFailSafe(std::string name, TypeId *tid)
Get a TypeId by name.
Definition: type-id.cc:821
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Attribute or trace source is not used anymore; simulation fails.
Definition: type-id.h:73
uint32_t GetRegisteredN(void) const
Get the total number of type ids.
Definition: type-id.cc:588
ns3::Hasher, ns3::Hash32() and ns3::Hash64() function declarations.
static IidManager * Get(void)
Get a pointer to the singleton instance.
TypeId::hash_t hash
The type id hash value.
Definition: type-id.cc:301
Callback< ObjectBase * > GetConstructor(void) const
Get the constructor callback.
Definition: type-id.cc:1042
static bool LookupByHashFailSafe(hash_t hash, TypeId *tid)
Get a TypeId by hash.
Definition: type-id.cc:841
bool HasConstructor(void) const
Check if this TypeId has a constructor.
Definition: type-id.cc:980
bool MustHideFromDocumentation(void) const
Check if this TypeId should not be listed in documentation.
Definition: type-id.cc:1050
A template singleton.
Definition: singleton.h:63
TypeId information manager.
Definition: type-id.cc:78
Ptr< const AttributeAccessor > accessor
Accessor object.
Definition: type-id.h:88
bool hasConstructor
true if a constructor Callback has been registered.
Definition: type-id.cc:309
static uint32_t GetRegisteredN(void)
Get the number of registered TypeIds.
Definition: type-id.cc:853
Ptr< const TraceSourceAccessor > LookupTraceSourceByName(std::string name) const
Find a TraceSource by name.
Definition: type-id.cc:1165
uint32_t GetAttributeN(uint16_t uid) const
Get the number of attributes.
Definition: type-id.cc:684
Ptr< const AttributeValue > initialValue
Configured initial value.
Definition: type-id.h:86
uint16_t m_tid
The TypeId value.
Definition: type-id.h:575
uint16_t parent
The parent type id.
Definition: type-id.cc:303
The information record about a single type id.
Definition: type-id.cc:297
TypeId()
Default constructor.
Definition: type-id.h:614
bool HasParent(void) const
Check if this TypeId has a parent.
Definition: type-id.cc:932
uint32_t GetHash32(const char *buffer, const size_t size)
Compute 32-bit hash of a byte buffer.
Definition: hash.h:239
uint32_t GetTraceSourceN(void) const
Get the number of Trace sources.
Definition: type-id.cc:1079
static TypeId GetRegistered(uint32_t i)
Get a TypeId by index.
Definition: type-id.cc:859
uint32_t GetTraceSourceN(uint16_t uid) const
Get the number of Trace sources.
Definition: type-id.cc:762
void SetAttributeInitialValue(uint16_t uid, uint32_t i, Ptr< const AttributeValue > initialValue)
Set the initial value of an Attribute.
Definition: type-id.cc:671
Hash chaining flag.
Definition: type-id.cc:355
std::vector< struct TypeId::AttributeInformation > attributes
The container of Attributes.
Definition: type-id.cc:315
Attribute or trace source is deprecated; user is warned.
Definition: type-id.h:72
Attribute implementation.
Definition: type-id.h:76
struct TypeId::TraceSourceInformation GetTraceSource(uint16_t uid, uint32_t i) const
Get the trace source by index.
Definition: type-id.cc:771
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
void HideFromDocumentation(uint16_t uid)
Mark this type id to be excluded from documentation.
Definition: type-id.cc:475
bool MustHideFromDocumentation(uint16_t uid) const
Check if this TypeId should not be listed in documentation.
Definition: type-id.cc:780
TypeId::SupportLevel supportLevel
Support level/deprecation.
Definition: type-id.h:92
uint32_t flags
AttributeFlags value.
Definition: type-id.h:82
bool operator<(const int64x64_t &lhs, const int64x64_t &rhs)
Less than operator.
Definition: int64x64-128.h:356
std::size_t size
The size of the object represented by this type id.
Definition: type-id.cc:307
TypeId SetGroupName(std::string groupName)
Set the group name.
Definition: type-id.cc:911
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
std::string name
The type id name.
Definition: type-id.cc:299
Ptr< const AttributeChecker > checker
Checker object.
Definition: type-id.h:90
virtual Ptr< AttributeValue > Copy(void) const =0
std::string supportMsg
Support message.
Definition: type-id.h:94
std::string supportMsg
Support message.
Definition: type-id.cc:321
Hasher & clear(void)
Restore initial state.
Definition: hash.cc:48
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::string name
Attribute name.
Definition: type-id.h:78
bool LookupAttributeByName(std::string name, struct AttributeInformation *info) const
Find an Attribute by name, retrieving the associated AttributeInformation.
Definition: type-id.cc:866
ns3::TraceSourceAccessor and ns3::MakeTraceSourceAccessor declarations.
ns3::TypeId declaration; inline and template implementations.
std::string name
Trace name.
Definition: type-id.h:99
std::map< std::string, uint16_t > namemap_t
Type of the by-name index.
Definition: type-id.cc:337
uint16_t GetRegistered(uint32_t i) const
Get a type id by index.
Definition: type-id.cc:594
std::string GetName(void) const
Get the name.
Definition: type-id.cc:958
The attribute can be read, and written at any time.
Definition: type-id.h:66
uint16_t GetUid(void) const
Get the internal id of this TypeId.
Definition: type-id.cc:1172
void DoAddConstructor(Callback< ObjectBase * > callback)
Implementation for AddConstructor().
Definition: type-id.cc:988
hash_t GetHash(void) const
Get the hash.
Definition: type-id.cc:966
void SetUid(uint16_t uid)
Set the internal id of this TypeId.
Definition: type-id.cc:1178
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:90
bool SetAttributeInitialValue(uint32_t i, Ptr< const AttributeValue > initialValue)
Set the initial value of an Attribute.
Definition: type-id.cc:1032
NS_DEPRECATED TypeId AddTraceSource(std::string name, std::string help, Ptr< const TraceSourceAccessor > accessor)
Record a new TraceSource.
Definition: type-id.cc:1092
uint16_t AllocateUid(std::string name)
Create a new unique type id.
Definition: type-id.cc:372
void SetParent(uint16_t uid, uint16_t parent)
Set the parent of a type id.
Definition: type-id.cc:453
std::size_t GetSize(void) const
Get the size of this object.
Definition: type-id.cc:972
uint16_t GetParent(uint16_t uid) const
Get the parent of a type id.
Definition: type-id.cc:539
void SetSize(uint16_t uid, std::size_t size)
Set the size of the object class referred to by this id.
Definition: type-id.cc:468
SupportLevel
The level of support or deprecation for attributes or trace sources.
Definition: type-id.h:69
TypeId SetSize(std::size_t size)
Set the size of this type.
Definition: type-id.cc:918
void AddTraceSource(uint16_t uid, std::string name, std::string help, Ptr< const TraceSourceAccessor > accessor, std::string callback, TypeId::SupportLevel supportLevel=TypeId::SUPPORTED, const std::string &supportMsg="")
Record a new TraceSource.
Definition: type-id.cc:734
std::string GetName(uint16_t uid) const
Get the name of a type id.
Definition: type-id.cc:522
std::string callback
Callback function signature type.
Definition: type-id.h:103
TypeId::SupportLevel supportLevel
Support level/deprecation.
Definition: type-id.cc:319
TypeId::hash_t GetHash(uint16_t uid) const
Get the hash of a type id.
Definition: type-id.cc:530
std::string GetAttributeFullName(uint32_t i) const
Get the Attribute name by index.
Definition: type-id.cc:1071
bool HasConstructor(uint16_t uid) const
Check if a type id has a constructor Callback.
Definition: type-id.cc:578
bool IsChildOf(TypeId other) const
Check if this TypeId is a child of another.
Definition: type-id.cc:939
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:220
Callback< ObjectBase * > constructor
The constructor Callback.
Definition: type-id.cc:311
struct TypeId::TraceSourceInformation GetTraceSource(uint32_t i) const
Get the trace source by index.
Definition: type-id.cc:1085
std::string GetGroupName(void) const
Get the group name.
Definition: type-id.cc:950
struct IidManager::IidInformation * LookupInformation(uint16_t uid) const
Retrieve the information record for a type.
Definition: type-id.cc:444
struct TypeId::AttributeInformation GetAttribute(uint32_t i) const
Get Attribute information by index.
Definition: type-id.cc:1065
Debug message logging.
#define IID
Definition: type-id.cc:368
void AddConstructor(uint16_t uid, Callback< ObjectBase * > callback)
Add a constructor Callback to this type id.
Definition: type-id.cc:483
a unique identifier for an interface.
Definition: type-id.h:58
std::vector< struct IidInformation >::const_iterator Iterator
Iterator type.
Definition: type-id.cc:324
std::size_t GetSize(uint16_t uid) const
Get the size of a type id.
Definition: type-id.cc:556
Generic Hash function interface.
Definition: hash.h:87
std::string help
Attribute help string.
Definition: type-id.h:80
TypeId HideFromDocumentation(void)
Hide this TypeId from documentation.
Definition: type-id.cc:1117
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:813
uint16_t GetUid(std::string name) const
Get a type id by name.
Definition: type-id.cc:496
void AddAttribute(uint16_t uid, std::string name, std::string help, uint32_t flags, Ptr< const AttributeValue > initialValue, Ptr< const AttributeAccessor > accessor, Ptr< const AttributeChecker > checker, TypeId::SupportLevel supportLevel=TypeId::SUPPORTED, const std::string &supportMsg="")
Record a new attribute in a type id.
Definition: type-id.cc:632