move attribute code to ObjectBase.
--- a/src/core/attribute-list.h Sun Mar 16 21:52:15 2008 +0100
+++ b/src/core/attribute-list.h Mon Mar 17 05:22:29 2008 +0100
@@ -70,7 +70,7 @@
std::string SerializeToString (void) const;
bool DeserializeFromString (std::string value);
private:
- friend class Object;
+ friend class ObjectBase;
struct Attr {
Ptr<const AttributeChecker> checker;
Attribute value;
--- a/src/core/object-base.cc Sun Mar 16 21:52:15 2008 +0100
+++ b/src/core/object-base.cc Mon Mar 17 05:22:29 2008 +0100
@@ -1,3 +1,254 @@
#include "object-base.h"
+#include "log.h"
+#include "trace-source-accessor.h"
+#include "string.h"
+
+NS_LOG_COMPONENT_DEFINE ("ObjectBase");
+
+namespace ns3 {
+
+static TypeId
+GetObjectIid (void)
+{
+ TypeId tid = TypeId ("ns3::ObjectBase");
+ tid.SetParent (tid);
+ return tid;
+}
+
+TypeId
+ObjectBase::GetTypeId (void)
+{
+ static TypeId tid = GetObjectIid ();
+ return tid;
+}
+
+ObjectBase::~ObjectBase ()
+{}
+
+void
+ObjectBase::NotifyConstructionCompleted (void)
+{}
+
+void
+ObjectBase::ConstructSelf (const AttributeList &attributes)
+{
+ // loop over the inheritance tree back to the Object base class.
+ TypeId tid = GetInstanceTypeId ();
+ do {
+ // loop over all attributes in object type
+ NS_LOG_DEBUG ("construct tid="<<tid.GetName ()<<", params="<<tid.GetAttributeListN ());
+ for (uint32_t i = 0; i < tid.GetAttributeListN (); i++)
+ {
+ Ptr<const AttributeAccessor> paramSpec = tid.GetAttributeAccessor (i);
+ Attribute initial = tid.GetAttributeInitialValue (i);
+ Ptr<const AttributeChecker> checker = tid.GetAttributeChecker (i);
+ NS_LOG_DEBUG ("try to construct \""<< tid.GetName ()<<"::"<<
+ tid.GetAttributeName (i)<<"\"");
+ if (!(tid.GetAttributeFlags (i) & TypeId::ATTR_CONSTRUCT))
+ {
+ continue;
+ }
+ bool found = false;
+ // is this attribute stored in this AttributeList instance ?
+ for (AttributeList::Attrs::const_iterator j = attributes.m_attributes.begin ();
+ j != attributes.m_attributes.end (); j++)
+ {
+ if (j->checker == checker)
+ {
+ // We have a matching attribute value.
+ DoSet (paramSpec, initial, checker, j->value);
+ NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<<
+ tid.GetAttributeName (i)<<"\"");
+ found = true;
+ break;
+ }
+ }
+ if (!found)
+ {
+ // is this attribute stored in the global instance instance ?
+ for (AttributeList::Attrs::const_iterator j = AttributeList::GetGlobal ()->m_attributes.begin ();
+ j != AttributeList::GetGlobal ()->m_attributes.end (); j++)
+ {
+ if (j->checker == checker)
+ {
+ // We have a matching attribute value.
+ DoSet (paramSpec, initial, checker, j->value);
+ NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<<
+ tid.GetAttributeName (i)<<"\" from global");
+ found = true;
+ break;
+ }
+ }
+ }
+ if (!found)
+ {
+ // No matching attribute value so we set the default value.
+ paramSpec->Set (this, initial);
+ NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<<
+ tid.GetAttributeName (i)<<"\" from initial value.");
+ }
+ }
+ tid = tid.GetParent ();
+ } while (tid != ObjectBase::GetTypeId ());
+ NotifyConstructionCompleted ();
+}
-ns3::ObjectBase::~ObjectBase () {}
+bool
+ObjectBase::DoSet (Ptr<const AttributeAccessor> spec, Attribute initialValue,
+ Ptr<const AttributeChecker> checker, Attribute value)
+{
+ bool ok = checker->Check (value);
+ if (!ok)
+ {
+ // attempt to convert to string
+ const StringValue *str = value.DynCast<const StringValue *> ();
+ if (str == 0)
+ {
+ return false;
+ }
+ // attempt to convert back from string.
+ Attribute v = checker->Create ();
+ ok = v.DeserializeFromString (str->Get ().Get (), checker);
+ if (!ok)
+ {
+ return false;
+ }
+ ok = checker->Check (v);
+ if (!ok)
+ {
+ return false;
+ }
+ value = v;
+ }
+ ok = spec->Set (this, value);
+ return ok;
+}
+void
+ObjectBase::SetAttribute (std::string name, Attribute value)
+{
+ struct TypeId::AttributeInfo info;
+ TypeId tid = GetInstanceTypeId ();
+ if (!tid.LookupAttributeByName (name, &info))
+ {
+ NS_FATAL_ERROR ("Attribute name="<<name<<" does not exist for this object: tid="<<tid.GetName ());
+ }
+ if (!(info.flags & TypeId::ATTR_SET))
+ {
+ NS_FATAL_ERROR ("Attribute name="<<name<<" is not settable for this object: tid="<<tid.GetName ());
+ }
+ if (!DoSet (info.accessor, info.initialValue, info.checker, value))
+ {
+ NS_FATAL_ERROR ("Attribute name="<<name<<" could not be set for this object: tid="<<tid.GetName ());
+ }
+}
+bool
+ObjectBase::SetAttributeFailSafe (std::string name, Attribute value)
+{
+ struct TypeId::AttributeInfo info;
+ TypeId tid = GetInstanceTypeId ();
+ if (!tid.LookupAttributeByName (name, &info))
+ {
+ return false;
+ }
+ if (!(info.flags & TypeId::ATTR_SET))
+ {
+ return false;
+ }
+ return DoSet (info.accessor, info.initialValue, info.checker, value);
+}
+bool
+ObjectBase::GetAttribute (std::string name, std::string &value) const
+{
+ struct TypeId::AttributeInfo info;
+ TypeId tid = GetInstanceTypeId ();
+ if (!tid.LookupAttributeByName (name, &info))
+ {
+ return false;
+ }
+ if (!(info.flags & TypeId::ATTR_GET))
+ {
+ return false;
+ }
+ Attribute v = info.checker->Create ();
+ bool ok = info.accessor->Get (this, v);
+ if (ok)
+ {
+ value = v.SerializeToString (info.checker);
+ }
+ return ok;
+}
+
+Attribute
+ObjectBase::GetAttribute (std::string name) const
+{
+ struct TypeId::AttributeInfo info;
+ TypeId tid = GetInstanceTypeId ();
+ if (!tid.LookupAttributeByName (name, &info))
+ {
+ return Attribute ();
+ }
+ if (!(info.flags & TypeId::ATTR_GET))
+ {
+ return Attribute ();
+ }
+ Attribute value = info.checker->Create ();
+ bool ok = info.accessor->Get (this, value);
+ if (!ok)
+ {
+ return Attribute ();
+ }
+ return value;
+}
+
+bool
+ObjectBase::TraceConnectWithoutContext (std::string name, const CallbackBase &cb)
+{
+ TypeId tid = GetInstanceTypeId ();
+ Ptr<const TraceSourceAccessor> accessor = tid.LookupTraceSourceByName (name);
+ if (accessor == 0)
+ {
+ return false;
+ }
+ bool ok = accessor->ConnectWithoutContext (this, cb);
+ return ok;
+}
+bool
+ObjectBase::TraceConnectWithoutContext (std::string name, std::string context, const CallbackBase &cb)
+{
+ TypeId tid = GetInstanceTypeId ();
+ Ptr<const TraceSourceAccessor> accessor = tid.LookupTraceSourceByName (name);
+ if (accessor == 0)
+ {
+ return false;
+ }
+ bool ok = accessor->Connect (this, context, cb);
+ return ok;
+}
+bool
+ObjectBase::TraceDisconnectWithoutContext (std::string name, const CallbackBase &cb)
+{
+ TypeId tid = GetInstanceTypeId ();
+ Ptr<const TraceSourceAccessor> accessor = tid.LookupTraceSourceByName (name);
+ if (accessor == 0)
+ {
+ return false;
+ }
+ bool ok = accessor->DisconnectWithoutContext (this, cb);
+ return ok;
+}
+bool
+ObjectBase::TraceDisconnectWithoutContext (std::string name, std::string context, const CallbackBase &cb)
+{
+ TypeId tid = GetInstanceTypeId ();
+ Ptr<const TraceSourceAccessor> accessor = tid.LookupTraceSourceByName (name);
+ if (accessor == 0)
+ {
+ return false;
+ }
+ bool ok = accessor->Disconnect (this, context, cb);
+ return ok;
+}
+
+
+
+} // namespace ns3
--- a/src/core/object-base.h Sun Mar 16 21:52:15 2008 +0100
+++ b/src/core/object-base.h Mon Mar 17 05:22:29 2008 +0100
@@ -2,6 +2,9 @@
#define OBJECT_BASE_H
#include "type-id.h"
+#include "callback.h"
+#include "attribute-list.h"
+#include <string>
/**
* This macro should be invoked once for every class which
@@ -27,8 +30,61 @@
class ObjectBase
{
public:
+ static TypeId GetTypeId (void);
+
virtual ~ObjectBase ();
virtual TypeId GetInstanceTypeId (void) const = 0;
+
+ /**
+ * \param name the name of the attribute to set
+ * \param value the name of the attribute to set
+ *
+ * Set a single attribute. This cannot fail: if the input is invalid,
+ * it will crash immediately.
+ */
+ void SetAttribute (std::string name, Attribute value);
+ /**
+ * \param name the name of the attribute to set
+ * \param value the name of the attribute to set
+ * \returns true if the requested attribute exists and could be set,
+ * false otherwise.
+ */
+ bool SetAttributeFailSafe (std::string name, Attribute value);
+ /**
+ * \param name the name of the attribute to read
+ * \param value a reference to the string where the value of the
+ * attribute should be stored.
+ * \returns true if the requested attribute was found, false otherwise.
+ */
+ bool GetAttribute (std::string name, std::string &value) const;
+ /**
+ * \param name the name of the attribute to read
+ * \returns the attribute read.
+ *
+ * If the input attribute name does not exist, this method crashes.
+ */
+ Attribute GetAttribute (std::string name) const;
+
+ bool TraceConnectWithoutContext (std::string name, const CallbackBase &cb);
+ bool TraceConnectWithoutContext (std::string name, std::string context, const CallbackBase &cb);
+ bool TraceDisconnectWithoutContext (std::string name, const CallbackBase &cb);
+ bool TraceDisconnectWithoutContext (std::string name, std::string context, const CallbackBase &cb);
+
+protected:
+ virtual void NotifyConstructionCompleted (void);
+ /**
+ * \param attributes the attribute values used to initialize
+ * the member variables of this object's instance.
+ *
+ * Invoked from subclasses to initialize all of their
+ * attribute members.
+ */
+ void ConstructSelf (const AttributeList &attributes);
+
+private:
+ bool DoSet (Ptr<const AttributeAccessor> spec, Attribute intialValue,
+ Ptr<const AttributeChecker> checker, Attribute value);
+
};
} // namespace ns3
--- a/src/core/object.cc Sun Mar 16 21:52:15 2008 +0100
+++ b/src/core/object.cc Mon Mar 17 05:22:29 2008 +0100
@@ -38,14 +38,6 @@
NS_OBJECT_ENSURE_REGISTERED (Object);
-static TypeId
-GetObjectIid (void)
-{
- TypeId tid = TypeId ("ns3::Object");
- tid.SetParent (tid);
- return tid;
-}
-
TypeId
Object::GetInstanceTypeId (void) const
{
@@ -55,7 +47,9 @@
TypeId
Object::GetTypeId (void)
{
- static TypeId tid = GetObjectIid ();
+ static TypeId tid = TypeId ("ns3::Object")
+ .SetParent<ObjectBase> ()
+ ;
return tid;
}
@@ -73,212 +67,7 @@
void
Object::Construct (const AttributeList &attributes)
{
- // loop over the inheritance tree back to the Object base class.
- TypeId tid = m_tid;
- do {
- // loop over all attributes in object type
- NS_LOG_DEBUG ("construct tid="<<tid.GetName ()<<", params="<<tid.GetAttributeListN ());
- for (uint32_t i = 0; i < tid.GetAttributeListN (); i++)
- {
- Ptr<const AttributeAccessor> paramSpec = tid.GetAttributeAccessor (i);
- Attribute initial = tid.GetAttributeInitialValue (i);
- Ptr<const AttributeChecker> checker = tid.GetAttributeChecker (i);
- NS_LOG_DEBUG ("try to construct \""<< tid.GetName ()<<"::"<<
- tid.GetAttributeName (i)<<"\"");
- if (!(tid.GetAttributeFlags (i) & TypeId::ATTR_CONSTRUCT))
- {
- continue;
- }
- bool found = false;
- // is this attribute stored in this AttributeList instance ?
- for (AttributeList::Attrs::const_iterator j = attributes.m_attributes.begin ();
- j != attributes.m_attributes.end (); j++)
- {
- if (j->checker == checker)
- {
- // We have a matching attribute value.
- DoSet (paramSpec, initial, checker, j->value);
- NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<<
- tid.GetAttributeName (i)<<"\"");
- found = true;
- break;
- }
- }
- if (!found)
- {
- // is this attribute stored in the global instance instance ?
- for (AttributeList::Attrs::const_iterator j = AttributeList::GetGlobal ()->m_attributes.begin ();
- j != AttributeList::GetGlobal ()->m_attributes.end (); j++)
- {
- if (j->checker == checker)
- {
- // We have a matching attribute value.
- DoSet (paramSpec, initial, checker, j->value);
- NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<<
- tid.GetAttributeName (i)<<"\" from global");
- found = true;
- break;
- }
- }
- }
- if (!found)
- {
- // No matching attribute value so we set the default value.
- paramSpec->Set (this, initial);
- NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<<
- tid.GetAttributeName (i)<<"\" from initial value.");
- }
- }
- tid = tid.GetParent ();
- } while (tid != Object::GetTypeId ());
- NotifyConstructionCompleted ();
-}
-bool
-Object::DoSet (Ptr<const AttributeAccessor> spec, Attribute initialValue,
- Ptr<const AttributeChecker> checker, Attribute value)
-{
- bool ok = checker->Check (value);
- if (!ok)
- {
- // attempt to convert to string
- const StringValue *str = value.DynCast<const StringValue *> ();
- if (str == 0)
- {
- return false;
- }
- // attempt to convert back from string.
- Attribute v = checker->Create ();
- ok = v.DeserializeFromString (str->Get ().Get (), checker);
- if (!ok)
- {
- return false;
- }
- ok = checker->Check (v);
- if (!ok)
- {
- return false;
- }
- value = v;
- }
- ok = spec->Set (this, value);
- return ok;
-}
-void
-Object::SetAttribute (std::string name, Attribute value)
-{
- struct TypeId::AttributeInfo info;
- if (!m_tid.LookupAttributeByName (name, &info))
- {
- NS_FATAL_ERROR ("Attribute name="<<name<<" does not exist for this object: tid="<<m_tid.GetName ());
- }
- if (!(info.flags & TypeId::ATTR_SET))
- {
- NS_FATAL_ERROR ("Attribute name="<<name<<" is not settable for this object: tid="<<m_tid.GetName ());
- }
- if (!DoSet (info.accessor, info.initialValue, info.checker, value))
- {
- NS_FATAL_ERROR ("Attribute name="<<name<<" could not be set for this object: tid="<<m_tid.GetName ());
- }
-}
-bool
-Object::SetAttributeFailSafe (std::string name, Attribute value)
-{
- struct TypeId::AttributeInfo info;
- if (!m_tid.LookupAttributeByName (name, &info))
- {
- return false;
- }
- if (!(info.flags & TypeId::ATTR_SET))
- {
- return false;
- }
- return DoSet (info.accessor, info.initialValue, info.checker, value);
-}
-bool
-Object::GetAttribute (std::string name, std::string &value) const
-{
- struct TypeId::AttributeInfo info;
- if (!m_tid.LookupAttributeByName (name, &info))
- {
- return false;
- }
- if (!(info.flags & TypeId::ATTR_GET))
- {
- return false;
- }
- Attribute v = info.checker->Create ();
- bool ok = info.accessor->Get (this, v);
- if (ok)
- {
- value = v.SerializeToString (info.checker);
- }
- return ok;
-}
-
-Attribute
-Object::GetAttribute (std::string name) const
-{
- struct TypeId::AttributeInfo info;
- if (!m_tid.LookupAttributeByName (name, &info))
- {
- return Attribute ();
- }
- if (!(info.flags & TypeId::ATTR_GET))
- {
- return Attribute ();
- }
- Attribute value = info.checker->Create ();
- bool ok = info.accessor->Get (this, value);
- if (!ok)
- {
- return Attribute ();
- }
- return value;
-}
-
-bool
-Object::TraceConnectWithoutContext (std::string name, const CallbackBase &cb)
-{
- Ptr<const TraceSourceAccessor> accessor = m_tid.LookupTraceSourceByName (name);
- if (accessor == 0)
- {
- return false;
- }
- bool ok = accessor->ConnectWithoutContext (this, cb);
- return ok;
-}
-bool
-Object::TraceConnectWithoutContext (std::string name, std::string context, const CallbackBase &cb)
-{
- Ptr<const TraceSourceAccessor> accessor = m_tid.LookupTraceSourceByName (name);
- if (accessor == 0)
- {
- return false;
- }
- bool ok = accessor->Connect (this, context, cb);
- return ok;
-}
-bool
-Object::TraceDisconnectWithoutContext (std::string name, const CallbackBase &cb)
-{
- Ptr<const TraceSourceAccessor> accessor = m_tid.LookupTraceSourceByName (name);
- if (accessor == 0)
- {
- return false;
- }
- bool ok = accessor->DisconnectWithoutContext (this, cb);
- return ok;
-}
-bool
-Object::TraceDisconnectWithoutContext (std::string name, std::string context, const CallbackBase &cb)
-{
- Ptr<const TraceSourceAccessor> accessor = m_tid.LookupTraceSourceByName (name);
- if (accessor == 0)
- {
- return false;
- }
- bool ok = accessor->Disconnect (this, context, cb);
- return ok;
+ ConstructSelf (attributes);
}
Ptr<Object>
@@ -313,9 +102,6 @@
current = current->m_next;
} while (current != this);
}
-void
-Object::NotifyConstructionCompleted (void)
-{}
void
Object::AggregateObject (Ptr<Object> o)
{
--- a/src/core/object.h Sun Mar 16 21:52:15 2008 +0100
+++ b/src/core/object.h Mon Mar 17 05:22:29 2008 +0100
@@ -56,41 +56,6 @@
virtual TypeId GetInstanceTypeId (void) const;
/**
- * \param name the name of the attribute to set
- * \param value the name of the attribute to set
- *
- * Set a single attribute. This cannot fail: if the input is invalid,
- * it will crash immediately.
- */
- void SetAttribute (std::string name, Attribute value);
- /**
- * \param name the name of the attribute to set
- * \param value the name of the attribute to set
- * \returns true if the requested attribute exists and could be set,
- * false otherwise.
- */
- bool SetAttributeFailSafe (std::string name, Attribute value);
- /**
- * \param name the name of the attribute to read
- * \param value a reference to the string where the value of the
- * attribute should be stored.
- * \returns true if the requested attribute was found, false otherwise.
- */
- bool GetAttribute (std::string name, std::string &value) const;
- /**
- * \param name the name of the attribute to read
- * \returns the attribute read.
- *
- * If the input attribute name does not exist, this method crashes.
- */
- Attribute GetAttribute (std::string name) const;
-
- bool TraceConnectWithoutContext (std::string name, const CallbackBase &cb);
- bool TraceConnectWithoutContext (std::string name, std::string context, const CallbackBase &cb);
- bool TraceDisconnectWithoutContext (std::string name, const CallbackBase &cb);
- bool TraceDisconnectWithoutContext (std::string name, std::string context, const CallbackBase &cb);
-
- /**
* Increment the reference count. This method should not be called
* by user code. Object instances are expected to be used in conjunction
* of the Ptr template which would make calling Ref unecessary and
@@ -140,7 +105,6 @@
* up to their parent's implementation once they are done.
*/
virtual void DoDispose (void);
- virtual void NotifyConstructionCompleted (void);
private:
template <typename T>
friend Ptr<T> CreateObject (const AttributeList &attributes);
@@ -149,8 +113,6 @@
friend class ObjectFactory;
- bool DoSet (Ptr<const AttributeAccessor> spec, Attribute intialValue,
- Ptr<const AttributeChecker> checker, Attribute value);
Ptr<Object> DoGetObject (TypeId tid) const;
bool Check (void) const;
bool CheckLoose (void) const;
@@ -169,11 +131,11 @@
* keep track of the type of this object instance.
*/
void SetTypeId (TypeId tid);
- /**
+ /**
* \param attributes the attribute values used to initialize
* the member variables of this object's instance.
*
- * Invoked from ns3::CreateObject only.
+ * Invoked from ns3::ObjectFactory::Create and ns3::CreateObject only.
* Initialize all the member variables which were
* registered with the associated TypeId.
*/