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"
25
26#include <map>
27#include <vector>
28#include <sstream>
29#include <iomanip>
30
38/*********************************************************************
39 * Helper code
40 *********************************************************************/
41
42namespace ns3 {
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{
80public:
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;
246 void AddTraceSource (uint16_t uid,
247 std::string name,
248 std::string help,
249 Ptr<const TraceSourceAccessor> accessor,
250 std::string callback,
251 TypeId::SupportLevel supportLevel = TypeId::SUPPORTED,
252 const std::string &supportMsg = "");
258 std::size_t GetTraceSourceN (uint16_t uid) const;
265 struct TypeId::TraceSourceInformation GetTraceSource (uint16_t uid, std::size_t i) const;
271 bool MustHideFromDocumentation (uint16_t uid) const;
272
273private:
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<struct TypeId::AttributeInformation> attributes;
317 std::vector<struct TypeId::TraceSourceInformation> traceSources;
321 std::string supportMsg;
322 };
324 typedef std::vector<struct IidInformation>::const_iterator Iterator;
325
331 struct IidManager::IidInformation * LookupInformation (uint16_t uid) const;
332
334 std::vector<struct IidInformation> m_information;
335
337 typedef std::map<std::string, uint16_t> namemap_t;
340
342 typedef std::map<TypeId::hash_t, uint16_t> hashmap_t;
345
346
348 enum
349 {
356 HashChainFlag = 0x80000000
357 };
358};
359
360
361//static
363IidManager::Hasher (const std::string name)
364{
365 static ns3::Hasher hasher ( Create<Hash::Function::Murmur3> () );
366 return hasher.clear ().GetHash32 (name);
367}
368
374#define IID "IidManager"
380#define IIDL IID << ": "
381
382uint16_t
383IidManager::AllocateUid (std::string name)
384{
385 NS_LOG_FUNCTION (IID << name);
386 // Type names are definitive: equal names are equal types
387 NS_ASSERT_MSG (m_namemap.count (name) == 0,
388 "Trying to allocate twice the same uid: " << name);
389
390 TypeId::hash_t hash = Hasher (name) & (~HashChainFlag);
391 if (m_hashmap.count (hash) == 1)
392 {
393 NS_LOG_ERROR ("Hash chaining TypeId for '" << name << "'. "
394 << "This is not a bug, but is extremely unlikely. "
395 << "Please contact the ns3 developers.");
396 // ns3 developer contacted about this message:
397 // You have four options (in order of difficulty):
398 // 1. Let it ride, and play the odds that a third collision
399 // never appears.
400 // 2. Change the name of the new (or old) tag, even trivially, to
401 // remove the collision.
402 // 3. Switch to 64-bit hashes.
403 // 4. Implement 2-bit (or higher) chaining.
404 //
405 // Oh, by the way, I owe you a beer, since I bet Mathieu that
406 // this would never happen.. -- Peter Barnes, LLNL
407
408 NS_ASSERT_MSG (m_hashmap.count (hash | HashChainFlag) == 0,
409 "Triplicate hash detected while chaining TypeId for '"
410 << name
411 << "'. Please contact the ns3 developers for assistance.");
412 // ns3 developer contacted about this message:
413 // You have three options: #2-4 above.
414 //
415 // Oh, by the way, I have no idea how this crazy hashing idea got
416 // into ns3. -- Peter Barnes, LLNL
417
418 // Alphabetize the two types, so it's deterministic
419 struct IidInformation * hinfo = LookupInformation (GetUid (hash));
420 if (name > hinfo->name)
421 { // new type gets chained
422 NS_LOG_LOGIC (IIDL << "New TypeId '" << name << "' getting chained.");
423 hash = hash | HashChainFlag;
424 }
425 else
426 { // chain old type
427 NS_LOG_LOGIC (IIDL << "Old TypeId '" << hinfo->name << "' getting chained.");
428 uint16_t oldUid = GetUid (hinfo->hash);
429 m_hashmap.erase (m_hashmap.find (hinfo->hash));
430 hinfo->hash = hash | HashChainFlag;
431 m_hashmap.insert (std::make_pair (hinfo->hash, oldUid));
432 // leave new hash unchained
433 }
434 }
435
436 struct IidInformation information;
437 information.name = name;
438 information.hash = hash;
439 information.parent = 0;
440 information.groupName = "";
441 information.size = (std::size_t)(-1);
442 information.hasConstructor = false;
443 information.mustHideFromDocumentation = false;
444 information.supportLevel = TypeId::SUPPORTED;
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
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
466void
467IidManager::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}
474void
475IidManager::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}
481void
482IidManager::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}
488void
490{
491 NS_LOG_FUNCTION (IID << uid);
492 struct IidInformation *information = LookupInformation (uid);
493 information->mustHideFromDocumentation = true;
494}
495
496void
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
509uint16_t
510IidManager::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}
522uint16_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}
535std::string
536IidManager::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}
544IidManager::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}
552uint16_t
553IidManager::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}
561std::string
562IidManager::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}
569std::size_t
570IidManager::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
580IidManager::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
591bool
592IidManager::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
601uint16_t
603{
604 NS_LOG_FUNCTION (IID << m_information.size ());
605 return static_cast<uint16_t> (m_information.size ());
606}
607uint16_t
609{
610 NS_LOG_FUNCTION (IID << i);
611 return i + 1;
612}
613
614bool
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
645void
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 }
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}
685void
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
698std::size_t
699IidManager::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}
708IidManager::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
717bool
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
748void
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}
777std::size_t
778IidManager::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}
787IidManager::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}
795bool
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
807namespace ns3 {
808
809/*********************************************************************
810 * The TypeId class
811 *********************************************************************/
812
813TypeId::TypeId (const std::string &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
823TypeId::TypeId (uint16_t tid)
824 : m_tid (tid)
825{
826 NS_LOG_FUNCTION (this << tid);
827}
828TypeId
829TypeId::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}
836bool
837TypeId::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}
848TypeId
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}
856bool
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
868uint16_t
870{
872 return IidManager::Get ()->GetRegisteredN ();
873}
874TypeId
876{
877 NS_LOG_FUNCTION (i);
878 return TypeId (IidManager::Get ()->GetRegistered (i));
879}
880
881bool
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 {
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
921TypeId
923{
924 NS_LOG_FUNCTION (this << tid.GetUid ());
926 return *this;
927}
928TypeId
929TypeId::SetGroupName (std::string groupName)
930{
931 NS_LOG_FUNCTION (this << groupName);
932 IidManager::Get ()->SetGroupName (m_tid, groupName);
933 return *this;
934}
935TypeId
936TypeId::SetSize (std::size_t size)
937{
938 NS_LOG_FUNCTION (this << size);
939 IidManager::Get ()->SetSize (m_tid, size);
940 return *this;
941}
942TypeId
944{
945 NS_LOG_FUNCTION (this);
946 uint16_t parent = IidManager::Get ()->GetParent (m_tid);
947 return TypeId (parent);
948}
949bool
951{
952 NS_LOG_FUNCTION (this);
953 uint16_t parent = IidManager::Get ()->GetParent (m_tid);
954 return parent != m_tid;
955}
956bool
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}
967std::string
969{
970 NS_LOG_FUNCTION (this);
971 std::string groupName = IidManager::Get ()->GetGroupName (m_tid);
972 return groupName;
973}
974
975std::string
976TypeId::GetName (void) const
977{
978 NS_LOG_FUNCTION (this);
979 std::string name = IidManager::Get ()->GetName (m_tid);
980 return name;
981}
982
984TypeId::GetHash (void) const
985{
986 hash_t hash = IidManager::Get ()->GetHash (m_tid);
987 return hash;
988}
989std::size_t
990TypeId::GetSize (void) const
991{
992 NS_LOG_FUNCTION (this);
993 std::size_t size = IidManager::Get ()->GetSize (m_tid);
994 return size;
995}
996
997bool
999{
1000 NS_LOG_FUNCTION (this);
1001 bool hasConstructor = IidManager::Get ()->HasConstructor (m_tid);
1002 return hasConstructor;
1003}
1004
1005void
1007{
1008 NS_LOG_FUNCTION (this << &cb);
1010}
1011
1012TypeId
1013TypeId::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
1030TypeId
1031TypeId::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
1049bool
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
1067bool
1069{
1070 NS_LOG_FUNCTION (this);
1071 bool mustHide = IidManager::Get ()->MustHideFromDocumentation (m_tid);
1072 return mustHide;
1073}
1074
1075std::size_t
1077{
1078 NS_LOG_FUNCTION (this);
1079 std::size_t n = IidManager::Get ()->GetAttributeN (m_tid);
1080 return n;
1081}
1083TypeId::GetAttribute (std::size_t i) const
1084{
1085 NS_LOG_FUNCTION (this << i);
1086 return IidManager::Get ()->GetAttribute (m_tid, i);
1087}
1088std::string
1089TypeId::GetAttributeFullName (std::size_t i) const
1090{
1091 NS_LOG_FUNCTION (this << i);
1093 return GetName () + "::" + info.name;
1094}
1095
1096std::size_t
1098{
1099 NS_LOG_FUNCTION (this);
1101}
1103TypeId::GetTraceSource (std::size_t i) const
1104{
1105 NS_LOG_FUNCTION (this << i);
1106 return IidManager::Get ()->GetTraceSource (m_tid, i);
1107}
1108
1109TypeId
1110TypeId::AddTraceSource (std::string name,
1111 std::string help,
1113 std::string callback,
1114 SupportLevel supportLevel,
1115 const std::string &supportMsg)
1116{
1117 NS_LOG_FUNCTION (this << name << help <<
1118 accessor << callback <<
1119 supportLevel << supportMsg);
1120 IidManager::Get ()->AddTraceSource (m_tid, name, help,
1121 accessor, callback,
1122 supportLevel, supportMsg);
1123 return *this;
1124}
1125
1126TypeId
1128{
1129 NS_LOG_FUNCTION (this);
1131 return *this;
1132}
1133
1136 struct TraceSourceInformation *info) const
1137{
1138 NS_LOG_FUNCTION (this << name);
1139 TypeId tid;
1140 TypeId nextTid = *this;
1142 do
1143 {
1144 tid = nextTid;
1145 for (std::size_t i = 0; i < tid.GetTraceSourceN (); i++)
1146 {
1147 tmp = tid.GetTraceSource (i);
1148 if (tmp.name == name)
1149 {
1151 {
1152 *info = tmp;
1153 return tmp.accessor;
1154 }
1155 else if (tmp.supportLevel == TypeId::DEPRECATED)
1156 {
1157 std::cerr << "TraceSource '" << name << "' is deprecated: "
1158 << tmp.supportMsg << std::endl;
1159 *info = tmp;
1160 return tmp.accessor;
1161 }
1162 else if (tmp.supportLevel == TypeId::OBSOLETE)
1163 {
1164 NS_FATAL_ERROR ("TraceSource '" << name <<
1165 "' is obsolete, with no fallback: " <<
1166 tmp.supportMsg);
1167 }
1168 }
1169 }
1170 nextTid = tid.GetParent ();
1171 }
1172 while (nextTid != tid);
1173 return 0;
1174}
1175
1177TypeId::LookupTraceSourceByName (std::string name) const
1178{
1179 struct TraceSourceInformation info;
1180 return LookupTraceSourceByName (name, &info);
1181}
1182
1183uint16_t
1184TypeId::GetUid (void) const
1185{
1186 NS_LOG_FUNCTION (this);
1187 return m_tid;
1188}
1189void
1190TypeId::SetUid (uint16_t uid)
1191{
1192 NS_LOG_FUNCTION (this << uid);
1193 m_tid = uid;
1194}
1195
1202std::ostream & operator << (std::ostream &os, TypeId tid)
1203{
1204 os << tid.GetName ();
1205 return os;
1206}
1213std::istream & operator >> (std::istream &is, TypeId &tid)
1214{
1215 std::string tidString;
1216 is >> tidString;
1217 bool ok = TypeId::LookupByNameFailSafe (tidString, &tid);
1218 if (!ok)
1219 {
1220 is.setstate (std::ios_base::badbit);
1221 }
1222 return is;
1223}
1224
1225
1227
1229{
1230 return a.m_tid < b.m_tid;
1231}
1232
1233} // namespace ns3
Hold a value for an Attribute.
Definition: attribute.h:69
virtual Ptr< AttributeValue > Copy(void) const =0
Callback template class.
Definition: callback.h:1279
Generic Hash function interface.
Definition: hash.h:88
uint32_t GetHash32(const char *buffer, const std::size_t size)
Compute 32-bit hash of a byte buffer.
Definition: hash.h:239
Hasher & clear(void)
Restore initial state.
Definition: hash.cc:55
TypeId information manager.
Definition: type-id.cc:79
@ HashChainFlag
Hash chaining flag.
Definition: type-id.cc:356
uint16_t GetRegisteredN(void) const
Get the total number of type ids.
Definition: type-id.cc:602
TypeId::hash_t GetHash(uint16_t uid) const
Get the hash of a type id.
Definition: type-id.cc:544
std::map< TypeId::hash_t, uint16_t > hashmap_t
Type of the by-hash index.
Definition: type-id.cc:342
uint16_t GetParent(uint16_t uid) const
Get the parent of a type id.
Definition: type-id.cc:553
std::map< std::string, uint16_t > namemap_t
Type of the by-name index.
Definition: type-id.cc:337
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
void SetGroupName(uint16_t uid, std::string groupName)
Set the group name of a type id.
Definition: type-id.cc:475
void SetParent(uint16_t uid, uint16_t parent)
Set the parent of a type id.
Definition: type-id.cc:467
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:562
bool HasTraceSource(uint16_t uid, std::string name)
Check if a type id has a given TraceSource.
Definition: type-id.cc:718
uint16_t GetRegistered(uint16_t i) const
Get a type id by index.
Definition: type-id.cc:608
Callback< ObjectBase * > GetConstructor(uint16_t uid) const
Get the constructor Callback of a type id.
Definition: type-id.cc:580
std::size_t GetAttributeN(uint16_t uid) const
Get the number of attributes.
Definition: type-id.cc:699
uint16_t GetUid(std::string name) const
Get a type id by name.
Definition: type-id.cc:510
std::vector< structIidInformation >::const_iterator Iterator
Iterator type.
Definition: type-id.cc:324
struct TypeId::TraceSourceInformation GetTraceSource(uint16_t uid, std::size_t i) const
Get the trace source by index.
Definition: type-id.cc:787
void AddConstructor(uint16_t uid, Callback< ObjectBase * > callback)
Add a constructor Callback to this type id.
Definition: type-id.cc:497
void HideFromDocumentation(uint16_t uid)
Mark this type id to be excluded from documentation.
Definition: type-id.cc:489
std::string GetName(uint16_t uid) const
Get the name of a type id.
Definition: type-id.cc:536
std::vector< struct IidInformation > m_information
The container of all type id records.
Definition: type-id.cc:334
std::size_t GetTraceSourceN(uint16_t uid) const
Get the number of Trace sources.
Definition: type-id.cc:778
std::size_t GetSize(uint16_t uid) const
Get the size of a type id.
Definition: type-id.cc:570
struct IidManager::IidInformation * LookupInformation(uint16_t uid) const
Retrieve the information record for a type.
Definition: type-id.cc:458
struct TypeId::AttributeInformation GetAttribute(uint16_t uid, std::size_t i) const
Get Attribute information by index.
Definition: type-id.cc:708
bool MustHideFromDocumentation(uint16_t uid) const
Check if this TypeId should not be listed in documentation.
Definition: type-id.cc:796
bool HasConstructor(uint16_t uid) const
Check if a type id has a constructor Callback.
Definition: type-id.cc:592
bool HasAttribute(uint16_t uid, std::string name)
Check if a type id has a given Attribute.
Definition: type-id.cc:615
static TypeId::hash_t Hasher(const std::string name)
Hashing function.
Definition: type-id.cc:363
hashmap_t m_hashmap
The by-hash index.
Definition: type-id.cc:344
uint16_t AllocateUid(std::string name)
Create a new unique type id.
Definition: type-id.cc:383
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
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
namemap_t m_namemap
The by-name index.
Definition: type-id.cc:339
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
A template singleton.
Definition: singleton.h:61
static IidManager * Get(void)
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:957
bool SetAttributeInitialValue(std::size_t i, Ptr< const AttributeValue > initialValue)
Set the initial value of an Attribute.
Definition: type-id.cc:1050
std::size_t GetTraceSourceN(void) const
Get the number of Trace sources.
Definition: type-id.cc:1097
TypeId SetParent(void)
Set the parent TypeId.
Definition: type-id.h:631
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:829
std::size_t GetAttributeN(void) const
Get the number of attributes.
Definition: type-id.cc:1076
TypeId SetSize(std::size_t size)
Set the size of this type.
Definition: type-id.cc:936
bool HasParent(void) const
Check if this TypeId has a parent.
Definition: type-id.cc:950
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:1110
struct TypeId::TraceSourceInformation GetTraceSource(std::size_t i) const
Get the trace source by index.
Definition: type-id.cc:1103
@ 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:857
static uint16_t GetRegisteredN(void)
Get the number of registered TypeIds.
Definition: type-id.cc:869
uint16_t GetUid(void) const
Get the internal id of this TypeId.
Definition: type-id.cc:1184
hash_t GetHash(void) const
Get the hash.
Definition: type-id.cc:984
Callback< ObjectBase * > GetConstructor(void) const
Get the constructor callback.
Definition: type-id.cc:1060
std::string GetAttributeFullName(std::size_t i) const
Get the Attribute name by index.
Definition: type-id.cc:1089
struct TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
Definition: type-id.cc:1083
std::string GetGroupName(void) const
Get the group name.
Definition: type-id.cc:968
void SetUid(uint16_t uid)
Set the internal id of this TypeId.
Definition: type-id.cc:1190
uint16_t m_tid
The TypeId value.
Definition: type-id.h:562
TypeId SetGroupName(std::string groupName)
Set the group name.
Definition: type-id.cc:929
static TypeId LookupByHash(hash_t hash)
Get a TypeId by hash.
Definition: type-id.cc:849
static TypeId GetRegistered(uint16_t i)
Get a TypeId by index.
Definition: type-id.cc:875
bool HasConstructor(void) const
Check if this TypeId has a constructor.
Definition: type-id.cc:998
Ptr< const TraceSourceAccessor > LookupTraceSourceByName(std::string name) const
Find a TraceSource by name.
Definition: type-id.cc:1177
TypeId HideFromDocumentation(void)
Hide this TypeId from documentation.
Definition: type-id.cc:1127
bool MustHideFromDocumentation(void) const
Check if this TypeId should not be listed in documentation.
Definition: type-id.cc:1068
uint32_t hash_t
Type of hash values.
Definition: type-id.h:116
TypeId()
Default constructor.
Definition: type-id.h:601
TypeId GetParent(void) const
Get the parent of this TypeId.
Definition: type-id.cc:943
std::string GetName(void) const
Get the name.
Definition: type-id.cc:976
std::size_t GetSize(void) const
Get the size of this object.
Definition: type-id.cc:990
bool LookupAttributeByName(std::string name, struct AttributeInformation *info) const
Find an Attribute by name, retrieving the associated AttributeInformation.
Definition: type-id.cc:882
static bool LookupByNameFailSafe(std::string name, TypeId *tid)
Get a TypeId by name.
Definition: type-id.cc:837
SupportLevel
The level of support or deprecation for attributes or trace sources.
Definition: type-id.h:71
@ SUPPORTED
Attribute or trace source is currently used.
Definition: type-id.h:72
@ OBSOLETE
Attribute or trace source is not used anymore; simulation fails.
Definition: type-id.h:74
@ DEPRECATED
Attribute or trace source is deprecated; user is warned.
Definition: type-id.h:73
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
void DoAddConstructor(Callback< ObjectBase * > callback)
Implementation for AddConstructor().
Definition: type-id.cc:1006
#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
#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
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:257
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#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:380
#define IID
Definition: type-id.cc:374
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:139
ATTRIBUTE_HELPER_CPP(Length)
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:162
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:176
STL namespace.
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< struct TypeId::AttributeInformation > attributes
The container of Attributes.
Definition: type-id.cc:315
Callback< ObjectBase * > constructor
The constructor Callback.
Definition: type-id.cc:311
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
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
std::vector< struct TypeId::TraceSourceInformation > traceSources
The container of TraceSources.
Definition: type-id.cc:317
TypeId::SupportLevel supportLevel
Support level/deprecation.
Definition: type-id.cc:319
Attribute implementation.
Definition: type-id.h:78
Ptr< const AttributeValue > originalInitialValue
Default initial value.
Definition: type-id.h:86
TypeId::SupportLevel supportLevel
Support level/deprecation.
Definition: type-id.h:94
std::string name
Attribute name.
Definition: type-id.h:80
Ptr< const AttributeAccessor > accessor
Accessor object.
Definition: type-id.h:90
uint32_t flags
AttributeFlags value.
Definition: type-id.h:84
Ptr< const AttributeChecker > checker
Checker object.
Definition: type-id.h:92
std::string supportMsg
Support message.
Definition: type-id.h:96
Ptr< const AttributeValue > initialValue
Configured initial value.
Definition: type-id.h:88
std::string help
Attribute help string.
Definition: type-id.h:82
TraceSource implementation.
Definition: type-id.h:100
std::string name
Trace name.
Definition: type-id.h:102
std::string supportMsg
Support message.
Definition: type-id.h:112
std::string help
Trace help string.
Definition: type-id.h:104
Ptr< const TraceSourceAccessor > accessor
Trace accessor.
Definition: type-id.h:108
std::string callback
Callback function signature type.
Definition: type-id.h:106
TypeId::SupportLevel supportLevel
Support level/deprecation.
Definition: type-id.h:110
ns3::TraceSourceAccessor and ns3::MakeTraceSourceAccessor declarations.
ns3::TypeId declaration; inline and template implementations.