diff --git a/src/core/component-manager.cc b/src/core/component-manager.cc index 1c8b58423..473f81a56 100644 --- a/src/core/component-manager.cc +++ b/src/core/component-manager.cc @@ -39,7 +39,7 @@ ClassId::ClassId (uint32_t classId) {} std::string -ClassId::GetName (void) +ClassId::GetName (void) const { return Singleton::Get ()->LookupByUid (m_classId); } @@ -79,38 +79,147 @@ ComponentManager::LookupByName (std::string name) { return ClassId (Singleton::Get ()->LookupByName (name)); } +ClassId +ComponentManager::LookupByName (std::string name, bool *ok) +{ + uint32_t cid = Singleton::Get ()->LookupByName (name); + if (cid == 0) + { + *ok = false; + } + else + { + *ok = true; + } + return ClassId (cid); +} +std::vector +ComponentManager::LookupByInterfaceId (InterfaceId iid) +{ + std::vector classIdList; + List *list = Singleton::Get (); + for (List::const_iterator i = list->begin (); i != list->end (); i++) + { + for (std::vector::const_iterator j = i->m_supportedInterfaces.begin (); + j != i->m_supportedInterfaces.end (); j++) + { + if (*(*j) == iid) + { + classIdList.push_back (i->m_classId); + break; + } + } + } + unique (classIdList.begin (), classIdList.end ()); + return classIdList; +} void ComponentManager::Register (ClassId classId, CallbackBase *callback, - std::vector supportedInterfaces) + std::vector supportedInterfaces) { List *list = Singleton::Get (); struct ClassIdEntry entry = ClassIdEntry (classId); entry.m_callback = callback; bool foundObject = false; - for (std::vector::iterator i = supportedInterfaces.begin (); + for (std::vector::iterator i = supportedInterfaces.begin (); i != supportedInterfaces.end (); i++) { - if (*i == Object::iid) + if (*(*i) == Object::iid) { foundObject = true; } } if (!foundObject) { - supportedInterfaces.push_back (Object::iid); + supportedInterfaces.push_back (&Object::iid); } entry.m_supportedInterfaces = supportedInterfaces; list->push_back (entry); } void -RegisterCallback (ClassId classId, CallbackBase *callback, std::vector supportedInterfaces) +RegisterCallback (ClassId classId, CallbackBase *callback, std::vector supportedInterfaces) { return ComponentManager::Register (classId, callback, supportedInterfaces); } +ClassIdDefaultValue::ClassIdDefaultValue (std::string name, + std::string help, + const InterfaceId &iid, + std::string defaultValue) + : DefaultValueBase (name, help), + m_defaultName (defaultValue), + m_name (defaultValue), + m_interfaceId (&iid) +{ + DefaultValueList::Add (this); +} +ClassId +ClassIdDefaultValue::GetValue (void) const +{ + return ComponentManager::LookupByName (m_name); +} +void +ClassIdDefaultValue::SetValue (ClassId classId) +{ + m_name = classId.GetName (); +} +void +ClassIdDefaultValue::SetValue (std::string name) +{ + m_name = name; +} +bool +ClassIdDefaultValue::DoParseValue (const std::string &value) +{ + bool ok; + ClassId classId = ComponentManager::LookupByName (value, &ok); + if (!ok) + { + return false; + } + std::vector classIdList = ComponentManager::LookupByInterfaceId (*m_interfaceId); + for (std::vector::const_iterator i = classIdList.begin (); + i != classIdList.end (); i++) + { + if (*i == classId) + { + m_name = value; + return true; + } + } + return false; +} + +std::string +ClassIdDefaultValue::DoGetType (void) const +{ + std::vector classIdList = ComponentManager::LookupByInterfaceId (*m_interfaceId); + std::ostringstream oss; + oss << "("; + for (std::vector::const_iterator i = classIdList.begin (); + i != classIdList.end (); i++) + { + if (i != classIdList.begin ()) + { + oss << "|"; + } + oss << i->GetName (); + } + oss << ")"; + return oss.str (); +} + +std::string +ClassIdDefaultValue::DoGetDefaultValue (void) const +{ + return m_name; +} + + + } // namespace ns3 @@ -161,7 +270,7 @@ public: const ns3::ClassId A::cidZero = ns3::MakeClassId ("A", A::iid); const ns3::ClassId A::cidOneBool = ns3::MakeClassId ("ABool", A::iid); const ns3::ClassId A::cidOneUi32 = ns3::MakeClassId ("AUi32", A::iid); -const ns3::InterfaceId A::iid = MakeInterfaceId ("A", Object::iid); +const ns3::InterfaceId A::iid = ns3::MakeInterfaceId ("A", Object::iid); A::A () : m_zeroInvoked (true), diff --git a/src/core/component-manager.h b/src/core/component-manager.h index 4d95642e3..2e7564289 100644 --- a/src/core/component-manager.h +++ b/src/core/component-manager.h @@ -29,10 +29,7 @@ #include "fatal-error.h" #include "ptr.h" #include "empty.h" - -namespace { -class unknown; -} +#include "default-value.h" namespace ns3 { @@ -53,7 +50,7 @@ public: * This name is also the name which is expected to be input * to ns3::UnknownManager::LookupByName. */ - std::string GetName (void); + std::string GetName (void) const; protected: ClassId (std::string name); private: @@ -86,7 +83,19 @@ public: * \returns the ClassId associated to the input name. */ static ClassId LookupByName (std::string name); - static std::vector LookupByInterface (InterfaceId iid); + static ClassId LookupByName (std::string name, bool *ok); + /** + * \param iid interface id to lookup + * \returns the list of ClassId which can be used to + * create objects which support the requested + * interface. + * + * Note that this method will not necessarily return the + * complete list of objects which support a given interface + * since dynamic aggregation of objects is not under + * the control of this class. + */ + static std::vector LookupByInterfaceId (InterfaceId iid); /** * \param classId class id of the constructor to invoke. @@ -140,9 +149,9 @@ public: private: friend void RegisterCallback (ClassId classId, CallbackBase *callback, - std::vector supportedInterfaces); + std::vector supportedInterfaces); static void Register (ClassId classId, CallbackBase *callback, - std::vector supportedInterfaces); + std::vector supportedInterfaces); template m_supportedInterfaces; + std::vector m_supportedInterfaces; }; typedef std::vector List; @@ -161,6 +170,25 @@ private: static CallbackBase *Lookup (ClassId classId); }; +class ClassIdDefaultValue : public DefaultValueBase +{ +public: + ClassIdDefaultValue (std::string name, + std::string help, + const InterfaceId &iid, + std::string defaultValue); + ClassId GetValue (void) const; + void SetValue (ClassId classId); + void SetValue (std::string name); +private: + virtual bool DoParseValue (const std::string &value); + virtual std::string DoGetType (void) const; + virtual std::string DoGetDefaultValue (void) const; + std::string m_defaultName; + std::string m_name; + const InterfaceId *m_interfaceId; +}; + } // namespace ns3 @@ -196,9 +224,9 @@ struct ObjectMaker { } // anonymous namespace namespace ns3 { - + void RegisterCallback (ClassId classId, ns3::CallbackBase *callback, - std::vector supportedInterfaces); + std::vector supportedInterfaces); template @@ -207,27 +235,30 @@ class MakeClassId : public ClassId private: typedef ObjectMaker MakerType; static Callback,T1,T2> m_callback; - static std::vector m_supportedInterfaces; + static std::vector m_supportedInterfaces; public: MakeClassId (std::string name) : ClassId (name) { RegisterCallback (*this, &m_callback, m_supportedInterfaces); } - MakeClassId (std::string name, InterfaceId iid) : ClassId (name) { - m_supportedInterfaces.push_back (iid); - RegisterCallback (*this, &m_callback, m_supportedInterfaces); - } - MakeClassId (std::string name, InterfaceId iid0, InterfaceId iid1) : ClassId (name) { - m_supportedInterfaces.push_back (iid0); - m_supportedInterfaces.push_back (iid1); + MakeClassId (std::string name, + const InterfaceId &iid) : ClassId (name) { + m_supportedInterfaces.push_back (&iid); RegisterCallback (*this, &m_callback, m_supportedInterfaces); } MakeClassId (std::string name, - InterfaceId iid0, - InterfaceId iid1, - InterfaceId iid2) : ClassId (name) { - m_supportedInterfaces.push_back (iid0); - m_supportedInterfaces.push_back (iid1); - m_supportedInterfaces.push_back (iid2); + const InterfaceId &iid0, + const InterfaceId iid1) : ClassId (name) { + m_supportedInterfaces.push_back (&iid0); + m_supportedInterfaces.push_back (&iid1); + RegisterCallback (*this, &m_callback, m_supportedInterfaces); + } + MakeClassId (std::string name, + const InterfaceId &iid0, + const InterfaceId &iid1, + const InterfaceId &iid2) : ClassId (name) { + m_supportedInterfaces.push_back (&iid0); + m_supportedInterfaces.push_back (&iid1); + m_supportedInterfaces.push_back (&iid2); RegisterCallback (*this, &m_callback, m_supportedInterfaces); } }; @@ -235,7 +266,7 @@ public: template Callback,T1,T2> MakeClassId::m_callback = MakeCallback (&MakeClassId::MakerType::MakeObject); template -std::vector MakeClassId::m_supportedInterfaces; +std::vector MakeClassId::m_supportedInterfaces; diff --git a/src/node/drop-tail.cc b/src/node/drop-tail.cc index 512d1184d..ed6606370 100644 --- a/src/node/drop-tail.cc +++ b/src/node/drop-tail.cc @@ -24,13 +24,6 @@ NS_DEBUG_COMPONENT_DEFINE ("DropTailQueue"); namespace ns3 { -static class QueueStackInitializationClass { -public: - QueueStackInitializationClass () { - Queue::AddDefault ("DropTailQueue"); - } -} queue_stack_initialization_class; - const ClassId DropTailQueue::cid = MakeClassId ("DropTailQueue", Queue::iid); diff --git a/src/node/queue.cc b/src/node/queue.cc index ba542540f..69b23bc0c 100644 --- a/src/node/queue.cc +++ b/src/node/queue.cc @@ -28,6 +28,8 @@ NS_DEBUG_COMPONENT_DEFINE ("Queue"); namespace ns3 { const InterfaceId Queue::iid = MakeInterfaceId ("Queue", Object::iid); +static ClassIdDefaultValue g_classIdDefaultValue ("Queue", "Packet Queue", + Queue::iid, "DropTailQueue"); Queue::Queue() : m_nBytes(0), @@ -199,27 +201,9 @@ Queue::Drop (const Packet& p) Ptr Queue::CreateDefault (void) { - std::string defaultValue = GetDefault ()->GetValue (); - ClassId classId = ComponentManager::LookupByName (defaultValue); + ClassId classId = g_classIdDefaultValue.GetValue (); Ptr queue = ComponentManager::Create (classId, Queue::iid); return queue; } -void -Queue::Add (const std::string &name) -{ - GetDefault ()->AddPossibleValue (name); -} -void -Queue::AddDefault (const std::string &name) -{ - GetDefault ()->AddDefaultValue (name); -} -StringEnumDefaultValue * -Queue::GetDefault (void) -{ - static StringEnumDefaultValue value ("Queue", "Packet Queue"); - return &value; -} - }; // namespace ns3 diff --git a/src/node/queue.h b/src/node/queue.h index 7977db136..02f57ca5c 100644 --- a/src/node/queue.h +++ b/src/node/queue.h @@ -172,10 +172,6 @@ public: * \return a Queue smart pointer that is the default Queue type defined */ static Ptr CreateDefault (void); - static void Add (const std::string &name); - static void AddDefault (const std::string &name); -private: - static StringEnumDefaultValue *GetDefault (void); }; }; // namespace ns3