make CreateObject<> behave like Create<>: use positional constructor arguments rather than attribute lists.

This commit is contained in:
Mathieu Lacage committed 2009-06-19 08:43:58 +02:00
1 parent 9dc9655596
commit 0b8a6084bd
7 files changed
+77 -20

No files matched your search

@@ -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<T> (AttributeList)
## Replace all class constructors with a generic constructor based on CreateObjectWithAttributes<T> (AttributeList)
module.header.writeln('''
namespace ns3 {
@@ -357,7 +357,7 @@ Ptr<T> 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<T> 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);
}
+1 -1
View File
@@ -72,7 +72,7 @@ main (int argc, char *argv[])
// create the shared medium used by all csma devices.
NS_LOG_INFO ("Create channels.");
Ptr<CsmaChannel> channel = CreateObject<CsmaChannel> (
Ptr<CsmaChannel> channel = CreateObjectWithAttributes<CsmaChannel> (
"DataRate", DataRateValue (DataRate(5000000)),
"Delay", TimeValue (MilliSeconds(2)));
+1 -1
View File
@@ -153,7 +153,7 @@ main (int argc, char *argv[])
//
// Create an ErrorModel based on the implementation (constructor)
// specified by the default classId
Ptr<RateErrorModel> em = CreateObject<RateErrorModel> ("RanVar", RandomVariableValue (UniformVariable (0.0, 1.0)),
Ptr<RateErrorModel> em = CreateObjectWithAttributes<RateErrorModel> ("RanVar", RandomVariableValue (UniformVariable (0.0, 1.0)),
"ErrorRate", DoubleValue (0.001));
d3d2.Get (0)->SetAttribute ("ReceiveErrorModel", PointerValue (em));
+2 -2
View File
@@ -109,9 +109,9 @@ int main (int argc, char *argv[])
DoubleValue rate(errRate);
RandomVariableValue u01(UniformVariable (0.0, 1.0));
Ptr<RateErrorModel> em1 =
CreateObject<RateErrorModel> ("RanVar", u01, "ErrorRate", rate);
CreateObjectWithAttributes<RateErrorModel> ("RanVar", u01, "ErrorRate", rate);
Ptr<RateErrorModel> em2 =
CreateObject<RateErrorModel> ("RanVar", u01, "ErrorRate", rate);
CreateObjectWithAttributes<RateErrorModel> ("RanVar", u01, "ErrorRate", rate);
// This enables the specified errRate on both link endpoints.
p2pInterfaces.Get(0)->SetAttribute("ReceiveErrorModel", PointerValue (em1));
+4 -4
View File
@@ -279,7 +279,7 @@ AttributeTest::RunTests (void)
AttributeList params;
Ptr<AttributeObjectTest> p;
NS_TEST_ASSERT (params.SetFailSafe ("ns3::AttributeObjectTest::TestBoolName", StringValue ("false")));
p = CreateObject<AttributeObjectTest> (params);
p = CreateObjectWithAttributes<AttributeObjectTest> (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<AttributeObjectTest> ("TestBoolName", StringValue ("true"));
p = CreateObjectWithAttributes<AttributeObjectTest> ("TestBoolName", StringValue ("true"));
CHECK_GET_STR (p, "TestBoolName", "true");
CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
p = CreateObject<AttributeObjectTest> ("TestBoolName", BooleanValue (true));
p = CreateObjectWithAttributes<AttributeObjectTest> ("TestBoolName", BooleanValue (true));
CHECK_GET_STR (p, "TestBoolName", "true");
CHECK_GET_PARAM (p, "TestBoolName", BooleanValue, true);
@@ -502,7 +502,7 @@ AttributeTest::RunTests (void)
Ptr<AttributeObjectTest> x = ptr.Get<AttributeObjectTest> ();
NS_TEST_ASSERT (x == 0);
p = CreateObject<AttributeObjectTest> ("Pointer", PointerValue (Create<Derived> ()));
p = CreateObjectWithAttributes<AttributeObjectTest> ("Pointer", PointerValue (Create<Derived> ()));
NS_TEST_ASSERT (p != 0);
derived = 0;
p->GetAttribute ("Pointer", ptr);
+64 -7
View File
@@ -200,7 +200,7 @@ protected:
private:
template <typename T>
friend Ptr<T> CreateObject (const AttributeList &attributes);
friend Ptr<T> CreateObjectWithAttributes (const AttributeList &attributes);
template <typename T>
friend Ptr<T> CopyObject (Ptr<T> object);
template <typename T>
@@ -209,6 +209,8 @@ private:
// by our python bindings to call the protected ObjectBase::Construct
// method.
friend void PythonCompleteConstruct (Ptr<Object> object, TypeId typeId, const AttributeList &attributes);
template <typename T>
friend Ptr<T> CompleteConstruct (T *object);
friend class ObjectFactory;
friend class AggregateIterator;
@@ -290,7 +292,7 @@ Ptr<T> CopyObject (Ptr<T> object);
* it with a set of attributes.
*/
template <typename T>
Ptr<T> CreateObject (const AttributeList &attributes);
Ptr<T> CreateObjectWithAttributes (const AttributeList &attributes);
/**
* \param n1 name of attribute
@@ -318,7 +320,7 @@ Ptr<T> CreateObject (const AttributeList &attributes);
*/
template <typename T>
Ptr<T>
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<T> CopyObject (Ptr<const T> object)
return p;
}
template <typename T>
Ptr<T> CreateObject (const AttributeList &attributes)
Ptr<T> CompleteConstruct (T *p)
{
p->SetTypeId (T::GetTypeId ());
p->Object::Construct (AttributeList());
return Ptr<T> (p, false);
}
template <typename T>
Ptr<T> CreateObjectWithAttributes (const AttributeList &attributes)
{
Ptr<T> p = Ptr<T> (new T (), false);
p->SetTypeId (T::GetTypeId ());
@@ -410,7 +418,7 @@ Ptr<T> CreateObject (const AttributeList &attributes)
template <typename T>
Ptr<T>
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<T> (attributes);
return CreateObjectWithAttributes<T> (attributes);
}
template <typename T>
Ptr<T> CreateObject (void)
{
return CompleteConstruct (new T ());
}
template <typename T, typename T1>
Ptr<T> CreateObject (T1 a1)
{
return CompleteConstruct (new T (a1));
}
template <typename T, typename T1, typename T2>
Ptr<T> CreateObject (T1 a1, T2 a2)
{
return CompleteConstruct (new T (a1,a2));
}
template <typename T, typename T1, typename T2, typename T3>
Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3)
{
return CompleteConstruct (new T (a1,a2,a3));
}
template <typename T, typename T1, typename T2, typename T3, typename T4>
Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4)
{
return CompleteConstruct (new T (a1,a2,a3,a4));
}
template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5>
Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
{
return CompleteConstruct (new T (a1,a2,a3,a4,a5));
}
template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6)
{
return CompleteConstruct (new T (a1,a2,a3,a4,a5,a6));
}
template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
Ptr<T> 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 */
+2 -2
View File
@@ -34,7 +34,7 @@ NS_LOG_COMPONENT_DEFINE ("MobilityHelper");
MobilityHelper::MobilityHelper ()
{
m_position = CreateObject<RandomRectanglePositionAllocator>
m_position = CreateObjectWithAttributes<RandomRectanglePositionAllocator>
("X", RandomVariableValue (ConstantVariable (0.0)),
"Y", RandomVariableValue (ConstantVariable (0.0)));
m_mobility.SetTypeId ("ns3::ConstantPositionMobilityModel");
@@ -147,7 +147,7 @@ MobilityHelper::Install (Ptr<Node> node) const
// we need to setup a hierarchical mobility model
Ptr<MobilityModel> parent = m_mobilityStack.back ();
Ptr<MobilityModel> hierarchical =
CreateObject<HierarchicalMobilityModel> ("Child", PointerValue (model),
CreateObjectWithAttributes<HierarchicalMobilityModel> ("Child", PointerValue (model),
"Parent", PointerValue (parent));
object->AggregateObject (hierarchical);
NS_LOG_DEBUG ("node="<<object<<", mob="<<hierarchical);