From 0d5554a509f440f7d9b0b2a98f1cc21dae931d37 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Fri, 19 Jun 2009 08:43:58 +0200 Subject: [PATCH] make CreateObject<> behave like Create<>: use positional constructor arguments rather than attribute lists. --- .../ns3modulegen_core_customizations.py | 6 +- examples/csma-packet-socket.cc | 2 +- examples/simple-error-model.cc | 2 +- examples/tcp-nsc-lfn.cc | 4 +- src/core/attribute-test.cc | 8 +-- src/core/object.h | 71 +++++++++++++++++-- src/helper/mobility-helper.cc | 4 +- 7 files changed, 77 insertions(+), 20 deletions(-) diff --git a/bindings/python/ns3modulegen_core_customizations.py b/bindings/python/ns3modulegen_core_customizations.py index 8ad75632f..194d99599 100644 --- a/bindings/python/ns3modulegen_core_customizations.py +++ b/bindings/python/ns3modulegen_core_customizations.py @@ -325,7 +325,7 @@ static ns3::TypeId GetTypeId (void) "NS_OBJECT_ENSURE_REGISTERED (%s);" % helper_class.name) Object.add_helper_class_hook(helper_class_hook) - ## Replace all class constructors with a generic constructor based on CreateObject (AttributeList) + ## Replace all class constructors with a generic constructor based on CreateObjectWithAttributes (AttributeList) module.header.writeln(''' namespace ns3 { @@ -357,7 +357,7 @@ Ptr CreateObjectPython (PyObject *pyobj, const AttributeList &attributes) if construct_name and not cls.helper_class: construct_code = ''' - ns3::Ptr< %(CONSTRUCT_NAME)s > obj = ns3::CreateObject< %(CONSTRUCT_NAME)s > (attrList); + ns3::Ptr< %(CONSTRUCT_NAME)s > obj = ns3::CreateObjectWithAttributes< %(CONSTRUCT_NAME)s > (attrList); obj->Ref (); self->obj = ns3::PeekPointer (obj); ''' % dict (CONSTRUCT_NAME=construct_name) @@ -393,7 +393,7 @@ Ptr CreateObjectPython (PyObject *pyobj, const AttributeList &attributes) obj->Ref (); self->obj = ns3::PeekPointer (obj); } else { - ns3::Ptr< %(CONSTRUCT_NAME)s > obj = ns3::CreateObject< %(CONSTRUCT_NAME)s > (attrList); + ns3::Ptr< %(CONSTRUCT_NAME)s > obj = ns3::CreateObjectWithAttributes< %(CONSTRUCT_NAME)s > (attrList); obj->Ref (); self->obj = ns3::PeekPointer (obj); } diff --git a/examples/csma-packet-socket.cc b/examples/csma-packet-socket.cc index c75035642..fa24b3d0b 100644 --- a/examples/csma-packet-socket.cc +++ b/examples/csma-packet-socket.cc @@ -72,7 +72,7 @@ main (int argc, char *argv[]) // create the shared medium used by all csma devices. NS_LOG_INFO ("Create channels."); - Ptr channel = CreateObject ( + Ptr channel = CreateObjectWithAttributes ( "DataRate", DataRateValue (DataRate(5000000)), "Delay", TimeValue (MilliSeconds(2))); diff --git a/examples/simple-error-model.cc b/examples/simple-error-model.cc index c43664dbd..2751c0303 100644 --- a/examples/simple-error-model.cc +++ b/examples/simple-error-model.cc @@ -153,7 +153,7 @@ main (int argc, char *argv[]) // // Create an ErrorModel based on the implementation (constructor) // specified by the default classId - Ptr em = CreateObject ("RanVar", RandomVariableValue (UniformVariable (0.0, 1.0)), + Ptr em = CreateObjectWithAttributes ("RanVar", RandomVariableValue (UniformVariable (0.0, 1.0)), "ErrorRate", DoubleValue (0.001)); d3d2.Get (0)->SetAttribute ("ReceiveErrorModel", PointerValue (em)); diff --git a/examples/tcp-nsc-lfn.cc b/examples/tcp-nsc-lfn.cc index 93a67774b..0bb6fc685 100644 --- a/examples/tcp-nsc-lfn.cc +++ b/examples/tcp-nsc-lfn.cc @@ -109,9 +109,9 @@ int main (int argc, char *argv[]) DoubleValue rate(errRate); RandomVariableValue u01(UniformVariable (0.0, 1.0)); Ptr em1 = - CreateObject ("RanVar", u01, "ErrorRate", rate); + CreateObjectWithAttributes ("RanVar", u01, "ErrorRate", rate); Ptr em2 = - CreateObject ("RanVar", u01, "ErrorRate", rate); + CreateObjectWithAttributes ("RanVar", u01, "ErrorRate", rate); // This enables the specified errRate on both link endpoints. p2pInterfaces.Get(0)->SetAttribute("ReceiveErrorModel", PointerValue (em1)); diff --git a/src/core/attribute-test.cc b/src/core/attribute-test.cc index b0c3bf74f..46adf84d4 100644 --- a/src/core/attribute-test.cc +++ b/src/core/attribute-test.cc @@ -279,7 +279,7 @@ AttributeTest::RunTests (void) AttributeList params; Ptr p; NS_TEST_ASSERT (params.SetFailSafe ("ns3::AttributeObjectTest::TestBoolName", StringValue ("false"))); - p = CreateObject (params); + p = CreateObjectWithAttributes (params); CHECK_GET_STR (p, "TestBoolName", "false"); CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, false); @@ -291,11 +291,11 @@ AttributeTest::RunTests (void) CHECK_GET_STR (p, "TestBoolName", "false"); CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, false); - p = CreateObject ("TestBoolName", StringValue ("true")); + p = CreateObjectWithAttributes ("TestBoolName", StringValue ("true")); CHECK_GET_STR (p, "TestBoolName", "true"); CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true); - p = CreateObject ("TestBoolName", BooleanValue (true)); + p = CreateObjectWithAttributes ("TestBoolName", BooleanValue (true)); CHECK_GET_STR (p, "TestBoolName", "true"); CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true); @@ -502,7 +502,7 @@ AttributeTest::RunTests (void) Ptr x = ptr.Get (); NS_TEST_ASSERT (x == 0); - p = CreateObject ("Pointer", PointerValue (Create ())); + p = CreateObjectWithAttributes ("Pointer", PointerValue (Create ())); NS_TEST_ASSERT (p != 0); derived = 0; p->GetAttribute ("Pointer", ptr); diff --git a/src/core/object.h b/src/core/object.h index 8cf1120d2..721c7453b 100644 --- a/src/core/object.h +++ b/src/core/object.h @@ -200,7 +200,7 @@ protected: private: template - friend Ptr CreateObject (const AttributeList &attributes); + friend Ptr CreateObjectWithAttributes (const AttributeList &attributes); template friend Ptr CopyObject (Ptr object); template @@ -209,6 +209,8 @@ private: // by our python bindings to call the protected ObjectBase::Construct // method. friend void PythonCompleteConstruct (Ptr object, TypeId typeId, const AttributeList &attributes); + template + friend Ptr CompleteConstruct (T *object); friend class ObjectFactory; friend class AggregateIterator; @@ -290,7 +292,7 @@ Ptr CopyObject (Ptr object); * it with a set of attributes. */ template -Ptr CreateObject (const AttributeList &attributes); +Ptr CreateObjectWithAttributes (const AttributeList &attributes); /** * \param n1 name of attribute @@ -318,7 +320,7 @@ Ptr CreateObject (const AttributeList &attributes); */ template Ptr -CreateObject (std::string n1 = "", const AttributeValue & v1 = EmptyAttributeValue (), +CreateObjectWithAttributes (std::string n1 = "", const AttributeValue & v1 = EmptyAttributeValue (), std::string n2 = "", const AttributeValue & v2 = EmptyAttributeValue (), std::string n3 = "", const AttributeValue & v3 = EmptyAttributeValue (), std::string n4 = "", const AttributeValue & v4 = EmptyAttributeValue (), @@ -398,9 +400,15 @@ Ptr CopyObject (Ptr object) return p; } - template -Ptr CreateObject (const AttributeList &attributes) +Ptr CompleteConstruct (T *p) +{ + p->SetTypeId (T::GetTypeId ()); + p->Object::Construct (AttributeList()); + return Ptr (p, false); +} +template +Ptr CreateObjectWithAttributes (const AttributeList &attributes) { Ptr p = Ptr (new T (), false); p->SetTypeId (T::GetTypeId ()); @@ -410,7 +418,7 @@ Ptr CreateObject (const AttributeList &attributes) template Ptr -CreateObject (std::string n1 , const AttributeValue & v1, +CreateObjectWithAttributes (std::string n1 , const AttributeValue & v1, std::string n2 , const AttributeValue & v2, std::string n3 , const AttributeValue & v3, std::string n4 , const AttributeValue & v4, @@ -467,9 +475,58 @@ CreateObject (std::string n1 , const AttributeValue & v1, } attributes.SetWithTid (T::GetTypeId (), n9, v9); end: - return CreateObject (attributes); + return CreateObjectWithAttributes (attributes); } +template +Ptr CreateObject (void) +{ + return CompleteConstruct (new T ()); +} + +template +Ptr CreateObject (T1 a1) +{ + return CompleteConstruct (new T (a1)); +} + +template +Ptr CreateObject (T1 a1, T2 a2) +{ + return CompleteConstruct (new T (a1,a2)); +} + +template +Ptr CreateObject (T1 a1, T2 a2, T3 a3) +{ + return CompleteConstruct (new T (a1,a2,a3)); +} + +template +Ptr CreateObject (T1 a1, T2 a2, T3 a3, T4 a4) +{ + return CompleteConstruct (new T (a1,a2,a3,a4)); +} + +template +Ptr CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) +{ + return CompleteConstruct (new T (a1,a2,a3,a4,a5)); +} + +template +Ptr CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6) +{ + return CompleteConstruct (new T (a1,a2,a3,a4,a5,a6)); +} + +template +Ptr CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7) +{ + return CompleteConstruct (new T (a1,a2,a3,a4,a5,a6,a7)); +} + + } // namespace ns3 #endif /* OBJECT_H */ diff --git a/src/helper/mobility-helper.cc b/src/helper/mobility-helper.cc index bca62db35..8cfe13bd4 100644 --- a/src/helper/mobility-helper.cc +++ b/src/helper/mobility-helper.cc @@ -34,7 +34,7 @@ NS_LOG_COMPONENT_DEFINE ("MobilityHelper"); MobilityHelper::MobilityHelper () { - m_position = CreateObject + m_position = CreateObjectWithAttributes ("X", RandomVariableValue (ConstantVariable (0.0)), "Y", RandomVariableValue (ConstantVariable (0.0))); m_mobility.SetTypeId ("ns3::ConstantPositionMobilityModel"); @@ -147,7 +147,7 @@ MobilityHelper::Install (Ptr node) const // we need to setup a hierarchical mobility model Ptr parent = m_mobilityStack.back (); Ptr hierarchical = - CreateObject ("Child", PointerValue (model), + CreateObjectWithAttributes ("Child", PointerValue (model), "Parent", PointerValue (parent)); object->AggregateObject (hierarchical); NS_LOG_DEBUG ("node="<