replace TypeId::CreateObject with TypeId::GetConstructor
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sun, 16 Mar 2008 19:24:50 +0100
changeset 2631 365595f1f9a8
parent 2630 17b545238ab3
child 2632 e7989e4c674a
replace TypeId::CreateObject with TypeId::GetConstructor
src/core/object-factory.cc
src/core/object.cc
src/core/object.h
--- a/src/core/object-factory.cc	Sun Mar 16 18:42:23 2008 +0100
+++ b/src/core/object-factory.cc	Sun Mar 16 19:24:50 2008 +0100
@@ -59,7 +59,12 @@
 Ptr<Object> 
 ObjectFactory::Create (void) const
 {
-  Ptr<Object> object = m_tid.CreateObject (m_parameters);
+  Callback<ObjectBase *> cb = m_tid.GetConstructor ();
+  ObjectBase *base = cb ();
+  Object *derived = dynamic_cast<Object *> (base);
+  derived->SetTypeId (m_tid);
+  derived->Construct (m_parameters);
+  Ptr<Object> object = Ptr<Object> (derived, false);
   return object;
 }
 
--- a/src/core/object.cc	Sun Mar 16 18:42:23 2008 +0100
+++ b/src/core/object.cc	Sun Mar 16 19:24:50 2008 +0100
@@ -44,17 +44,17 @@
   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<ns3::ObjectBase *> 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<ns3::ObjectBase *> 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 @@
     std::string typeName;
     std::string groupName;
     bool hasConstructor;
-    ns3::CallbackBase constructor;
+    ns3::Callback<ns3::ObjectBase *> constructor;
     bool mustHideFromDocumentation;
     std::vector<struct AttributeInformation> attributes;
     std::vector<struct TraceSourceInformation> traceSources;
@@ -174,7 +174,7 @@
 }
 
 void 
-IidManager::AddConstructor (uint16_t uid, ns3::CallbackBase callback)
+IidManager::AddConstructor (uint16_t uid, ns3::Callback<ns3::ObjectBase *> callback)
 {
   struct IidInformation *information = LookupInformation (uid);
   if (information->hasConstructor)
@@ -225,8 +225,8 @@
   return information->groupName;
 }
 
-ns3::CallbackBase 
-IidManager::GetConstructor (uint16_t uid)
+ns3::Callback<ns3::ObjectBase *> 
+IidManager::GetConstructor (uint16_t uid) const
 {
   struct IidInformation *information = LookupInformation (uid);
   if (!information->hasConstructor)
@@ -237,19 +237,19 @@
 }
 
 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 @@
 }
 
 void
-TypeId::DoAddConstructor (CallbackBase cb)
+TypeId::DoAddConstructor (Callback<ObjectBase *> cb)
 {
   Singleton<IidManager>::Get ()->AddConstructor (m_tid, cb);
 }
@@ -557,27 +557,11 @@
   return *this;
 }
 
-
-CallbackBase
-TypeId::LookupConstructor (void) const
-{
-  CallbackBase constructor = Singleton<IidManager>::Get ()->GetConstructor (m_tid);
-  return constructor;
-}
-
-Ptr<Object> 
-TypeId::CreateObject (void) const
+Callback<ObjectBase *> 
+TypeId::GetConstructor (void) const
 {
-  return CreateObject (AttributeList ());
-}
-Ptr<Object> 
-TypeId::CreateObject (const AttributeList &attributes) const
-{
-  CallbackBase cb = LookupConstructor ();
-  Callback<Ptr<Object>,const AttributeList &> realCb;
-  realCb.Assign (cb);
-  Ptr<Object> object = realCb (attributes);
-  return object;  
+  Callback<ObjectBase *>  cb = Singleton<IidManager>::Get ()->GetConstructor (m_tid);
+  return cb;
 }
 
 bool 
@@ -1345,6 +1329,7 @@
 #ifdef RUN_SELF_TESTS
 
 #include "test.h"
+#include "object-factory.h"
 
 namespace {
 
@@ -1481,11 +1466,14 @@
 
 
   // Test the object creation code of TypeId
-  Ptr<Object> a = BaseA::GetTypeId ().CreateObject ();
+  ObjectFactory factory;
+  factory.SetTypeId (BaseA::GetTypeId ());
+  Ptr<Object> a = factory.Create ();
   NS_TEST_ASSERT_EQUAL (a->GetObject<BaseA> (), a);
   NS_TEST_ASSERT_EQUAL (a->GetObject<BaseA> (DerivedA::GetTypeId ()), 0);
   NS_TEST_ASSERT_EQUAL (a->GetObject<DerivedA> (), 0);
-  a = DerivedA::GetTypeId ().CreateObject ();
+  factory.SetTypeId (DerivedA::GetTypeId ());
+  a = factory.Create ();
   NS_TEST_ASSERT_EQUAL (a->GetObject<BaseA> (), a);
   NS_TEST_ASSERT_EQUAL (a->GetObject<BaseA> (DerivedA::GetTypeId ()), a);
   NS_TEST_ASSERT_UNEQUAL (a->GetObject<DerivedA> (), 0);
--- a/src/core/object.h	Sun Mar 16 18:42:23 2008 +0100
+++ b/src/core/object.h	Sun Mar 16 19:24:50 2008 +0100
@@ -177,8 +177,7 @@
   std::string GetTraceSourceHelp (uint32_t i) const;
   Ptr<const TraceSourceAccessor> GetTraceSourceAccessor (uint32_t i) const;
 
-  Ptr<Object> CreateObject (const AttributeList &attributes) const;
-  Ptr<Object> CreateObject (void) const;
+  Callback<ObjectBase *> GetConstructor (void) const;
 
   bool MustHideFromDocumentation (void) const;
 
@@ -319,8 +318,7 @@
   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<ObjectBase *> callback);
   Ptr<const AttributeAccessor> GetAttributeAccessor (uint32_t i) const;
   
   uint16_t m_tid;
@@ -518,6 +516,8 @@
   template <typename T>
   friend Ptr<T> CopyObject (Ptr<T> object);
 
+  friend class ObjectFactory;
+
   bool DoSet (Ptr<const AttributeAccessor> spec, Attribute intialValue, 
               Ptr<const AttributeChecker> checker, Attribute value);
   Ptr<Object> DoGetObject (TypeId tid) const;
@@ -596,11 +596,12 @@
 TypeId::AddConstructor (void)
 {
   struct Maker {
-    static Ptr<Object> Create (const AttributeList &attributes) {
-      return ns3::CreateObject<T> (attributes);
+    static ObjectBase * Create () {
+      ObjectBase * base = new T ();
+      return base;
     }
   };
-  CallbackBase cb = MakeCallback (&Maker::Create);
+  Callback<ObjectBase *> cb = MakeCallback (&Maker::Create);
   DoAddConstructor (cb);
   return *this;
 }