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