src/core/object-base.cc
changeset 2965 4b28e9740e3b
parent 2718 9fe723baa16b
child 4216 621e2ef9e7b8
--- a/src/core/object-base.cc	Mon Apr 14 16:19:17 2008 -0700
+++ b/src/core/object-base.cc	Thu Apr 17 13:42:25 2008 -0700
@@ -20,6 +20,7 @@
 #include "object-base.h"
 #include "log.h"
 #include "trace-source-accessor.h"
+#include "attribute-list.h"
 #include "string.h"
 
 NS_LOG_COMPONENT_DEFINE ("ObjectBase");
@@ -58,8 +59,8 @@
     NS_LOG_DEBUG ("construct tid="<<tid.GetName ()<<", params="<<tid.GetAttributeN ());
     for (uint32_t i = 0; i < tid.GetAttributeN (); i++)
       {
-        Ptr<const AttributeAccessor> paramSpec = tid.GetAttributeAccessor (i);
-        Attribute initial = tid.GetAttributeInitialValue (i);
+        Ptr<const AttributeAccessor> accessor = tid.GetAttributeAccessor (i);
+        Ptr<const AttributeValue> initial = tid.GetAttributeInitialValue (i);
         Ptr<const AttributeChecker> checker = tid.GetAttributeChecker (i);
         NS_LOG_DEBUG ("try to construct \""<< tid.GetName ()<<"::"<<
                       tid.GetAttributeName (i)<<"\"");
@@ -75,7 +76,7 @@
             if (j->checker == checker)
               {
                 // We have a matching attribute value.
-                DoSet (paramSpec, initial, checker, j->value);
+                DoSet (accessor, checker, *j->value);
                 NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<<
                               tid.GetAttributeName (i)<<"\"");
                 found = true;
@@ -91,7 +92,7 @@
                 if (j->checker == checker)
                   {
                     // We have a matching attribute value.
-                    DoSet (paramSpec, initial, checker, j->value);
+                    DoSet (accessor, checker, *j->value);
                     NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<<
                                   tid.GetAttributeName (i)<<"\" from global");
                     found = true;
@@ -102,7 +103,7 @@
         if (!found)
           {
             // No matching attribute value so we set the default value.
-            paramSpec->Set (this, initial);
+            DoSet (accessor, checker, *initial);
             NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<<
                           tid.GetAttributeName (i)<<"\" from initial value.");
           }
@@ -113,70 +114,39 @@
 }
 
 bool
-ObjectBase::DoSet (Ptr<const AttributeAccessor> spec, Attribute initialValue, 
-		   Ptr<const AttributeChecker> checker, Attribute value)
+ObjectBase::DoSet (Ptr<const AttributeAccessor> spec, 
+		   Ptr<const AttributeChecker> checker, 
+                   const AttributeValue &value)
 {
   bool ok = checker->Check (value);
+  if (ok)
+    {
+      ok = spec->Set (this, value);
+      return ok;
+    }
+  // attempt to convert to string
+  const StringValue *str = dynamic_cast<const StringValue *> (&value);
+  if (str == 0)
+    {
+      return false;
+    }
+  // attempt to convert back from string.
+  Ptr<AttributeValue> v = checker->Create ();
+  ok = v->DeserializeFromString (str->Get (), checker);
   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;
+      return false;
     }
-  ok = spec->Set (this, value);
+  ok = checker->Check (*v);
+  if (!ok)
+    {
+      return false;
+    }
+  ok = spec->Set (this, *v);
   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);
-}
-std::string
-ObjectBase::GetAttributeAsString (std::string name) const
+ObjectBase::SetAttribute (std::string name, const AttributeValue &value)
 {
   struct TypeId::AttributeInfo info;
   TypeId tid = GetInstanceTypeId ();
@@ -184,44 +154,18 @@
     {
       NS_FATAL_ERROR ("Attribute name="<<name<<" does not exist for this object: tid="<<tid.GetName ());
     }
-  if (!(info.flags & TypeId::ATTR_GET))
+  if (!(info.flags & TypeId::ATTR_SET) ||
+      !info.accessor->HasSetter ())
     {
-      NS_FATAL_ERROR ("Attribute name="<<name<<" is not gettable for this object: tid="<<tid.GetName ());
+      NS_FATAL_ERROR ("Attribute name="<<name<<" is not settable for this object: tid="<<tid.GetName ());
     }
-  Attribute v = info.checker->Create ();
-  bool ok = info.accessor->Get (this, v);
-  if (!ok)
+  if (!DoSet (info.accessor, info.checker, value))
     {
-      NS_FATAL_ERROR ("Attribute name="<<name<<" is not gettable for this object: tid="<<tid.GetName ());
+      NS_FATAL_ERROR ("Attribute name="<<name<<" could not be set for this object: tid="<<tid.GetName ());
     }
-  std::string value = v.SerializeToString (info.checker);
-  return value;
 }
-
-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::GetAttributeAsStringFailSafe (std::string name, std::string &value) const
+bool 
+ObjectBase::SetAttributeFailSafe (std::string name, const AttributeValue &value)
 {
   struct TypeId::AttributeInfo info;
   TypeId tid = GetInstanceTypeId ();
@@ -229,21 +173,50 @@
     {
       return false;
     }
-  if (!(info.flags & TypeId::ATTR_GET))
+  if (!(info.flags & TypeId::ATTR_SET) ||
+      !info.accessor->HasSetter ())
     {
       return false;
     }
-  Attribute v = info.checker->Create ();
-  bool ok = info.accessor->Get (this, v);
+  return DoSet (info.accessor, info.checker, value);
+}
+
+void
+ObjectBase::GetAttribute (std::string name, AttributeValue &value) const
+{
+  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_GET) || 
+      !info.accessor->HasGetter ())
+    {
+      NS_FATAL_ERROR ("Attribute name="<<name<<" is not gettable for this object: tid="<<tid.GetName ());
+    }
+  bool ok = info.accessor->Get (this, value);
   if (ok)
     {
-      value = v.SerializeToString (info.checker);
+      return;
+    }
+  StringValue *str = dynamic_cast<StringValue *> (&value);
+  if (str == 0)
+    {
+      NS_FATAL_ERROR ("Attribute name="<<name<<" tid="<<tid.GetName () << ": input value is not a string");
     }
-  return ok;
+  Ptr<AttributeValue> v = info.checker->Create ();
+  ok = info.accessor->Get (this, *PeekPointer (v));
+  if (!ok)
+    {
+      NS_FATAL_ERROR ("Attribute name="<<name<<" tid="<<tid.GetName () << ": could not get value");
+    }
+  str->Set (v->SerializeToString (info.checker));
 }
 
+
 bool
-ObjectBase::GetAttributeFailSafe (std::string name, Attribute &value) const
+ObjectBase::GetAttributeFailSafe (std::string name, AttributeValue &value) const
 {
   struct TypeId::AttributeInfo info;
   TypeId tid = GetInstanceTypeId ();
@@ -251,13 +224,29 @@
     {
       return false;
     }
-  if (!(info.flags & TypeId::ATTR_GET))
+  if (!(info.flags & TypeId::ATTR_GET) ||
+      !info.accessor->HasGetter ())
     {
       return false;
     }
-  value = info.checker->Create ();
   bool ok = info.accessor->Get (this, value);
-  return ok;
+  if (ok)
+    {
+      return true;
+    }
+  StringValue *str = dynamic_cast<StringValue *> (&value);
+  if (str == 0)
+    {
+      return false;
+    }
+  Ptr<AttributeValue> v = info.checker->Create ();
+  ok = info.accessor->Get (this, *PeekPointer (v));
+  if (!ok)
+    {
+      return false;
+    }
+  str->Set (v->SerializeToString (info.checker));
+  return true;
 }
 
 bool