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  information.supportLevel = TypeId::SUPPORTED;
446  m_information.push_back (information);
447  std::size_t tuid = m_information.size ();
448  NS_ASSERT (tuid <= 0xffff);
449  uint16_t uid = static_cast<uint16_t> (tuid);
450 
451  // Add to both maps:
452  m_namemap.insert (std::make_pair (name, uid));
453  m_hashmap.insert (std::make_pair (hash, uid));
454  NS_LOG_LOGIC (IIDL << uid);
455  return uid;
456 }
457 
459 IidManager::LookupInformation (uint16_t uid) const
460 {
461  NS_LOG_FUNCTION (IID << uid);
462  NS_ASSERT (uid <= m_information.size () && uid != 0);
463  NS_LOG_LOGIC (IIDL << m_information[uid - 1].name);
464  return const_cast<struct IidInformation *> (&m_information[uid - 1]);
465 }
466 
467 void
468 IidManager::SetParent (uint16_t uid, uint16_t parent)
469 {
470  NS_LOG_FUNCTION (IID << uid << parent);
471  NS_ASSERT (parent <= m_information.size ());
472  struct IidInformation *information = LookupInformation (uid);
473  information->parent = parent;
474 }
475 void
476 IidManager::SetGroupName (uint16_t uid, std::string groupName)
477 {
478  NS_LOG_FUNCTION (IID << uid << groupName);
479  struct IidInformation *information = LookupInformation (uid);
480  information->groupName = groupName;
481 }
482 void
483 IidManager::SetSize (uint16_t uid, std::size_t size)
484 {
485  NS_LOG_FUNCTION (IID << uid << size);
486  struct IidInformation *information = LookupInformation (uid);
487  information->size = size;
488 }
489 void
491 {
492  NS_LOG_FUNCTION (IID << uid);
493  struct IidInformation *information = LookupInformation (uid);
494  information->mustHideFromDocumentation = true;
495 }
496 
497 void
499 {
500  NS_LOG_FUNCTION (IID << uid << &callback);
501  struct IidInformation *information = LookupInformation (uid);
502  if (information->hasConstructor)
503  {
504  NS_FATAL_ERROR (information->name << " already has a constructor.");
505  }
506  information->hasConstructor = true;
507  information->constructor = callback;
508 }
509 
510 uint16_t
511 IidManager::GetUid (std::string name) const
512 {
513  NS_LOG_FUNCTION (IID << name);
514  uint16_t uid = 0;
515  namemap_t::const_iterator it = m_namemap.find (name);
516  if (it != m_namemap.end ())
517  {
518  uid = it->second;
519  }
520  NS_LOG_LOGIC (IIDL << uid);
521  return uid;
522 }
523 uint16_t
525 {
526  NS_LOG_FUNCTION (IID << hash);
527  hashmap_t::const_iterator it = m_hashmap.find (hash);
528  uint16_t uid = 0;
529  if (it != m_hashmap.end ())
530  {
531  uid = it->second;
532  }
533  NS_LOG_LOGIC (IIDL << uid);
534  return uid;
535 }
536 std::string
537 IidManager::GetName (uint16_t uid) const
538 {
539  NS_LOG_FUNCTION (IID << uid);
540  struct IidInformation *information = LookupInformation (uid);
541  NS_LOG_LOGIC (IIDL << information->name);
542  return information->name;
543 }
545 IidManager::GetHash (uint16_t uid) const
546 {
547  NS_LOG_FUNCTION (IID << uid);
548  struct IidInformation *information = LookupInformation (uid);
549  TypeId::hash_t hash = information->hash;
550  NS_LOG_LOGIC (IIDL << hash);
551  return hash;
552 }
553 uint16_t
554 IidManager::GetParent (uint16_t uid) const
555 {
556  NS_LOG_FUNCTION (IID << uid);
557  struct IidInformation *information = LookupInformation (uid);
558  uint16_t pid = information->parent;
559  NS_LOG_LOGIC (IIDL << pid);
560  return pid;
561 }
562 std::string
563 IidManager::GetGroupName (uint16_t uid) const
564 {
565  NS_LOG_FUNCTION (IID << uid);
566  struct IidInformation *information = LookupInformation (uid);
567  NS_LOG_LOGIC (IIDL << information->groupName);
568  return information->groupName;
569 }
570 std::size_t
571 IidManager::GetSize (uint16_t uid) const
572 {
573  NS_LOG_FUNCTION (IID << uid);
574  struct IidInformation *information = LookupInformation (uid);
575  std::size_t size = information->size;
576  NS_LOG_LOGIC (IIDL << size);
577  return size;
578 }
579 
581 IidManager::GetConstructor (uint16_t uid) const
582 {
583  NS_LOG_FUNCTION (IID << uid);
584  struct IidInformation *information = LookupInformation (uid);
585  if (!information->hasConstructor)
586  {
587  NS_FATAL_ERROR ("Requested constructor for " << information->name << " but it does not have one.");
588  }
589  return information->constructor;
590 }
591 
592 bool
593 IidManager::HasConstructor (uint16_t uid) const
594 {
595  NS_LOG_FUNCTION (IID << uid);
596  struct IidInformation *information = LookupInformation (uid);
597  bool hasC = information->hasConstructor;
598  NS_LOG_LOGIC (IIDL << hasC);
599  return hasC;
600 }
601 
602 uint16_t
604 {
605  NS_LOG_FUNCTION (IID << m_information.size ());
606  return static_cast<uint16_t> (m_information.size ());
607 }
608 uint16_t
609 IidManager::GetRegistered (uint16_t i) const
610 {
611  NS_LOG_FUNCTION (IID << i);
612  return i + 1;
613 }
614 
615 bool
617  std::string name)
618 {
619  NS_LOG_FUNCTION (IID << uid << name);
620  struct IidInformation *information = LookupInformation (uid);
621  while (true)
622  {
623  for (std::vector<struct TypeId::AttributeInformation>::const_iterator i = information->attributes.begin ();
624  i != information->attributes.end (); ++i)
625  {
626  if (i->name == name)
627  {
628  NS_LOG_LOGIC (IIDL << true);
629  return true;
630  }
631  }
632  struct IidInformation *parent = LookupInformation (information->parent);
633  if (parent == information)
634  {
635  // top of inheritance tree
636  NS_LOG_LOGIC (IIDL << false);
637  return false;
638  }
639  // check parent
640  information = parent;
641  }
642  NS_LOG_LOGIC (IIDL << false);
643  return false;
644 }
645 
646 void
648  std::string name,
649  std::string help,
650  uint32_t flags,
651  Ptr<const AttributeValue> initialValue,
654  TypeId::SupportLevel supportLevel,
655  const std::string &supportMsg)
656 {
657  NS_LOG_FUNCTION (IID << uid << name << help << flags <<
658  initialValue << accessor << checker <<
659  supportLevel << supportMsg);
660  struct IidInformation *information = LookupInformation (uid);
661  if (name.find (' ') != std::string::npos)
662  {
663  NS_FATAL_ERROR ("Attribute name \"" << name << "\" may not contain spaces ' ', " <<
664  "encountered when registering TypeId \"" <<
665  information->name << "\"");
666  }
667  if (HasAttribute (uid, name))
668  {
669  NS_FATAL_ERROR ("Attribute \"" << name <<
670  "\" already registered on tid=\"" <<
671  information->name << "\"");
672  }
673  struct TypeId::AttributeInformation info;
674  info.name = name;
675  info.help = help;
676  info.flags = flags;
677  info.initialValue = initialValue;
678  info.originalInitialValue = initialValue;
679  info.accessor = accessor;
680  info.checker = checker;
681  info.supportLevel = supportLevel;
682  info.supportMsg = supportMsg;
683  information->attributes.push_back (info);
684  NS_LOG_LOGIC (IIDL << information->attributes.size () - 1);
685 }
686 void
688  std::size_t i,
689  Ptr<const AttributeValue> initialValue)
690 {
691  NS_LOG_FUNCTION (IID << uid << i << initialValue);
692  struct IidInformation *information = LookupInformation (uid);
693  NS_ASSERT (i < information->attributes.size ());
694  information->attributes[i].initialValue = initialValue;
695 }
696 
697 
698 
699 std::size_t
700 IidManager::GetAttributeN (uint16_t uid) const
701 {
702  NS_LOG_FUNCTION (IID << uid);
703  struct IidInformation *information = LookupInformation (uid);
704  std::size_t size = information->attributes.size ();
705  NS_LOG_LOGIC (IIDL << size);
706  return size;
707 }
709 IidManager::GetAttribute (uint16_t uid, std::size_t i) const
710 {
711  NS_LOG_FUNCTION (IID << uid << i);
712  struct IidInformation *information = LookupInformation (uid);
713  NS_ASSERT (i < information->attributes.size ());
714  NS_LOG_LOGIC (IIDL << information->name);
715  return information->attributes[i];
716 }
717 
718 bool
720  std::string name)
721 {
722  NS_LOG_FUNCTION (IID << uid << name);
723  struct IidInformation *information = LookupInformation (uid);
724  while (true)
725  {
726  for (std::vector<struct TypeId::TraceSourceInformation>::const_iterator i = information->traceSources.begin ();
727  i != information->traceSources.end (); ++i)
728  {
729  if (i->name == name)
730  {
731  NS_LOG_LOGIC (IIDL << true);
732  return true;
733  }
734  }
735  struct IidInformation *parent = LookupInformation (information->parent);
736  if (parent == information)
737  {
738  // top of inheritance tree
739  NS_LOG_LOGIC (IIDL << false);
740  return false;
741  }
742  // check parent
743  information = parent;
744  }
745  NS_LOG_LOGIC (IIDL << false);
746  return false;
747 }
748 
749 void
751  std::string name,
752  std::string help,
754  std::string callback,
755  TypeId::SupportLevel supportLevel,
756  const std::string &supportMsg)
757 {
758  NS_LOG_FUNCTION (IID << uid << name << help <<
759  accessor << callback <<
760  supportLevel << supportMsg);
761  struct IidInformation *information = LookupInformation (uid);
762  if (HasTraceSource (uid, name))
763  {
764  NS_FATAL_ERROR ("Trace source \"" << name <<
765  "\" already registered on tid=\"" <<
766  information->name << "\"");
767  }
768  struct TypeId::TraceSourceInformation source;
769  source.name = name;
770  source.help = help;
771  source.accessor = accessor;
772  source.callback = callback;
773  source.supportLevel = supportLevel;
774  source.supportMsg = supportMsg;
775  information->traceSources.push_back (source);
776  NS_LOG_LOGIC (IIDL << information->traceSources.size () - 1);
777 }
778 std::size_t
779 IidManager::GetTraceSourceN (uint16_t uid) const
780 {
781  NS_LOG_FUNCTION (IID << uid);
782  struct IidInformation *information = LookupInformation (uid);
783  std::size_t size = information->traceSources.size ();
784  NS_LOG_LOGIC (IIDL << size);
785  return size;
786 }
788 IidManager::GetTraceSource (uint16_t uid, std::size_t i) const
789 {
790  NS_LOG_FUNCTION (IID << uid << i);
791  struct IidInformation *information = LookupInformation (uid);
792  NS_ASSERT (i < information->traceSources.size ());
793  NS_LOG_LOGIC (IIDL << information->name);
794  return information->traceSources[i];
795 }
796 bool
798 {
799  NS_LOG_FUNCTION (IID << uid);
800  struct IidInformation *information = LookupInformation (uid);
801  bool hide = information->mustHideFromDocumentation;
802  NS_LOG_LOGIC (IIDL << hide);
803  return hide;
804 }
805 
806 } // namespace ns3
807 
808 namespace ns3 {
809 
810 /*********************************************************************
811  * The TypeId class
812  *********************************************************************/
813 
814 TypeId::TypeId (const char *name)
815 {
816  NS_LOG_FUNCTION (this << name);
817  uint16_t uid = IidManager::Get ()->AllocateUid (name);
818  NS_LOG_LOGIC (uid);
819  NS_ASSERT (uid != 0);
820  m_tid = uid;
821 }
822 
823 
824 TypeId::TypeId (uint16_t tid)
825  : m_tid (tid)
826 {
827  NS_LOG_FUNCTION (this << tid);
828 }
829 TypeId
830 TypeId::LookupByName (std::string name)
831 {
832  NS_LOG_FUNCTION (name);
833  uint16_t uid = IidManager::Get ()->GetUid (name);
834  NS_ASSERT_MSG (uid != 0, "Assert in TypeId::LookupByName: " << name << " not found");
835  return TypeId (uid);
836 }
837 bool
838 TypeId::LookupByNameFailSafe (std::string name, TypeId *tid)
839 {
840  NS_LOG_FUNCTION (name << tid->GetUid ());
841  uint16_t uid = IidManager::Get ()->GetUid (name);
842  if (uid == 0)
843  {
844  return false;
845  }
846  *tid = TypeId (uid);
847  return true;
848 }
849 TypeId
851 {
852  uint16_t uid = IidManager::Get ()->GetUid (hash);
853  NS_ASSERT_MSG (uid != 0, "Assert in TypeId::LookupByHash: 0x"
854  << std::hex << hash << std::dec << " not found");
855  return TypeId (uid);
856 }
857 bool
859 {
860  uint16_t uid = IidManager::Get ()->GetUid (hash);
861  if (uid == 0)
862  {
863  return false;
864  }
865  *tid = TypeId (uid);
866  return true;
867 }
868 
869 uint16_t
871 {
873  return IidManager::Get ()->GetRegisteredN ();
874 }
875 TypeId
877 {
878  NS_LOG_FUNCTION (i);
879  return TypeId (IidManager::Get ()->GetRegistered (i));
880 }
881 
882 bool
883 TypeId::LookupAttributeByName (std::string name, struct TypeId::AttributeInformation *info) const
884 {
885  NS_LOG_FUNCTION (this << name << info);
886  TypeId tid;
887  TypeId nextTid = *this;
888  do
889  {
890  tid = nextTid;
891  for (std::size_t i = 0; i < tid.GetAttributeN (); i++)
892  {
893  struct TypeId::AttributeInformation tmp = tid.GetAttribute (i);
894  if (tmp.name == name)
895  {
896  if (tmp.supportLevel == TypeId::SUPPORTED)
897  {
898  *info = tmp;
899  return true;
900  }
901  else if (tmp.supportLevel == TypeId::DEPRECATED)
902  {
903  std::cerr << "Attribute '" << name << "' is deprecated: "
904  << tmp.supportMsg << std::endl;
905  *info = tmp;
906  return true;
907  }
908  else if (tmp.supportLevel == TypeId::OBSOLETE)
909  {
910  NS_FATAL_ERROR ("Attribute '" << name <<
911  "' is obsolete, with no fallback: " <<
912  tmp.supportMsg);
913  }
914  }
915  }
916  nextTid = tid.GetParent ();
917  }
918  while (nextTid != tid);
919  return false;
920 }
921 
922 TypeId
924 {
925  NS_LOG_FUNCTION (this << tid.GetUid ());
927  return *this;
928 }
929 TypeId
930 TypeId::SetGroupName (std::string groupName)
931 {
932  NS_LOG_FUNCTION (this << groupName);
933  IidManager::Get ()->SetGroupName (m_tid, groupName);
934  return *this;
935 }
936 TypeId
937 TypeId::SetSize (std::size_t size)
938 {
939  NS_LOG_FUNCTION (this << size);
940  IidManager::Get ()->SetSize (m_tid, size);
941  return *this;
942 }
943 TypeId
944 TypeId::GetParent (void) const
945 {
946  NS_LOG_FUNCTION (this);
947  uint16_t parent = IidManager::Get ()->GetParent (m_tid);
948  return TypeId (parent);
949 }
950 bool
951 TypeId::HasParent (void) const
952 {
953  NS_LOG_FUNCTION (this);
954  uint16_t parent = IidManager::Get ()->GetParent (m_tid);
955  return parent != m_tid;
956 }
957 bool
959 {
960  NS_LOG_FUNCTION (this << other.GetUid ());
961  TypeId tmp = *this;
962  while (tmp != other && tmp != tmp.GetParent ())
963  {
964  tmp = tmp.GetParent ();
965  }
966  return tmp == other && *this != other;
967 }
968 std::string
970 {
971  NS_LOG_FUNCTION (this);
972  std::string groupName = IidManager::Get ()->GetGroupName (m_tid);
973  return groupName;
974 }
975 
976 std::string
977 TypeId::GetName (void) const
978 {
979  NS_LOG_FUNCTION (this);
980  std::string name = IidManager::Get ()->GetName (m_tid);
981  return name;
982 }
983 
985 TypeId::GetHash (void) const
986 {
988  return hash;
989 }
990 std::size_t
991 TypeId::GetSize (void) const
992 {
993  NS_LOG_FUNCTION (this);
994  std::size_t size = IidManager::Get ()->GetSize (m_tid);
995  return size;
996 }
997 
998 bool
1000 {
1001  NS_LOG_FUNCTION (this);
1002  bool hasConstructor = IidManager::Get ()->HasConstructor (m_tid);
1003  return hasConstructor;
1004 }
1005 
1006 void
1008 {
1009  NS_LOG_FUNCTION (this << &cb);
1011 }
1012 
1013 TypeId
1014 TypeId::AddAttribute (std::string name,
1015  std::string help,
1016  const AttributeValue &initialValue,
1019  SupportLevel supportLevel,
1020  const std::string &supportMsg)
1021 {
1022  NS_LOG_FUNCTION (this << name << help <<
1023  &initialValue << accessor << checker <<
1024  supportLevel << supportMsg);
1025  IidManager::Get ()->AddAttribute (m_tid, name, help, ATTR_SGC,
1026  initialValue.Copy (), accessor, checker,
1027  supportLevel, supportMsg);
1028  return *this;
1029 }
1030 
1031 TypeId
1032 TypeId::AddAttribute (std::string name,
1033  std::string help,
1034  uint32_t flags,
1035  const AttributeValue &initialValue,
1038  SupportLevel supportLevel,
1039  const std::string &supportMsg)
1040 {
1041  NS_LOG_FUNCTION (this << name << help << flags <<
1042  &initialValue << accessor << checker <<
1043  supportLevel << supportMsg);
1044  IidManager::Get ()->AddAttribute (m_tid, name, help, flags,
1045  initialValue.Copy (), accessor, checker,
1046  supportLevel, supportMsg);
1047  return *this;
1048 }
1049 
1050 bool
1052  Ptr<const AttributeValue> initialValue)
1053 {
1054  NS_LOG_FUNCTION (this << i << initialValue);
1055  IidManager::Get ()->SetAttributeInitialValue (m_tid, i, initialValue);
1056  return true;
1057 }
1058 
1059 
1062 {
1063  NS_LOG_FUNCTION (this);
1065  return cb;
1066 }
1067 
1068 bool
1070 {
1071  NS_LOG_FUNCTION (this);
1072  bool mustHide = IidManager::Get ()->MustHideFromDocumentation (m_tid);
1073  return mustHide;
1074 }
1075 
1076 std::size_t
1078 {
1079  NS_LOG_FUNCTION (this);
1080  std::size_t n = IidManager::Get ()->GetAttributeN (m_tid);
1081  return n;
1082 }
1084 TypeId::GetAttribute (std::size_t i) const
1085 {
1086  NS_LOG_FUNCTION (this << i);
1087  return IidManager::Get ()->GetAttribute (m_tid, i);
1088 }
1089 std::string
1090 TypeId::GetAttributeFullName (std::size_t i) const
1091 {
1092  NS_LOG_FUNCTION (this << i);
1093  struct TypeId::AttributeInformation info = GetAttribute (i);
1094  return GetName () + "::" + info.name;
1095 }
1096 
1097 std::size_t
1099 {
1100  NS_LOG_FUNCTION (this);
1101  return IidManager::Get ()->GetTraceSourceN (m_tid);
1102 }
1104 TypeId::GetTraceSource (std::size_t i) const
1105 {
1106  NS_LOG_FUNCTION (this << i);
1107  return IidManager::Get ()->GetTraceSource (m_tid, i);
1108 }
1109 
1110 TypeId
1111 TypeId::AddTraceSource (std::string name,
1112  std::string help,
1114  std::string callback,
1115  SupportLevel supportLevel,
1116  const std::string &supportMsg)
1117 {
1118  NS_LOG_FUNCTION (this << name << help <<
1119  accessor << callback <<
1120  supportLevel << supportMsg);
1121  IidManager::Get ()->AddTraceSource (m_tid, name, help,
1122  accessor, callback,
1123  supportLevel, supportMsg);
1124  return *this;
1125 }
1126 
1127 TypeId
1129 {
1130  NS_LOG_FUNCTION (this);
1132  return *this;
1133 }
1134 
1137  struct TraceSourceInformation *info) const
1138 {
1139  NS_LOG_FUNCTION (this << name);
1140  TypeId tid;
1141  TypeId nextTid = *this;
1142  struct TypeId::TraceSourceInformation tmp;
1143  do
1144  {
1145  tid = nextTid;
1146  for (std::size_t i = 0; i < tid.GetTraceSourceN (); i++)
1147  {
1148  tmp = tid.GetTraceSource (i);
1149  if (tmp.name == name)
1150  {
1151  if (tmp.supportLevel == TypeId::SUPPORTED)
1152  {
1153  *info = tmp;
1154  return tmp.accessor;
1155  }
1156  else if (tmp.supportLevel == TypeId::DEPRECATED)
1157  {
1158  std::cerr << "TraceSource '" << name << "' is deprecated: "
1159  << tmp.supportMsg << std::endl;
1160  *info = tmp;
1161  return tmp.accessor;
1162  }
1163  else if (tmp.supportLevel == TypeId::OBSOLETE)
1164  {
1165  NS_FATAL_ERROR ("TraceSource '" << name <<
1166  "' is obsolete, with no fallback: " <<
1167  tmp.supportMsg);
1168  }
1169  }
1170  }
1171  nextTid = tid.GetParent ();
1172  }
1173  while (nextTid != tid);
1174  return 0;
1175 }
1176 
1178 TypeId::LookupTraceSourceByName (std::string name) const
1179 {
1180  struct TraceSourceInformation info;
1181  return LookupTraceSourceByName (name, &info);
1182 }
1183 
1184 uint16_t
1185 TypeId::GetUid (void) const
1186 {
1187  NS_LOG_FUNCTION (this);
1188  return m_tid;
1189 }
1190 void
1191 TypeId::SetUid (uint16_t uid)
1192 {
1193  NS_LOG_FUNCTION (this << uid);
1194  m_tid = uid;
1195 }
1196 
1203 std::ostream & operator << (std::ostream &os, TypeId tid)
1204 {
1205  os << tid.GetName ();
1206  return os;
1207 }
1214 std::istream & operator >> (std::istream &is, TypeId &tid)
1215 {
1216  std::string tidString;
1217  is >> tidString;
1218  bool ok = TypeId::LookupByNameFailSafe (tidString, &tid);
1219  if (!ok)
1220  {
1221  is.setstate (std::ios_base::badbit);
1222  }
1223  return is;
1224 }
1225 
1226 
1228 
1230 {
1231  return a.m_tid < b.m_tid;
1232 }
1233 
1234 } // 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:958
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:687
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
Callback< ObjectBase * > GetConstructor(void) const
Get the constructor callback.
Definition: type-id.cc:1061
std::string GetName(void) const
Get the name.
Definition: type-id.cc:977
hash_t GetHash(void) const
Get the hash.
Definition: type-id.cc:985
bool HasAttribute(uint16_t uid, std::string name)
Check if a type id has a given Attribute.
Definition: type-id.cc:616
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:1014
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:1178
bool HasConstructor(void) const
Check if this TypeId has a constructor.
Definition: type-id.cc:999
std::string help
Trace help string.
Definition: type-id.h:104
Callback template class.
Definition: callback.h:1278
bool HasTraceSource(uint16_t uid, std::string name)
Check if a type id has a given TraceSource.
Definition: type-id.cc:719
TypeId SetParent(void)
Set the parent TypeId.
Definition: type-id.h:631
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:850
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
TypeId AddTraceSource(std::string name, std::string help, Ptr< const TraceSourceAccessor > accessor, std::string callback, SupportLevel supportLevel=SUPPORTED, const std::string &supportMsg="")
Record a new TraceSource.
Definition: type-id.cc:1111
#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:476
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:876
bool HasConstructor(uint16_t uid) const
Check if a type id has a constructor Callback.
Definition: type-id.cc:593
struct TypeId::TraceSourceInformation GetTraceSource(uint16_t uid, std::size_t i) const
Get the trace source by index.
Definition: type-id.cc:788
std::size_t GetAttributeN(uint16_t uid) const
Get the number of attributes.
Definition: type-id.cc:700
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
static bool LookupByNameFailSafe(std::string name, TypeId *tid)
Get a TypeId by name.
Definition: type-id.cc:838
#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:571
bool HasParent(void) const
Check if this TypeId has a parent.
Definition: type-id.cc:951
bool LookupAttributeByName(std::string name, struct AttributeInformation *info) const
Find an Attribute by name, retrieving the associated AttributeInformation.
Definition: type-id.cc:883
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:160
uint16_t GetUid(std::string name) const
Get a type id by name.
Definition: type-id.cc:511
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:1098
bool SetAttributeInitialValue(std::size_t i, Ptr< const AttributeValue > initialValue)
Set the initial value of an Attribute.
Definition: type-id.cc:1051
TypeId GetParent(void) const
Get the parent of this TypeId.
Definition: type-id.cc:944
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:858
Hash chaining flag.
Definition: type-id.cc:357
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:160
void DoAddConstructor(Callback< ObjectBase *> callback)
Implementation for AddConstructor().
Definition: type-id.cc:1007
A template singleton.
Definition: singleton.h:63
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:137
TypeId information manager.
Definition: type-id.cc:78
uint16_t GetRegisteredN(void) const
Get the total number of type ids.
Definition: type-id.cc:603
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:537
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:554
uint16_t m_tid
The TypeId value.
Definition: type-id.h:562
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:601
bool MustHideFromDocumentation(void) const
Check if this TypeId should not be listed in documentation.
Definition: type-id.cc:1069
struct TypeId::TraceSourceInformation GetTraceSource(std::size_t i) const
Get the trace source by index.
Definition: type-id.cc:1104
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
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
void HideFromDocumentation(uint16_t uid)
Mark this type id to be excluded from documentation.
Definition: type-id.cc:490
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:563
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:930
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:1185
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:969
std::size_t GetSize(void) const
Get the size of this object.
Definition: type-id.cc:991
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:1090
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:459
void AddConstructor(uint16_t uid, Callback< ObjectBase *> callback)
Add a constructor Callback to this type id.
Definition: type-id.cc:498
uint16_t GetRegistered(uint16_t i) const
Get a type id by index.
Definition: type-id.cc:609
void SetUid(uint16_t uid)
Set the internal id of this TypeId.
Definition: type-id.cc:1191
#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:88
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:797
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:779
struct TypeId::AttributeInformation GetAttribute(uint16_t uid, std::size_t i) const
Get Attribute information by index.
Definition: type-id.cc:709
void SetParent(uint16_t uid, uint16_t parent)
Set the parent of a type id.
Definition: type-id.cc:468
TypeId::hash_t GetHash(uint16_t uid) const
Get the hash of a type id.
Definition: type-id.cc:545
std::size_t GetAttributeN(void) const
Get the number of attributes.
Definition: type-id.cc:1077
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:483
struct TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
Definition: type-id.cc:1084
TypeId SetSize(std::size_t size)
Set the size of this type.
Definition: type-id.cc:937
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:750
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:581
static uint16_t GetRegisteredN(void)
Get the number of registered TypeIds.
Definition: type-id.cc:870
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:1128
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:830
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:647