From 3aa97061505419bdb95c10feb2e90e72ed8d413b Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Sun, 16 Mar 2008 19:24:50 +0100 Subject: [PATCH] replace TypeId::CreateObject with TypeId::GetConstructor --- src/core/object-factory.cc | 7 ++++- src/core/object.cc | 58 +++++++++++++++----------------------- src/core/object.h | 15 +++++----- 3 files changed, 37 insertions(+), 43 deletions(-) diff --git a/src/core/object-factory.cc b/src/core/object-factory.cc index 264f03fc2..ebdf5e20c 100644 --- a/src/core/object-factory.cc +++ b/src/core/object-factory.cc @@ -59,7 +59,12 @@ ObjectFactory::GetTypeId (void) const Ptr ObjectFactory::Create (void) const { - Ptr object = m_tid.CreateObject (m_parameters); + Callback cb = m_tid.GetConstructor (); + ObjectBase *base = cb (); + Object *derived = dynamic_cast (base); + derived->SetTypeId (m_tid); + derived->Construct (m_parameters); + Ptr object = Ptr (derived, false); return object; } diff --git a/src/core/object.cc b/src/core/object.cc index 0ccd12870..50742da4f 100644 --- a/src/core/object.cc +++ b/src/core/object.cc @@ -44,17 +44,17 @@ public: void SetParent (uint16_t uid, uint16_t parent); void SetTypeName (uint16_t uid, std::string typeName); void SetGroupName (uint16_t uid, std::string groupName); - void AddConstructor (uint16_t uid, ns3::CallbackBase callback); + void AddConstructor (uint16_t uid, ns3::Callback callback); void HideFromDocumentation (uint16_t uid); uint16_t GetUid (std::string name) const; std::string GetName (uint16_t uid) const; uint16_t GetParent (uint16_t uid) const; std::string GetTypeName (uint16_t uid) const; std::string GetGroupName (uint16_t uid) const; - ns3::CallbackBase GetConstructor (uint16_t uid); - bool HasConstructor (uint16_t uid); - uint32_t GetRegisteredN (void); - uint16_t GetRegistered (uint32_t i); + ns3::Callback GetConstructor (uint16_t uid) const; + bool HasConstructor (uint16_t uid) const; + uint32_t GetRegisteredN (void) const; + uint16_t GetRegistered (uint32_t i) const; void AddAttribute (uint16_t uid, std::string name, std::string help, @@ -99,7 +99,7 @@ private: std::string typeName; std::string groupName; bool hasConstructor; - ns3::CallbackBase constructor; + ns3::Callback constructor; bool mustHideFromDocumentation; std::vector attributes; std::vector traceSources; @@ -174,7 +174,7 @@ IidManager::HideFromDocumentation (uint16_t uid) } void -IidManager::AddConstructor (uint16_t uid, ns3::CallbackBase callback) +IidManager::AddConstructor (uint16_t uid, ns3::Callback callback) { struct IidInformation *information = LookupInformation (uid); if (information->hasConstructor) @@ -225,8 +225,8 @@ IidManager::GetGroupName (uint16_t uid) const return information->groupName; } -ns3::CallbackBase -IidManager::GetConstructor (uint16_t uid) +ns3::Callback +IidManager::GetConstructor (uint16_t uid) const { struct IidInformation *information = LookupInformation (uid); if (!information->hasConstructor) @@ -237,19 +237,19 @@ IidManager::GetConstructor (uint16_t uid) } bool -IidManager::HasConstructor (uint16_t uid) +IidManager::HasConstructor (uint16_t uid) const { struct IidInformation *information = LookupInformation (uid); return information->hasConstructor; } uint32_t -IidManager::GetRegisteredN (void) +IidManager::GetRegisteredN (void) const { return m_information.size (); } uint16_t -IidManager::GetRegistered (uint32_t i) +IidManager::GetRegistered (uint32_t i) const { return i + 1; } @@ -529,7 +529,7 @@ TypeId::HasConstructor (void) const } void -TypeId::DoAddConstructor (CallbackBase cb) +TypeId::DoAddConstructor (Callback cb) { Singleton::Get ()->AddConstructor (m_tid, cb); } @@ -557,27 +557,11 @@ TypeId::AddAttribute (std::string name, return *this; } - -CallbackBase -TypeId::LookupConstructor (void) const +Callback +TypeId::GetConstructor (void) const { - CallbackBase constructor = Singleton::Get ()->GetConstructor (m_tid); - return constructor; -} - -Ptr -TypeId::CreateObject (void) const -{ - return CreateObject (AttributeList ()); -} -Ptr -TypeId::CreateObject (const AttributeList &attributes) const -{ - CallbackBase cb = LookupConstructor (); - Callback,const AttributeList &> realCb; - realCb.Assign (cb); - Ptr object = realCb (attributes); - return object; + Callback cb = Singleton::Get ()->GetConstructor (m_tid); + return cb; } bool @@ -1345,6 +1329,7 @@ Object::MaybeDelete (void) const #ifdef RUN_SELF_TESTS #include "test.h" +#include "object-factory.h" namespace { @@ -1481,11 +1466,14 @@ ObjectTest::RunTests (void) // Test the object creation code of TypeId - Ptr a = BaseA::GetTypeId ().CreateObject (); + ObjectFactory factory; + factory.SetTypeId (BaseA::GetTypeId ()); + Ptr a = factory.Create (); NS_TEST_ASSERT_EQUAL (a->GetObject (), a); NS_TEST_ASSERT_EQUAL (a->GetObject (DerivedA::GetTypeId ()), 0); NS_TEST_ASSERT_EQUAL (a->GetObject (), 0); - a = DerivedA::GetTypeId ().CreateObject (); + factory.SetTypeId (DerivedA::GetTypeId ()); + a = factory.Create (); NS_TEST_ASSERT_EQUAL (a->GetObject (), a); NS_TEST_ASSERT_EQUAL (a->GetObject (DerivedA::GetTypeId ()), a); NS_TEST_ASSERT_UNEQUAL (a->GetObject (), 0); diff --git a/src/core/object.h b/src/core/object.h index 53a0c4958..6d0e5d279 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -177,8 +177,7 @@ public: std::string GetTraceSourceHelp (uint32_t i) const; Ptr GetTraceSourceAccessor (uint32_t i) const; - Ptr CreateObject (const AttributeList &attributes) const; - Ptr CreateObject (void) const; + Callback GetConstructor (void) const; bool MustHideFromDocumentation (void) const; @@ -319,8 +318,7 @@ private: static bool LookupAttributeByFullName (std::string fullName, struct AttributeInfo *info); explicit TypeId (uint16_t tid); - void DoAddConstructor (CallbackBase callback); - CallbackBase LookupConstructor (void) const; + void DoAddConstructor (Callback callback); Ptr GetAttributeAccessor (uint32_t i) const; uint16_t m_tid; @@ -518,6 +516,8 @@ private: template friend Ptr CopyObject (Ptr object); + friend class ObjectFactory; + bool DoSet (Ptr spec, Attribute intialValue, Ptr checker, Attribute value); Ptr DoGetObject (TypeId tid) const; @@ -596,11 +596,12 @@ TypeId TypeId::AddConstructor (void) { struct Maker { - static Ptr Create (const AttributeList &attributes) { - return ns3::CreateObject (attributes); + static ObjectBase * Create () { + ObjectBase * base = new T (); + return base; } }; - CallbackBase cb = MakeCallback (&Maker::Create); + Callback cb = MakeCallback (&Maker::Create); DoAddConstructor (cb); return *this; }