A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
type-id.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#include "type-id.h"
20
21#include "hash.h"
22#include "log.h" // NS_ASSERT and NS_LOG
23#include "singleton.h"
25
26#include <iomanip>
27#include <map>
28#include <sstream>
29#include <vector>
30
37/*********************************************************************
38 * Helper code
39 *********************************************************************/
40
41namespace ns3
42{
43
45
46// IidManager needs to be in ns3 namespace for NS_ASSERT and NS_LOG
47// to find g_log
48
78class 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() 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 TypeId::AttributeInformation GetAttribute(uint16_t uid, std::size_t i) const;
246 void AddTraceSource(uint16_t uid,
247 std::string name,
248 std::string help,
250 std::string callback,
252 const std::string& supportMsg = "");
258 std::size_t GetTraceSourceN(uint16_t uid) const;
265 TypeId::TraceSourceInformation GetTraceSource(uint16_t uid, std::size_t i) const;
271 bool MustHideFromDocumentation(uint16_t uid) const;
272
273 private:
280 bool HasTraceSource(uint16_t uid, std::string name);
287 bool HasAttribute(uint16_t uid, std::string name);
293 static TypeId::hash_t Hasher(const std::string name);
294
297 {
299 std::string name;
303 uint16_t parent;
305 std::string groupName;
307 std::size_t size;
315 std::vector<TypeId::AttributeInformation> attributes;
317 std::vector<TypeId::TraceSourceInformation> traceSources;
321 std::string supportMsg;
322 };
323
325 typedef std::vector<IidInformation>::const_iterator Iterator;
326
333
335 std::vector<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
348 enum
349 {
356 HashChainFlag = 0x80000000
357 };
358};
359
360// static
362IidManager::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"
379#define IIDL IID << ": "
380
381uint16_t
382IidManager::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, "Trying to allocate twice the same uid: " << name);
387
388 TypeId::hash_t hash = Hasher(name) & (~HashChainFlag);
389 if (m_hashmap.count(hash) == 1)
390 {
391 NS_LOG_ERROR("Hash chaining TypeId for '"
392 << name << "'. "
393 << "This is not a bug, but is extremely unlikely. "
394 << "Please contact the ns3 developers.");
395 // ns3 developer contacted about this message:
396 // You have four options (in order of difficulty):
397 // 1. Let it ride, and play the odds that a third collision
398 // never appears.
399 // 2. Change the name of the new (or old) tag, even trivially, to
400 // remove the collision.
401 // 3. Switch to 64-bit hashes.
402 // 4. Implement 2-bit (or higher) chaining.
403 //
404 // Oh, by the way, I owe you a beer, since I bet Mathieu that
405 // this would never happen.. -- Peter Barnes, LLNL
406
407 NS_ASSERT_MSG(m_hashmap.count(hash | HashChainFlag) == 0,
408 "Triplicate hash detected while chaining TypeId for '"
409 << name << "'. 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
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 uint16_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 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 information.supportLevel = TypeId::SUPPORTED;
443 m_information.push_back(information);
444 std::size_t tuid = m_information.size();
445 NS_ASSERT(tuid <= 0xffff);
446 uint16_t uid = static_cast<uint16_t>(tuid);
447
448 // Add to both maps:
449 m_namemap.insert(std::make_pair(name, uid));
450 m_hashmap.insert(std::make_pair(hash, uid));
451 NS_LOG_LOGIC(IIDL << uid);
452 return uid;
453}
454
457{
458 NS_LOG_FUNCTION(IID << uid);
459 NS_ASSERT(uid <= m_information.size() && uid != 0);
460 NS_LOG_LOGIC(IIDL << m_information[uid - 1].name);
461 return const_cast<IidInformation*>(&m_information[uid - 1]);
462}
463
464void
465IidManager::SetParent(uint16_t uid, uint16_t parent)
466{
467 NS_LOG_FUNCTION(IID << uid << parent);
468 NS_ASSERT(parent <= m_information.size());
469 IidInformation* information = LookupInformation(uid);
470 information->parent = parent;
471}
472
473void
474IidManager::SetGroupName(uint16_t uid, std::string groupName)
475{
476 NS_LOG_FUNCTION(IID << uid << groupName);
477 IidInformation* information = LookupInformation(uid);
478 information->groupName = groupName;
479}
480
481void
482IidManager::SetSize(uint16_t uid, std::size_t size)
483{
484 NS_LOG_FUNCTION(IID << uid << size);
485 IidInformation* information = LookupInformation(uid);
486 information->size = size;
487}
488
489void
491{
492 NS_LOG_FUNCTION(IID << uid);
493 IidInformation* information = LookupInformation(uid);
494 information->mustHideFromDocumentation = true;
495}
496
497void
499{
500 NS_LOG_FUNCTION(IID << uid << &callback);
501 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
510uint16_t
511IidManager::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
524uint16_t
526{
527 NS_LOG_FUNCTION(IID << hash);
528 hashmap_t::const_iterator it = m_hashmap.find(hash);
529 uint16_t uid = 0;
530 if (it != m_hashmap.end())
531 {
532 uid = it->second;
533 }
534 NS_LOG_LOGIC(IIDL << uid);
535 return uid;
536}
537
538std::string
539IidManager::GetName(uint16_t uid) const
540{
541 NS_LOG_FUNCTION(IID << uid);
542 IidInformation* information = LookupInformation(uid);
543 NS_LOG_LOGIC(IIDL << information->name);
544 return information->name;
545}
546
548IidManager::GetHash(uint16_t uid) const
549{
550 NS_LOG_FUNCTION(IID << uid);
551 IidInformation* information = LookupInformation(uid);
552 TypeId::hash_t hash = information->hash;
553 NS_LOG_LOGIC(IIDL << hash);
554 return hash;
555}
556
557uint16_t
558IidManager::GetParent(uint16_t uid) const
559{
560 NS_LOG_FUNCTION(IID << uid);
561 IidInformation* information = LookupInformation(uid);
562 uint16_t pid = information->parent;
563 NS_LOG_LOGIC(IIDL << pid);
564 return pid;
565}
566
567std::string
568IidManager::GetGroupName(uint16_t uid) const
569{
570 NS_LOG_FUNCTION(IID << uid);
571 IidInformation* information = LookupInformation(uid);
572 NS_LOG_LOGIC(IIDL << information->groupName);
573 return information->groupName;
574}
575
576std::size_t
577IidManager::GetSize(uint16_t uid) const
578{
579 NS_LOG_FUNCTION(IID << uid);
580 IidInformation* information = LookupInformation(uid);
581 std::size_t size = information->size;
582 NS_LOG_LOGIC(IIDL << size);
583 return size;
584}
585
587IidManager::GetConstructor(uint16_t uid) const
588{
589 NS_LOG_FUNCTION(IID << uid);
590 IidInformation* information = LookupInformation(uid);
591 if (!information->hasConstructor)
592 {
593 NS_FATAL_ERROR("Requested constructor for " << information->name
594 << " but it does not have one.");
595 }
596 return information->constructor;
597}
598
599bool
600IidManager::HasConstructor(uint16_t uid) const
601{
602 NS_LOG_FUNCTION(IID << uid);
603 IidInformation* information = LookupInformation(uid);
604 bool hasC = information->hasConstructor;
605 NS_LOG_LOGIC(IIDL << hasC);
606 return hasC;
607}
608
609uint16_t
611{
613 return static_cast<uint16_t>(m_information.size());
614}
615
616uint16_t
618{
619 NS_LOG_FUNCTION(IID << i);
620 return i + 1;
621}
622
623bool
624IidManager::HasAttribute(uint16_t uid, std::string name)
625{
626 NS_LOG_FUNCTION(IID << uid << name);
627 IidInformation* information = LookupInformation(uid);
628 while (true)
629 {
630 for (std::vector<TypeId::AttributeInformation>::const_iterator i =
631 information->attributes.begin();
632 i != information->attributes.end();
633 ++i)
634 {
635 if (i->name == name)
636 {
637 NS_LOG_LOGIC(IIDL << true);
638 return true;
639 }
640 }
641 IidInformation* parent = LookupInformation(information->parent);
642 if (parent == information)
643 {
644 // top of inheritance tree
645 NS_LOG_LOGIC(IIDL << false);
646 return false;
647 }
648 // check parent
649 information = parent;
650 }
651 NS_LOG_LOGIC(IIDL << false);
652 return false;
653}
654
655void
657 std::string name,
658 std::string help,
659 uint32_t flags,
660 Ptr<const AttributeValue> initialValue,
663 TypeId::SupportLevel supportLevel,
664 const std::string& supportMsg)
665{
666 NS_LOG_FUNCTION(IID << uid << name << help << flags << initialValue << accessor << checker
667 << supportLevel << supportMsg);
668 IidInformation* information = LookupInformation(uid);
669 if (name.find(' ') != std::string::npos)
670 {
671 NS_FATAL_ERROR("Attribute name \"" << name << "\" may not contain spaces ' ', "
672 << "encountered when registering TypeId \""
673 << information->name << "\"");
674 }
675 if (HasAttribute(uid, name))
676 {
677 NS_FATAL_ERROR("Attribute \"" << name << "\" already registered on tid=\""
678 << information->name << "\"");
679 }
681 info.name = name;
682 info.help = help;
683 info.flags = flags;
684 info.initialValue = initialValue;
685 info.originalInitialValue = initialValue;
686 info.accessor = accessor;
687 info.checker = checker;
688 info.supportLevel = supportLevel;
689 info.supportMsg = supportMsg;
690 information->attributes.push_back(info);
691 NS_LOG_LOGIC(IIDL << information->attributes.size() - 1);
692}
693
694void
696 std::size_t i,
697 Ptr<const AttributeValue> initialValue)
698{
699 NS_LOG_FUNCTION(IID << uid << i << initialValue);
700 IidInformation* information = LookupInformation(uid);
701 NS_ASSERT(i < information->attributes.size());
702 information->attributes[i].initialValue = initialValue;
703}
704
705std::size_t
706IidManager::GetAttributeN(uint16_t uid) const
707{
708 NS_LOG_FUNCTION(IID << uid);
709 IidInformation* information = LookupInformation(uid);
710 std::size_t size = information->attributes.size();
711 NS_LOG_LOGIC(IIDL << size);
712 return size;
713}
714
716IidManager::GetAttribute(uint16_t uid, std::size_t i) const
717{
718 NS_LOG_FUNCTION(IID << uid << i);
719 IidInformation* information = LookupInformation(uid);
720 NS_ASSERT(i < information->attributes.size());
721 NS_LOG_LOGIC(IIDL << information->name);
722 return information->attributes[i];
723}
724
725bool
726IidManager::HasTraceSource(uint16_t uid, std::string name)
727{
728 NS_LOG_FUNCTION(IID << uid << name);
729 IidInformation* information = LookupInformation(uid);
730 while (true)
731 {
732 for (std::vector<TypeId::TraceSourceInformation>::const_iterator i =
733 information->traceSources.begin();
734 i != information->traceSources.end();
735 ++i)
736 {
737 if (i->name == name)
738 {
739 NS_LOG_LOGIC(IIDL << true);
740 return true;
741 }
742 }
743 IidInformation* parent = LookupInformation(information->parent);
744 if (parent == information)
745 {
746 // top of inheritance tree
747 NS_LOG_LOGIC(IIDL << false);
748 return false;
749 }
750 // check parent
751 information = parent;
752 }
753 NS_LOG_LOGIC(IIDL << false);
754 return false;
755}
756
757void
759 std::string name,
760 std::string help,
762 std::string callback,
763 TypeId::SupportLevel supportLevel,
764 const std::string& supportMsg)
765{
766 NS_LOG_FUNCTION(IID << uid << name << help << accessor << callback << supportLevel
767 << supportMsg);
768 IidInformation* information = LookupInformation(uid);
769 if (HasTraceSource(uid, name))
770 {
771 NS_FATAL_ERROR("Trace source \"" << name << "\" already registered on tid=\""
772 << information->name << "\"");
773 }
775 source.name = name;
776 source.help = help;
777 source.accessor = accessor;
778 source.callback = callback;
779 source.supportLevel = supportLevel;
780 source.supportMsg = supportMsg;
781 information->traceSources.push_back(source);
782 NS_LOG_LOGIC(IIDL << information->traceSources.size() - 1);
783}
784
785std::size_t
787{
788 NS_LOG_FUNCTION(IID << uid);
789 IidInformation* information = LookupInformation(uid);
790 std::size_t size = information->traceSources.size();
791 NS_LOG_LOGIC(IIDL << size);
792 return size;
793}
794
796IidManager::GetTraceSource(uint16_t uid, std::size_t i) const
797{
798 NS_LOG_FUNCTION(IID << uid << i);
799 IidInformation* information = LookupInformation(uid);
800 NS_ASSERT(i < information->traceSources.size());
801 NS_LOG_LOGIC(IIDL << information->name);
802 return information->traceSources[i];
803}
804
805bool
807{
808 NS_LOG_FUNCTION(IID << uid);
809 IidInformation* information = LookupInformation(uid);
810 bool hide = information->mustHideFromDocumentation;
811 NS_LOG_LOGIC(IIDL << hide);
812 return hide;
813}
814
815} // namespace ns3
816
817namespace ns3
818{
819
820/*********************************************************************
821 * The TypeId class
822 *********************************************************************/
823
824TypeId::TypeId(const std::string& name)
825{
826 NS_LOG_FUNCTION(this << name);
827 uint16_t uid = IidManager::Get()->AllocateUid(name);
828 NS_LOG_LOGIC(uid);
829 NS_ASSERT(uid != 0);
830 m_tid = uid;
831}
832
833TypeId::TypeId(uint16_t tid)
834 : m_tid(tid)
835{
836 NS_LOG_FUNCTION(this << tid);
837}
838
839TypeId
840TypeId::LookupByName(std::string name)
841{
842 NS_LOG_FUNCTION(name);
843 uint16_t uid = IidManager::Get()->GetUid(name);
844 NS_ASSERT_MSG(uid != 0, "Assert in TypeId::LookupByName: " << name << " not found");
845 return TypeId(uid);
846}
847
848bool
850{
851 NS_LOG_FUNCTION(name << tid->GetUid());
852 uint16_t uid = IidManager::Get()->GetUid(name);
853 if (uid == 0)
854 {
855 return false;
856 }
857 *tid = TypeId(uid);
858 return true;
859}
860
861TypeId
863{
864 uint16_t uid = IidManager::Get()->GetUid(hash);
865 NS_ASSERT_MSG(uid != 0,
866 "Assert in TypeId::LookupByHash: 0x" << std::hex << hash << std::dec
867 << " not found");
868 return TypeId(uid);
869}
870
871bool
873{
874 uint16_t uid = IidManager::Get()->GetUid(hash);
875 if (uid == 0)
876 {
877 return false;
878 }
879 *tid = TypeId(uid);
880 return true;
881}
882
883uint16_t
885{
888}
889
890TypeId
892{
895}
896
897bool
899{
900 NS_LOG_FUNCTION(this << name << info);
901 TypeId tid;
902 TypeId nextTid = *this;
903 do
904 {
905 tid = nextTid;
906 for (std::size_t i = 0; i < tid.GetAttributeN(); i++)
907 {
909 if (tmp.name == name)
910 {
912 {
913 *info = tmp;
914 return true;
915 }
916 else if (tmp.supportLevel == TypeId::DEPRECATED)
917 {
918 std::cerr << "Attribute '" << name << "' is deprecated: " << tmp.supportMsg
919 << std::endl;
920 *info = tmp;
921 return true;
922 }
923 else if (tmp.supportLevel == TypeId::OBSOLETE)
924 {
925 NS_FATAL_ERROR("Attribute '" << name << "' is obsolete, with no fallback: "
926 << tmp.supportMsg);
927 }
928 }
929 }
930 nextTid = tid.GetParent();
931 } while (nextTid != tid);
932 return false;
933}
934
935TypeId
937{
938 NS_LOG_FUNCTION(this << tid.GetUid());
940 return *this;
941}
942
943TypeId
944TypeId::SetGroupName(std::string groupName)
945{
946 NS_LOG_FUNCTION(this << groupName);
947 IidManager::Get()->SetGroupName(m_tid, groupName);
948 return *this;
949}
950
951TypeId
952TypeId::SetSize(std::size_t size)
953{
954 NS_LOG_FUNCTION(this << size);
955 IidManager::Get()->SetSize(m_tid, size);
956 return *this;
957}
958
959TypeId
961{
962 NS_LOG_FUNCTION(this);
963 uint16_t parent = IidManager::Get()->GetParent(m_tid);
964 return TypeId(parent);
965}
966
967bool
969{
970 NS_LOG_FUNCTION(this);
971 uint16_t parent = IidManager::Get()->GetParent(m_tid);
972 return parent != m_tid;
973}
974
975bool
977{
978 NS_LOG_FUNCTION(this << other.GetUid());
979 TypeId tmp = *this;
980 while (tmp != other && tmp != tmp.GetParent())
981 {
982 tmp = tmp.GetParent();
983 }
984 return tmp == other && *this != other;
985}
986
987std::string
989{
990 NS_LOG_FUNCTION(this);
991 std::string groupName = IidManager::Get()->GetGroupName(m_tid);
992 return groupName;
993}
994
995std::string
997{
998 NS_LOG_FUNCTION(this);
999 std::string name = IidManager::Get()->GetName(m_tid);
1000 return name;
1001}
1002
1005{
1007 return hash;
1008}
1009
1010std::size_t
1012{
1013 NS_LOG_FUNCTION(this);
1014 std::size_t size = IidManager::Get()->GetSize(m_tid);
1015 return size;
1016}
1017
1018bool
1020{
1021 NS_LOG_FUNCTION(this);
1022 bool hasConstructor = IidManager::Get()->HasConstructor(m_tid);
1023 return hasConstructor;
1024}
1025
1026void
1028{
1029 NS_LOG_FUNCTION(this << &cb);
1031}
1032
1033TypeId
1034TypeId::AddAttribute(std::string name,
1035 std::string help,
1036 const AttributeValue& initialValue,
1039 SupportLevel supportLevel,
1040 const std::string& supportMsg)
1041{
1042 NS_LOG_FUNCTION(this << name << help << &initialValue << accessor << checker << supportLevel
1043 << supportMsg);
1045 name,
1046 help,
1047 ATTR_SGC,
1048 initialValue.Copy(),
1049 accessor,
1050 checker,
1051 supportLevel,
1052 supportMsg);
1053 return *this;
1054}
1055
1056TypeId
1057TypeId::AddAttribute(std::string name,
1058 std::string help,
1059 uint32_t flags,
1060 const AttributeValue& initialValue,
1063 SupportLevel supportLevel,
1064 const std::string& supportMsg)
1065{
1066 NS_LOG_FUNCTION(this << name << help << flags << &initialValue << accessor << checker
1067 << supportLevel << supportMsg);
1069 name,
1070 help,
1071 flags,
1072 initialValue.Copy(),
1073 accessor,
1074 checker,
1075 supportLevel,
1076 supportMsg);
1077 return *this;
1078}
1079
1080bool
1082{
1083 NS_LOG_FUNCTION(this << i << initialValue);
1084 IidManager::Get()->SetAttributeInitialValue(m_tid, i, initialValue);
1085 return true;
1086}
1087
1090{
1091 NS_LOG_FUNCTION(this);
1093 return cb;
1094}
1095
1096bool
1098{
1099 NS_LOG_FUNCTION(this);
1101 return mustHide;
1102}
1103
1104std::size_t
1106{
1107 NS_LOG_FUNCTION(this);
1108 std::size_t n = IidManager::Get()->GetAttributeN(m_tid);
1109 return n;
1110}
1111
1113TypeId::GetAttribute(std::size_t i) const
1114{
1115 NS_LOG_FUNCTION(this << i);
1116 return IidManager::Get()->GetAttribute(m_tid, i);
1117}
1118
1119std::string
1121{
1122 NS_LOG_FUNCTION(this << i);
1124 return GetName() + "::" + info.name;
1125}
1126
1127std::size_t
1129{
1130 NS_LOG_FUNCTION(this);
1132}
1133
1135TypeId::GetTraceSource(std::size_t i) const
1136{
1137 NS_LOG_FUNCTION(this << i);
1138 return IidManager::Get()->GetTraceSource(m_tid, i);
1139}
1140
1141TypeId
1142TypeId::AddTraceSource(std::string name,
1143 std::string help,
1145 std::string callback,
1146 SupportLevel supportLevel,
1147 const std::string& supportMsg)
1148{
1149 NS_LOG_FUNCTION(this << name << help << accessor << callback << supportLevel << supportMsg);
1151 ->AddTraceSource(m_tid, name, help, accessor, callback, supportLevel, supportMsg);
1152 return *this;
1153}
1154
1155TypeId
1157{
1158 NS_LOG_FUNCTION(this);
1160 return *this;
1161}
1162
1165{
1166 NS_LOG_FUNCTION(this << name);
1167 TypeId tid;
1168 TypeId nextTid = *this;
1170 do
1171 {
1172 tid = nextTid;
1173 for (std::size_t i = 0; i < tid.GetTraceSourceN(); i++)
1174 {
1175 tmp = tid.GetTraceSource(i);
1176 if (tmp.name == name)
1177 {
1179 {
1180 *info = tmp;
1181 return tmp.accessor;
1182 }
1183 else if (tmp.supportLevel == TypeId::DEPRECATED)
1184 {
1185 std::cerr << "TraceSource '" << name << "' is deprecated: " << tmp.supportMsg
1186 << std::endl;
1187 *info = tmp;
1188 return tmp.accessor;
1189 }
1190 else if (tmp.supportLevel == TypeId::OBSOLETE)
1191 {
1192 NS_FATAL_ERROR("TraceSource '" << name << "' is obsolete, with no fallback: "
1193 << tmp.supportMsg);
1194 }
1195 }
1196 }
1197 nextTid = tid.GetParent();
1198 } while (nextTid != tid);
1199 return nullptr;
1200}
1201
1203TypeId::LookupTraceSourceByName(std::string name) const
1204{
1206 return LookupTraceSourceByName(name, &info);
1207}
1208
1209uint16_t
1211{
1212 NS_LOG_FUNCTION(this);
1213 return m_tid;
1214}
1215
1216void
1217TypeId::SetUid(uint16_t uid)
1218{
1219 NS_LOG_FUNCTION(this << uid);
1220 m_tid = uid;
1221}
1222
1229std::ostream&
1230operator<<(std::ostream& os, TypeId tid)
1231{
1232 os << tid.GetName();
1233 return os;
1234}
1235
1242std::istream&
1243operator>>(std::istream& is, TypeId& tid)
1244{
1245 std::string tidString;
1246 is >> tidString;
1247 bool ok = TypeId::LookupByNameFailSafe(tidString, &tid);
1248 if (!ok)
1249 {
1250 is.setstate(std::ios_base::badbit);
1251 }
1252 return is;
1253}
1254
1256
1257bool
1259{
1260 return a.m_tid < b.m_tid;
1261}
1262
1263} // namespace ns3
Hold a value for an Attribute.
Definition: attribute.h:70
virtual Ptr< AttributeValue > Copy() const =0
Callback template class.
Definition: callback.h:438
Generic Hash function interface.
Definition: hash.h:87
uint32_t GetHash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer.
Definition: hash.h:236
Hasher & clear()
Restore initial state.
Definition: hash.cc:56
TypeId information manager.
Definition: type-id.cc:79
@ HashChainFlag
Hash chaining flag.
Definition: type-id.cc:356
TypeId::hash_t GetHash(uint16_t uid) const
Get the hash of a type id.
Definition: type-id.cc:548
std::map< TypeId::hash_t, uint16_t > hashmap_t
Type of the by-hash index.
Definition: type-id.cc:343
std::vector< IidInformation >::const_iterator Iterator
Iterator type.
Definition: type-id.cc:325
uint16_t GetRegisteredN() const
Get the total number of type ids.
Definition: type-id.cc:610
uint16_t GetParent(uint16_t uid) const
Get the parent of a type id.
Definition: type-id.cc:558
std::map< std::string, uint16_t > namemap_t
Type of the by-name index.
Definition: type-id.cc:338
void SetAttributeInitialValue(uint16_t uid, std::size_t i, Ptr< const AttributeValue > initialValue)
Set the initial value of an Attribute.
Definition: type-id.cc:695
void SetGroupName(uint16_t uid, std::string groupName)
Set the group name of a type id.
Definition: type-id.cc:474
void SetParent(uint16_t uid, uint16_t parent)
Set the parent of a type id.
Definition: type-id.cc:465
std::vector< IidInformation > m_information
The container of all type id records.
Definition: type-id.cc:335
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
std::string GetGroupName(uint16_t uid) const
Get the group name of a type id.
Definition: type-id.cc:568
bool HasTraceSource(uint16_t uid, std::string name)
Check if a type id has a given TraceSource.
Definition: type-id.cc:726
uint16_t GetRegistered(uint16_t i) const
Get a type id by index.
Definition: type-id.cc:617
Callback< ObjectBase * > GetConstructor(uint16_t uid) const
Get the constructor Callback of a type id.
Definition: type-id.cc:587
std::size_t GetAttributeN(uint16_t uid) const
Get the number of attributes.
Definition: type-id.cc:706
uint16_t GetUid(std::string name) const
Get a type id by name.
Definition: type-id.cc:511
void AddConstructor(uint16_t uid, Callback< ObjectBase * > callback)
Add a constructor Callback to this type id.
Definition: type-id.cc:498
void HideFromDocumentation(uint16_t uid)
Mark this type id to be excluded from documentation.
Definition: type-id.cc:490
std::string GetName(uint16_t uid) const
Get the name of a type id.
Definition: type-id.cc:539
std::size_t GetTraceSourceN(uint16_t uid) const
Get the number of Trace sources.
Definition: type-id.cc:786
std::size_t GetSize(uint16_t uid) const
Get the size of a type id.
Definition: type-id.cc:577
TypeId::TraceSourceInformation GetTraceSource(uint16_t uid, std::size_t i) const
Get the trace source by index.
Definition: type-id.cc:796
bool MustHideFromDocumentation(uint16_t uid) const
Check if this TypeId should not be listed in documentation.
Definition: type-id.cc:806
IidManager::IidInformation * LookupInformation(uint16_t uid) const
Retrieve the information record for a type.
Definition: type-id.cc:456
bool HasConstructor(uint16_t uid) const
Check if a type id has a constructor Callback.
Definition: type-id.cc:600
bool HasAttribute(uint16_t uid, std::string name)
Check if a type id has a given Attribute.
Definition: type-id.cc:624
static TypeId::hash_t Hasher(const std::string name)
Hashing function.
Definition: type-id.cc:362
hashmap_t m_hashmap
The by-hash index.
Definition: type-id.cc:345
uint16_t AllocateUid(std::string name)
Create a new unique type id.
Definition: type-id.cc:382
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:656
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:758
TypeId::AttributeInformation GetAttribute(uint16_t uid, std::size_t i) const
Get Attribute information by index.
Definition: type-id.cc:716
namemap_t m_namemap
The by-name index.
Definition: type-id.cc:340
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
A template singleton.
Definition: singleton.h:61
static IidManager * Get()
Get a pointer to the singleton instance.
Definition: singleton.h:100
a unique identifier for an interface.
Definition: type-id.h:59
bool IsChildOf(TypeId other) const
Check if this TypeId is a child of another.
Definition: type-id.cc:976
std::size_t GetTraceSourceN() const
Get the number of Trace sources.
Definition: type-id.cc:1128
bool SetAttributeInitialValue(std::size_t i, Ptr< const AttributeValue > initialValue)
Set the initial value of an Attribute.
Definition: type-id.cc:1081
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:840
bool HasParent() const
Check if this TypeId has a parent.
Definition: type-id.cc:968
TypeId SetSize(std::size_t size)
Set the size of this type.
Definition: type-id.cc:952
hash_t GetHash() const
Get the hash.
Definition: type-id.cc:1004
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:1142
bool MustHideFromDocumentation() const
Check if this TypeId should not be listed in documentation.
Definition: type-id.cc:1097
@ ATTR_SGC
The attribute can be read, and written at any time.
Definition: type-id.h:67
static bool LookupByHashFailSafe(hash_t hash, TypeId *tid)
Get a TypeId by hash.
Definition: type-id.cc:872
TypeId::TraceSourceInformation GetTraceSource(std::size_t i) const
Get the trace source by index.
Definition: type-id.cc:1135
std::string GetGroupName() const
Get the group name.
Definition: type-id.cc:988
TypeId HideFromDocumentation()
Hide this TypeId from documentation.
Definition: type-id.cc:1156
Callback< ObjectBase * > GetConstructor() const
Get the constructor callback.
Definition: type-id.cc:1089
static uint16_t GetRegisteredN()
Get the number of registered TypeIds.
Definition: type-id.cc:884
std::string GetAttributeFullName(std::size_t i) const
Get the Attribute name by index.
Definition: type-id.cc:1120
bool HasConstructor() const
Check if this TypeId has a constructor.
Definition: type-id.cc:1019
std::size_t GetAttributeN() const
Get the number of attributes.
Definition: type-id.cc:1105
TypeId GetParent() const
Get the parent of this TypeId.
Definition: type-id.cc:960
void SetUid(uint16_t uid)
Set the internal id of this TypeId.
Definition: type-id.cc:1217
uint16_t m_tid
The TypeId value.
Definition: type-id.h:565
TypeId SetGroupName(std::string groupName)
Set the group name.
Definition: type-id.cc:944
static TypeId LookupByHash(hash_t hash)
Get a TypeId by hash.
Definition: type-id.cc:862
static TypeId GetRegistered(uint16_t i)
Get a TypeId by index.
Definition: type-id.cc:891
std::size_t GetSize() const
Get the size of this object.
Definition: type-id.cc:1011
Ptr< const TraceSourceAccessor > LookupTraceSourceByName(std::string name) const
Find a TraceSource by name.
Definition: type-id.cc:1203
uint32_t hash_t
Type of hash values.
Definition: type-id.h:120
TypeId()
Default constructor.
Definition: type-id.h:605
TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
Definition: type-id.cc:1113
uint16_t GetUid() const
Get the internal id of this TypeId.
Definition: type-id.cc:1210
static bool LookupByNameFailSafe(std::string name, TypeId *tid)
Get a TypeId by name.
Definition: type-id.cc:849
SupportLevel
The level of support or deprecation for attributes or trace sources.
Definition: type-id.h:73
@ SUPPORTED
Attribute or trace source is currently used.
Definition: type-id.h:74
@ OBSOLETE
Attribute or trace source is not used anymore; simulation fails.
Definition: type-id.h:76
@ DEPRECATED
Attribute or trace source is deprecated; user is warned.
Definition: type-id.h:75
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:1034
bool LookupAttributeByName(std::string name, AttributeInformation *info) const
Find an Attribute by name, retrieving the associated AttributeInformation.
Definition: type-id.cc:898
std::string GetName() const
Get the name.
Definition: type-id.cc:996
TypeId SetParent()
Set the parent TypeId.
Definition: type-id.h:644
void DoAddConstructor(Callback< ObjectBase * > callback)
Implementation for AddConstructor().
Definition: type-id.cc:1027
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#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:86
#define ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:254
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define IIDL
Definition: type-id.cc:379
#define IID
Definition: type-id.cc:373
ns3::Hasher, ns3::Hash32() and ns3::Hash64() function declarations.
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:129
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:153
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:170
ns3::Singleton declaration and template implementation.
The information record about a single type id.
Definition: type-id.cc:297
std::string groupName
The group name.
Definition: type-id.cc:305
uint16_t parent
The parent type id.
Definition: type-id.cc:303
std::vector< TypeId::TraceSourceInformation > traceSources
The container of TraceSources.
Definition: type-id.cc:317
std::size_t size
The size of the object represented by this type id.
Definition: type-id.cc:307
std::string supportMsg
Support message.
Definition: type-id.cc:321
std::string name
The type id name.
Definition: type-id.cc:299
std::vector< TypeId::AttributeInformation > attributes
The container of Attributes.
Definition: type-id.cc:315
Callback< ObjectBase * > constructor
The constructor Callback.
Definition: type-id.cc:311
bool hasConstructor
true if a constructor Callback has been registered.
Definition: type-id.cc:309
bool mustHideFromDocumentation
true if this type should be omitted from documentation.
Definition: type-id.cc:313
TypeId::hash_t hash
The type id hash value.
Definition: type-id.cc:301
TypeId::SupportLevel supportLevel
Support level/deprecation.
Definition: type-id.cc:319
Attribute implementation.
Definition: type-id.h:81
Ptr< const AttributeValue > originalInitialValue
Default initial value.
Definition: type-id.h:89
TypeId::SupportLevel supportLevel
Support level/deprecation.
Definition: type-id.h:97
std::string name
Attribute name.
Definition: type-id.h:83
Ptr< const AttributeAccessor > accessor
Accessor object.
Definition: type-id.h:93
uint32_t flags
AttributeFlags value.
Definition: type-id.h:87
Ptr< const AttributeChecker > checker
Checker object.
Definition: type-id.h:95
std::string supportMsg
Support message.
Definition: type-id.h:99
Ptr< const AttributeValue > initialValue
Configured initial value.
Definition: type-id.h:91
std::string help
Attribute help string.
Definition: type-id.h:85
TraceSource implementation.
Definition: type-id.h:104
std::string name
Trace name.
Definition: type-id.h:106
std::string supportMsg
Support message.
Definition: type-id.h:116
std::string help
Trace help string.
Definition: type-id.h:108
Ptr< const TraceSourceAccessor > accessor
Trace accessor.
Definition: type-id.h:112
std::string callback
Callback function signature type.
Definition: type-id.h:110
TypeId::SupportLevel supportLevel
Support level/deprecation.
Definition: type-id.h:114
ns3::TraceSourceAccessor and ns3::MakeTraceSourceAccessor declarations.
ns3::TypeId declaration; inline and template implementations.