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 
373 #define IID "IidManager"
374 
379 #define IIDL IID << ": "
380 
381 uint16_t
382 IidManager::AllocateUid (std::string name)
383 {
384  NS_LOG_FUNCTION (IID << name);
385  // Type names are definitive: equal names are equal types
386  NS_ASSERT_MSG (m_namemap.count (name) == 0,
387  "Trying to allocate twice the same uid: " << name);
388 
389  TypeId::hash_t hash = Hasher (name) & (~HashChainFlag);
390  if (m_hashmap.count (hash) == 1) {
391  NS_LOG_ERROR ("Hash chaining TypeId for '" << name << "'. "
392  << "This is not a bug, but is extremely unlikely. "
393  << "Please contact the ns3 developers.");
394  // ns3 developer contacted about this message:
395  // You have four options (in order of difficulty):
396  // 1. Let it ride, and play the odds that a third collision
397  // never appears.
398  // 2. Change the name of the new (or old) tag, even trivially, to
399  // remove the collision.
400  // 3. Switch to 64-bit hashes.
401  // 4. Implement 2-bit (or higher) chaining.
402  //
403  // Oh, by the way, I owe you a beer, since I bet Mathieu that
404  // this would never happen.. -- Peter Barnes, LLNL
405 
406  NS_ASSERT_MSG (m_hashmap.count (hash | HashChainFlag) == 0,
407  "Triplicate hash detected while chaining TypeId for '"
408  << name
409  << "'. Please contact the ns3 developers for assistance.");
410  // ns3 developer contacted about this message:
411  // You have three options: #2-4 above.
412  //
413  // Oh, by the way, I have no idea how this crazy hashing idea got
414  // into ns3. -- Peter Barnes, LLNL
415 
416  // Alphabetize the two types, so it's deterministic
417  struct IidInformation * hinfo = LookupInformation (GetUid (hash));
418  if (name > hinfo->name)
419  { // new type gets chained
420  NS_LOG_LOGIC (IIDL << "New TypeId '" << name << "' getting chained.");
421  hash = hash | HashChainFlag;
422  }
423  else
424  { // chain old type
425  NS_LOG_LOGIC (IIDL << "Old TypeId '" << hinfo->name << "' getting chained.");
426  uint32_t oldUid = GetUid (hinfo->hash);
427  m_hashmap.erase (m_hashmap.find (hinfo->hash));
428  hinfo->hash = hash | HashChainFlag;
429  m_hashmap.insert (std::make_pair (hinfo->hash, oldUid));
430  // leave new hash unchained
431  }
432  }
433 
434  struct IidInformation information;
435  information.name = name;
436  information.hash = hash;
437  information.parent = 0;
438  information.groupName = "";
439  information.size = (std::size_t)(-1);
440  information.hasConstructor = false;
441  information.mustHideFromDocumentation = false;
442  m_information.push_back (information);
443  uint32_t uid = m_information.size ();
444  NS_ASSERT (uid <= 0xffff);
445 
446  // Add to both maps:
447  m_namemap.insert (std::make_pair (name, uid));
448  m_hashmap.insert (std::make_pair (hash, uid));
449  NS_LOG_LOGIC (IIDL << uid);
450  return uid;
451 }
452 
454 IidManager::LookupInformation (uint16_t uid) const
455 {
456  NS_LOG_FUNCTION (IID << uid);
457  NS_ASSERT (uid <= m_information.size () && uid != 0);
458  NS_LOG_LOGIC (IIDL << m_information[uid-1].name);
459  return const_cast<struct IidInformation *> (&m_information[uid-1]);
460 }
461 
462 void
463 IidManager::SetParent (uint16_t uid, uint16_t parent)
464 {
465  NS_LOG_FUNCTION (IID << uid << parent);
466  NS_ASSERT (parent <= m_information.size ());
467  struct IidInformation *information = LookupInformation (uid);
468  information->parent = parent;
469 }
470 void
471 IidManager::SetGroupName (uint16_t uid, std::string groupName)
472 {
473  NS_LOG_FUNCTION (IID << uid << groupName);
474  struct IidInformation *information = LookupInformation (uid);
475  information->groupName = groupName;
476 }
477 void
478 IidManager::SetSize (uint16_t uid, std::size_t size)
479 {
480  NS_LOG_FUNCTION (IID << uid << size);
481  struct IidInformation *information = LookupInformation (uid);
482  information->size = size;
483 }
484 void
486 {
487  NS_LOG_FUNCTION (IID << uid);
488  struct IidInformation *information = LookupInformation (uid);
489  information->mustHideFromDocumentation = true;
490 }
491 
492 void
494 {
495  NS_LOG_FUNCTION (IID << uid << &callback);
496  struct IidInformation *information = LookupInformation (uid);
497  if (information->hasConstructor)
498  {
499  NS_FATAL_ERROR (information->name<<" already has a constructor.");
500  }
501  information->hasConstructor = true;
502  information->constructor = callback;
503 }
504 
505 uint16_t
506 IidManager::GetUid (std::string name) const
507 {
508  NS_LOG_FUNCTION (IID << name);
509  uint16_t uid = 0;
510  namemap_t::const_iterator it = m_namemap.find (name);
511  if (it != m_namemap.end ())
512  {
513  uid = it->second;
514  }
515  NS_LOG_LOGIC (IIDL << uid);
516  return uid;
517 }
518 uint16_t
520 {
521  NS_LOG_FUNCTION (IID << hash);
522  hashmap_t::const_iterator it = m_hashmap.find (hash);
523  uint16_t uid = 0;
524  if (it != m_hashmap.end ())
525  {
526  uid = it->second;
527  }
528  NS_LOG_LOGIC (IIDL << uid);
529  return uid;
530 }
531 std::string
532 IidManager::GetName (uint16_t uid) const
533 {
534  NS_LOG_FUNCTION (IID << uid);
535  struct IidInformation *information = LookupInformation (uid);
536  NS_LOG_LOGIC (IIDL << information->name);
537  return information->name;
538 }
540 IidManager::GetHash (uint16_t uid) const
541 {
542  NS_LOG_FUNCTION (IID << uid);
543  struct IidInformation *information = LookupInformation (uid);
544  TypeId::hash_t hash = information->hash;
545  NS_LOG_LOGIC (IIDL << hash);
546  return hash;
547 }
548 uint16_t
549 IidManager::GetParent (uint16_t uid) const
550 {
551  NS_LOG_FUNCTION (IID << uid);
552  struct IidInformation *information = LookupInformation (uid);
553  uint16_t pid = information->parent;
554  NS_LOG_LOGIC (IIDL << pid);
555  return pid;
556 }
557 std::string
558 IidManager::GetGroupName (uint16_t uid) const
559 {
560  NS_LOG_FUNCTION (IID << uid);
561  struct IidInformation *information = LookupInformation (uid);
562  NS_LOG_LOGIC (IIDL << information->groupName);
563  return information->groupName;
564 }
565 std::size_t
566 IidManager::GetSize (uint16_t uid) const
567 {
568  NS_LOG_FUNCTION (IID << uid);
569  struct IidInformation *information = LookupInformation (uid);
570  std::size_t size = information->size;
571  NS_LOG_LOGIC (IIDL << size);
572  return size;
573 }
574 
576 IidManager::GetConstructor (uint16_t uid) const
577 {
578  NS_LOG_FUNCTION (IID << uid);
579  struct IidInformation *information = LookupInformation (uid);
580  if (!information->hasConstructor)
581  {
582  NS_FATAL_ERROR ("Requested constructor for "<<information->name<<" but it does not have one.");
583  }
584  return information->constructor;
585 }
586 
587 bool
588 IidManager::HasConstructor (uint16_t uid) const
589 {
590  NS_LOG_FUNCTION (IID << uid);
591  struct IidInformation *information = LookupInformation (uid);
592  bool hasC = information->hasConstructor;
593  NS_LOG_LOGIC (IIDL << hasC);
594  return hasC;
595 }
596 
597 uint32_t
599 {
600  NS_LOG_FUNCTION (IID << m_information.size ());
601  return m_information.size ();
602 }
603 uint16_t
604 IidManager::GetRegistered (uint32_t i) const
605 {
606  NS_LOG_FUNCTION (IID << i);
607  return i + 1;
608 }
609 
610 bool
612  std::string name)
613 {
614  NS_LOG_FUNCTION (IID << uid << name);
615  struct IidInformation *information = LookupInformation (uid);
616  while (true)
617  {
618  for (std::vector<struct TypeId::AttributeInformation>::const_iterator i = information->attributes.begin ();
619  i != information->attributes.end (); ++i)
620  {
621  if (i->name == name)
622  {
623  NS_LOG_LOGIC (IIDL << true);
624  return true;
625  }
626  }
627  struct IidInformation *parent = LookupInformation (information->parent);
628  if (parent == information)
629  {
630  // top of inheritance tree
631  NS_LOG_LOGIC (IIDL << false);
632  return false;
633  }
634  // check parent
635  information = parent;
636  }
637  NS_LOG_LOGIC (IIDL << false);
638  return false;
639 }
640 
641 void
643  std::string name,
644  std::string help,
645  uint32_t flags,
646  Ptr<const AttributeValue> initialValue,
649  TypeId::SupportLevel supportLevel,
650  const std::string &supportMsg)
651 {
652  NS_LOG_FUNCTION (IID << uid << name << help << flags
653  << initialValue << accessor << checker
654  << supportLevel << supportMsg);
655  struct IidInformation *information = LookupInformation (uid);
656  if (name.find (' ') != std::string::npos)
657  {
658  NS_FATAL_ERROR ("Attribute name \"" << name << "\" may not contain spaces ' ', "
659  << "encountered when registering TypeId \""
660  << information->name << "\"");
661  }
662  if (HasAttribute (uid, name))
663  {
664  NS_FATAL_ERROR ("Attribute \"" << name << "\" already registered on tid=\"" <<
665  information->name << "\"");
666  }
667  struct TypeId::AttributeInformation info;
668  info.name = name;
669  info.help = help;
670  info.flags = flags;
671  info.initialValue = initialValue;
672  info.originalInitialValue = initialValue;
673  info.accessor = accessor;
674  info.checker = checker;
675  info.supportLevel = supportLevel;
676  info.supportMsg = supportMsg;
677  information->attributes.push_back (info);
678  NS_LOG_LOGIC (IIDL << information->attributes.size () - 1);
679 }
680 void
682  uint32_t i,
683  Ptr<const AttributeValue> initialValue)
684 {
685  NS_LOG_FUNCTION (IID << uid << i << initialValue);
686  struct IidInformation *information = LookupInformation (uid);
687  NS_ASSERT (i < information->attributes.size ());
688  information->attributes[i].initialValue = initialValue;
689 }
690 
691 
692 
693 uint32_t
694 IidManager::GetAttributeN (uint16_t uid) const
695 {
696  NS_LOG_FUNCTION (IID << uid);
697  struct IidInformation *information = LookupInformation (uid);
698  uint32_t size = information->attributes.size ();
699  NS_LOG_LOGIC (IIDL << size);
700  return size;
701 }
703 IidManager::GetAttribute(uint16_t uid, uint32_t i) const
704 {
705  NS_LOG_FUNCTION (IID << uid << i);
706  struct IidInformation *information = LookupInformation (uid);
707  NS_ASSERT (i < information->attributes.size ());
708  NS_LOG_LOGIC (IIDL << information->name);
709  return information->attributes[i];
710 }
711 
712 bool
714  std::string name)
715 {
716  NS_LOG_FUNCTION (IID << uid << name);
717  struct IidInformation *information = LookupInformation (uid);
718  while (true)
719  {
720  for (std::vector<struct TypeId::TraceSourceInformation>::const_iterator i = information->traceSources.begin ();
721  i != information->traceSources.end (); ++i)
722  {
723  if (i->name == name)
724  {
725  NS_LOG_LOGIC (IIDL << true);
726  return true ;
727  }
728  }
729  struct IidInformation *parent = LookupInformation (information->parent);
730  if (parent == information)
731  {
732  // top of inheritance tree
733  NS_LOG_LOGIC (IIDL << false);
734  return false;
735  }
736  // check parent
737  information = parent;
738  }
739  NS_LOG_LOGIC (IIDL << false);
740  return false;
741 }
742 
743 void
745  std::string name,
746  std::string help,
748  std::string callback,
749  TypeId::SupportLevel supportLevel,
750  const std::string &supportMsg)
751 {
752  NS_LOG_FUNCTION (IID << uid << name << help
753  << accessor << callback
754  << supportLevel << supportMsg);
755  struct IidInformation *information = LookupInformation (uid);
756  if (HasTraceSource (uid, name))
757  {
758  NS_FATAL_ERROR ("Trace source \"" << name << "\" already registered on tid=\"" <<
759  information->name << "\"");
760  }
761  struct TypeId::TraceSourceInformation source;
762  source.name = name;
763  source.help = help;
764  source.accessor = accessor;
765  source.callback = callback;
766  source.supportLevel = supportLevel;
767  source.supportMsg = supportMsg;
768  information->traceSources.push_back (source);
769  NS_LOG_LOGIC (IIDL << information->traceSources.size () - 1);
770 }
771 uint32_t
772 IidManager::GetTraceSourceN (uint16_t uid) const
773 {
774  NS_LOG_FUNCTION (IID << uid);
775  struct IidInformation *information = LookupInformation (uid);
776  uint32_t size = information->traceSources.size ();
777  NS_LOG_LOGIC (IIDL << size);
778  return size;
779 }
781 IidManager::GetTraceSource(uint16_t uid, uint32_t i) const
782 {
783  NS_LOG_FUNCTION (IID << uid << i);
784  struct IidInformation *information = LookupInformation (uid);
785  NS_ASSERT (i < information->traceSources.size ());
786  NS_LOG_LOGIC (IIDL << information->name);
787  return information->traceSources[i];
788 }
789 bool
791 {
792  NS_LOG_FUNCTION (IID << uid);
793  struct IidInformation *information = LookupInformation (uid);
794  bool hide = information->mustHideFromDocumentation;
795  NS_LOG_LOGIC (IIDL << hide);
796  return hide;
797 }
798 
799 } // namespace ns3
800 
801 namespace ns3 {
802 
803 /*********************************************************************
804  * The TypeId class
805  *********************************************************************/
806 
807 TypeId::TypeId (const char *name)
808 {
809  NS_LOG_FUNCTION (this << name);
810  uint16_t uid = IidManager::Get ()->AllocateUid (name);
811  NS_LOG_LOGIC (uid);
812  NS_ASSERT (uid != 0);
813  m_tid = uid;
814 }
815 
816 
817 TypeId::TypeId (uint16_t tid)
818  : m_tid (tid)
819 {
820  NS_LOG_FUNCTION (this << tid);
821 }
822 TypeId
823 TypeId::LookupByName (std::string name)
824 {
825  NS_LOG_FUNCTION (name);
826  uint16_t uid = IidManager::Get ()->GetUid (name);
827  NS_ASSERT_MSG (uid != 0, "Assert in TypeId::LookupByName: " << name << " not found");
828  return TypeId (uid);
829 }
830 bool
831 TypeId::LookupByNameFailSafe (std::string name, TypeId *tid)
832 {
833  NS_LOG_FUNCTION (name << tid->GetUid ());
834  uint16_t uid = IidManager::Get ()->GetUid (name);
835  if (uid == 0)
836  {
837  return false;
838  }
839  *tid = TypeId (uid);
840  return true;
841 }
842 TypeId
844 {
845  uint16_t uid = IidManager::Get ()->GetUid (hash);
846  NS_ASSERT_MSG (uid != 0, "Assert in TypeId::LookupByHash: 0x"
847  << std::hex << hash << std::dec << " not found");
848  return TypeId (uid);
849 }
850 bool
852 {
853  uint16_t uid = IidManager::Get ()->GetUid (hash);
854  if (uid == 0)
855  {
856  return false;
857  }
858  *tid = TypeId (uid);
859  return true;
860 }
861 
862 uint32_t
864 {
866  return IidManager::Get ()->GetRegisteredN ();
867 }
868 TypeId
870 {
871  NS_LOG_FUNCTION (i);
872  return TypeId (IidManager::Get ()->GetRegistered (i));
873 }
874 
875 bool
876 TypeId::LookupAttributeByName (std::string name, struct TypeId::AttributeInformation *info) const
877 {
878  NS_LOG_FUNCTION (this << name << info);
879  TypeId tid;
880  TypeId nextTid = *this;
881  do {
882  tid = nextTid;
883  for (uint32_t i = 0; i < tid.GetAttributeN (); i++)
884  {
885  struct TypeId::AttributeInformation tmp = tid.GetAttribute(i);
886  if (tmp.name == name)
887  {
888  if (tmp.supportLevel == TypeId::SUPPORTED)
889  {
890  *info = tmp;
891  return true;
892  }
893  else if (tmp.supportLevel == TypeId::DEPRECATED)
894  {
895  std::cerr << "Attribute '" << name << "' is deprecated: "
896  << tmp.supportMsg << std::endl;
897  *info = tmp;
898  return true;
899  }
900  else if (tmp.supportLevel == TypeId::OBSOLETE)
901  {
902  NS_FATAL_ERROR ("Attribute '" << name
903  << "' is obsolete, with no fallback: "
904  << tmp.supportMsg);
905  }
906  }
907  }
908  nextTid = tid.GetParent ();
909  } while (nextTid != tid);
910  return false;
911 }
912 
913 TypeId
915 {
916  NS_LOG_FUNCTION (this << tid.GetUid ());
918  return *this;
919 }
920 TypeId
921 TypeId::SetGroupName (std::string groupName)
922 {
923  NS_LOG_FUNCTION (this << groupName);
924  IidManager::Get ()->SetGroupName (m_tid, groupName);
925  return *this;
926 }
927 TypeId
928 TypeId::SetSize (std::size_t size)
929 {
930  NS_LOG_FUNCTION (this << size);
931  IidManager::Get ()->SetSize (m_tid, size);
932  return *this;
933 }
934 TypeId
935 TypeId::GetParent (void) const
936 {
937  NS_LOG_FUNCTION (this);
938  uint16_t parent = IidManager::Get ()->GetParent (m_tid);
939  return TypeId (parent);
940 }
941 bool
942 TypeId::HasParent (void) const
943 {
944  NS_LOG_FUNCTION (this);
945  uint16_t parent = IidManager::Get ()->GetParent (m_tid);
946  return parent != m_tid;
947 }
948 bool
950 {
951  NS_LOG_FUNCTION (this << other.GetUid ());
952  TypeId tmp = *this;
953  while (tmp != other && tmp != tmp.GetParent ())
954  {
955  tmp = tmp.GetParent ();
956  }
957  return tmp == other && *this != other;
958 }
959 std::string
961 {
962  NS_LOG_FUNCTION (this);
963  std::string groupName = IidManager::Get ()->GetGroupName (m_tid);
964  return groupName;
965 }
966 
967 std::string
968 TypeId::GetName (void) const
969 {
970  NS_LOG_FUNCTION (this);
971  std::string name = IidManager::Get ()->GetName (m_tid);
972  return name;
973 }
974 
976 TypeId::GetHash (void) const
977 {
978  hash_t hash = IidManager::Get ()->GetHash (m_tid);
979  return hash;
980 }
981 std::size_t
982 TypeId::GetSize (void) const
983 {
984  NS_LOG_FUNCTION (this);
985  std::size_t size = IidManager::Get ()->GetSize (m_tid);
986  return size;
987 }
988 
989 bool
991 {
992  NS_LOG_FUNCTION (this);
993  bool hasConstructor = IidManager::Get ()->HasConstructor (m_tid);
994  return hasConstructor;
995 }
996 
997 void
999 {
1000  NS_LOG_FUNCTION (this << &cb);
1002 }
1003 
1004 TypeId
1005 TypeId::AddAttribute (std::string name,
1006  std::string help,
1007  const AttributeValue &initialValue,
1010  SupportLevel supportLevel,
1011  const std::string &supportMsg)
1012 {
1013  NS_LOG_FUNCTION (this << name << help
1014  << &initialValue << accessor << checker
1015  << supportLevel << supportMsg);
1016  IidManager::Get ()->AddAttribute (m_tid, name, help, ATTR_SGC,
1017  initialValue.Copy (), accessor, checker,
1018  supportLevel, supportMsg);
1019  return *this;
1020 }
1021 
1022 TypeId
1023 TypeId::AddAttribute (std::string name,
1024  std::string help,
1025  uint32_t flags,
1026  const AttributeValue &initialValue,
1029  SupportLevel supportLevel,
1030  const std::string &supportMsg)
1031 {
1032  NS_LOG_FUNCTION (this << name << help << flags
1033  << &initialValue << accessor << checker
1034  << supportLevel << supportMsg);
1035  IidManager::Get ()->AddAttribute (m_tid, name, help, flags,
1036  initialValue.Copy (), accessor, checker,
1037  supportLevel, supportMsg);
1038  return *this;
1039 }
1040 
1041 bool
1043  Ptr<const AttributeValue> initialValue)
1044 {
1045  NS_LOG_FUNCTION (this << i << initialValue);
1046  IidManager::Get ()->SetAttributeInitialValue (m_tid, i, initialValue);
1047  return true;
1048 }
1049 
1050 
1053 {
1054  NS_LOG_FUNCTION (this);
1056  return cb;
1057 }
1058 
1059 bool
1061 {
1062  NS_LOG_FUNCTION (this);
1063  bool mustHide = IidManager::Get ()->MustHideFromDocumentation (m_tid);
1064  return mustHide;
1065 }
1066 
1067 uint32_t
1069 {
1070  NS_LOG_FUNCTION (this);
1071  uint32_t n = IidManager::Get ()->GetAttributeN (m_tid);
1072  return n;
1073 }
1075 TypeId::GetAttribute(uint32_t i) const
1076 {
1077  NS_LOG_FUNCTION (this << i);
1078  return IidManager::Get ()->GetAttribute(m_tid, i);
1079 }
1080 std::string
1082 {
1083  NS_LOG_FUNCTION (this << i);
1084  struct TypeId::AttributeInformation info = GetAttribute(i);
1085  return GetName () + "::" + info.name;
1086 }
1087 
1088 uint32_t
1090 {
1091  NS_LOG_FUNCTION (this);
1092  return IidManager::Get ()->GetTraceSourceN (m_tid);
1093 }
1095 TypeId::GetTraceSource(uint32_t i) const
1096 {
1097  NS_LOG_FUNCTION (this << i);
1098  return IidManager::Get ()->GetTraceSource(m_tid, i);
1099 }
1100 
1101 TypeId
1102 TypeId::AddTraceSource (std::string name,
1103  std::string help,
1105 {
1106  return AddTraceSource (name, help, accessor, "(not yet documented)");
1107 }
1108 
1109 TypeId
1110 TypeId::AddTraceSource (std::string name,
1111  std::string help,
1113  std::string callback,
1114  SupportLevel supportLevel,
1115  const std::string &supportMsg)
1116 {
1117  NS_LOG_FUNCTION (this << name << help
1118  << accessor << callback
1119  << supportLevel << supportMsg);
1120  IidManager::Get ()->AddTraceSource (m_tid, name, help,
1121  accessor, callback,
1122  supportLevel, supportMsg);
1123  return *this;
1124 }
1125 
1126 TypeId
1128 {
1129  NS_LOG_FUNCTION (this);
1131  return *this;
1132 }
1133 
1136  struct TraceSourceInformation *info) const
1137 {
1138  NS_LOG_FUNCTION (this << name);
1139  TypeId tid;
1140  TypeId nextTid = *this;
1141  struct TypeId::TraceSourceInformation tmp;
1142  do {
1143  tid = nextTid;
1144  for (uint32_t i = 0; i < tid.GetTraceSourceN (); i++)
1145  {
1146  tmp = tid.GetTraceSource (i);
1147  if (tmp.name == name)
1148  {
1149  if (tmp.supportLevel == TypeId::SUPPORTED)
1150  {
1151  *info = tmp;
1152  return tmp.accessor;
1153  }
1154  else if (tmp.supportLevel == TypeId::DEPRECATED)
1155  {
1156  std::cerr << "TraceSource '" << name << "' is deprecated: "
1157  << tmp.supportMsg << std::endl;
1158  *info = tmp;
1159  return tmp.accessor;
1160  }
1161  else if (tmp.supportLevel == TypeId::OBSOLETE)
1162  {
1163  NS_FATAL_ERROR ("TraceSource '" << name
1164  << "' is obsolete, with no fallback: "
1165  << tmp.supportMsg);
1166  }
1167  }
1168  }
1169  nextTid = tid.GetParent ();
1170  } while (nextTid != tid);
1171  return 0;
1172 }
1173 
1175 TypeId::LookupTraceSourceByName (std::string name) const
1176 {
1177  struct TraceSourceInformation info;
1178  return LookupTraceSourceByName (name, &info);
1179 }
1180 
1181 uint16_t
1182 TypeId::GetUid (void) const
1183 {
1184  NS_LOG_FUNCTION (this);
1185  return m_tid;
1186 }
1187 void
1188 TypeId::SetUid (uint16_t uid)
1189 {
1190  NS_LOG_FUNCTION (this << uid);
1191  m_tid = uid;
1192 }
1193 
1200 std::ostream & operator << (std::ostream &os, TypeId tid)
1201 {
1202  os << tid.GetName ();
1203  return os;
1204 }
1211 std::istream & operator >> (std::istream &is, TypeId &tid)
1212 {
1213  std::string tidString;
1214  is >> tidString;
1215  bool ok = TypeId::LookupByNameFailSafe (tidString, &tid);
1216  if (!ok)
1217  {
1218  is.setstate (std::ios_base::badbit);
1219  }
1220  return is;
1221 }
1222 
1223 
1225 
1227 {
1228  return a.m_tid < b.m_tid;
1229 }
1230 
1231 } // namespace ns3
std::vector< struct TypeId::TraceSourceInformation > traceSources
The container of TraceSources.
Definition: type-id.cc:317
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:1068
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:611
Callback< ObjectBase * > GetConstructor(uint16_t uid) const
Get the constructor Callback of a type id.
Definition: type-id.cc:576
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="")
Record in this TypeId the fact that a new attribute exists.
Definition: type-id.cc:1005
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:713
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:558
static TypeId LookupByHash(hash_t hash)
Get a TypeId by hash.
Definition: type-id.cc:843
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:703
#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:471
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:935
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:831
#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:598
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:1052
static bool LookupByHashFailSafe(hash_t hash, TypeId *tid)
Get a TypeId by hash.
Definition: type-id.cc:851
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:153
bool HasConstructor(void) const
Check if this TypeId has a constructor.
Definition: type-id.cc:990
bool MustHideFromDocumentation(void) const
Check if this TypeId should not be listed in documentation.
Definition: type-id.cc:1060
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
NS_ASSERT_MSG(false,"Ipv4AddressGenerator::MaskToIndex(): Impossible")
#define IID
Definition: type-id.cc:373
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:863
Ptr< const TraceSourceAccessor > LookupTraceSourceByName(std::string name) const
Find a TraceSource by name.
Definition: type-id.cc:1175
uint32_t GetAttributeN(uint16_t uid) const
Get the number of attributes.
Definition: type-id.cc:694
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:942
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:1089
static TypeId GetRegistered(uint32_t i)
Get a TypeId by index.
Definition: type-id.cc:869
uint32_t GetTraceSourceN(uint16_t uid) const
Get the number of Trace sources.
Definition: type-id.cc:772
void SetAttributeInitialValue(uint16_t uid, uint32_t i, Ptr< const AttributeValue > initialValue)
Set the initial value of an Attribute.
Definition: type-id.cc:681
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:781
#define IIDL
Definition: type-id.cc:379
void HideFromDocumentation(uint16_t uid)
Mark this type id to be excluded from documentation.
Definition: type-id.cc:485
bool MustHideFromDocumentation(uint16_t uid) const
Check if this TypeId should not be listed in documentation.
Definition: type-id.cc:790
TypeId::SupportLevel supportLevel
Support level/deprecation.
Definition: type-id.h:92
uint32_t flags
AttributeFlags value.
Definition: type-id.h:82
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:921
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:876
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:604
std::string GetName(void) const
Get the name.
Definition: type-id.cc:968
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:1182
void DoAddConstructor(Callback< ObjectBase * > callback)
Implementation for AddConstructor().
Definition: type-id.cc:998
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
hash_t GetHash(void) const
Get the hash.
Definition: type-id.cc:976
void SetUid(uint16_t uid)
Set the internal id of this TypeId.
Definition: type-id.cc:1188
bool SetAttributeInitialValue(uint32_t i, Ptr< const AttributeValue > initialValue)
Set the initial value of an Attribute.
Definition: type-id.cc:1042
NS_DEPRECATED TypeId AddTraceSource(std::string name, std::string help, Ptr< const TraceSourceAccessor > accessor)
Record a new TraceSource.
Definition: type-id.cc:1102
uint16_t AllocateUid(std::string name)
Create a new unique type id.
Definition: type-id.cc:382
void SetParent(uint16_t uid, uint16_t parent)
Set the parent of a type id.
Definition: type-id.cc:463
std::size_t GetSize(void) const
Get the size of this object.
Definition: type-id.cc:982
uint16_t GetParent(uint16_t uid) const
Get the parent of a type id.
Definition: type-id.cc:549
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:478
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:928
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:744
std::string GetName(uint16_t uid) const
Get the name of a type id.
Definition: type-id.cc:532
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:540
std::string GetAttributeFullName(uint32_t i) const
Get the Attribute name by index.
Definition: type-id.cc:1081
bool HasConstructor(uint16_t uid) const
Check if a type id has a constructor Callback.
Definition: type-id.cc:588
bool IsChildOf(TypeId other) const
Check if this TypeId is a child of another.
Definition: type-id.cc:949
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:253
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:1095
std::string GetGroupName(void) const
Get the group name.
Definition: type-id.cc:960
struct IidManager::IidInformation * LookupInformation(uint16_t uid) const
Retrieve the information record for a type.
Definition: type-id.cc:454
struct TypeId::AttributeInformation GetAttribute(uint32_t i) const
Get Attribute information by index.
Definition: type-id.cc:1075
Debug message logging.
void AddConstructor(uint16_t uid, Callback< ObjectBase * > callback)
Add a constructor Callback to this type id.
Definition: type-id.cc:493
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:566
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:1127
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:823
uint16_t GetUid(std::string name) const
Get a type id by name.
Definition: type-id.cc:506
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:642