add FailSafe versions of setters which could fail.
--- a/src/core/attribute-test.cc Fri Mar 07 12:18:34 2008 -0800
+++ b/src/core/attribute-test.cc Sun Mar 09 21:07:14 2008 +0100
@@ -240,7 +240,7 @@
AttributeList params;
Ptr<AttributeObjectTest> p;
- NS_TEST_ASSERT (params.Set ("AttributeObjectTest::TestBoolName", String ("false")));
+ NS_TEST_ASSERT (params.SetFailSafe ("AttributeObjectTest::TestBoolName", String ("false")));
p = CreateObject<AttributeObjectTest> (params);
CHECK_GET_STR (p, "TestBoolName", "false");
CHECK_GET_PARAM (p, "TestBoolName", Boolean, false);
@@ -408,12 +408,12 @@
NS_TEST_ASSERT_EQUAL (vector.GetN (), 2);
}
- NS_TEST_ASSERT (AttributeList::GetGlobal ()->Set ("AttributeObjectTest::TestBoolName", String ("true")));
+ NS_TEST_ASSERT (AttributeList::GetGlobal ()->SetFailSafe ("AttributeObjectTest::TestBoolName", String ("true")));
p = CreateObjectWith<AttributeObjectTest> ();
Boolean boolV = p->GetAttribute ("TestBoolName");
NS_TEST_ASSERT_EQUAL (boolV, Boolean (true));
- NS_TEST_ASSERT (AttributeList::GetGlobal ()->Set ("AttributeObjectTest::TestBoolName", String ("false")));
+ NS_TEST_ASSERT (AttributeList::GetGlobal ()->SetFailSafe ("AttributeObjectTest::TestBoolName", String ("false")));
p = CreateObjectWith<AttributeObjectTest> ();
boolV = p->GetAttribute ("TestBoolName");
NS_TEST_ASSERT_EQUAL (boolV, Boolean (false));
--- a/src/core/config.cc Fri Mar 07 12:18:34 2008 -0800
+++ b/src/core/config.cc Sun Mar 09 21:07:14 2008 +0100
@@ -409,10 +409,18 @@
{
AttributeList::GetGlobal ()->Set (name, value);
}
+bool SetDefaultFailSafe (std::string name, Attribute value)
+{
+ return AttributeList::GetGlobal ()->SetFailSafe (name, value);
+}
void SetGlobal (std::string name, Attribute value)
{
GlobalValue::Bind (name, value);
}
+bool SetGlobalFailSafe (std::string name, Attribute value)
+{
+ return GlobalValue::BindFailSafe (name, value);
+}
void Connect (std::string path, const CallbackBase &cb)
{
Singleton<ConfigImpl>::Get ()->Connect (path, cb);
--- a/src/core/config.h Fri Mar 07 12:18:34 2008 -0800
+++ b/src/core/config.h Sun Mar 09 21:07:14 2008 +0100
@@ -12,7 +12,9 @@
void Set (std::string path, Attribute value);
void SetDefault (std::string name, Attribute value);
+bool SetDefaultFailSafe (std::string name, Attribute value);
void SetGlobal (std::string name, Attribute value);
+bool SetGlobalFailSafe (std::string name, Attribute value);
void Connect (std::string path, const CallbackBase &cb);
void Disconnect (std::string path, const CallbackBase &cb);
void ConnectWithContext (std::string path, const CallbackBase &cb);
--- a/src/core/global-value.cc Fri Mar 07 12:18:34 2008 -0800
+++ b/src/core/global-value.cc Sun Mar 09 21:07:14 2008 +0100
@@ -40,14 +40,15 @@
return m_checker;
}
-void
+bool
GlobalValue::SetValue (Attribute value)
{
if (!m_checker->Check (value))
{
- NS_FATAL_ERROR ("Invalid new value.");
+ return false;
}
m_initialValue = value;
+ return true;
}
void
@@ -57,10 +58,26 @@
{
if ((*i)->GetName () == name)
{
- (*i)->SetValue (value);
+ if (!(*i)->SetValue (value))
+ {
+ NS_FATAL_ERROR ("Invalid new value for global value: "<<name);
+ }
return;
}
}
+ NS_FATAL_ERROR ("Non-existant global value: "<<name);
+}
+bool
+GlobalValue::BindFailSafe (std::string name, Attribute value)
+{
+ for (Iterator i = Begin (); i != End (); i++)
+ {
+ if ((*i)->GetName () == name)
+ {
+ return (*i)->SetValue (value);
+ }
+ }
+ return false;
}
GlobalValue::Iterator
GlobalValue::Begin (void)
--- a/src/core/global-value.h Fri Mar 07 12:18:34 2008 -0800
+++ b/src/core/global-value.h Sun Mar 09 21:07:14 2008 +0100
@@ -24,9 +24,10 @@
Attribute GetValue (void) const;
Ptr<const AttributeChecker> GetChecker (void) const;
- void SetValue (Attribute value);
+ bool SetValue (Attribute value);
static void Bind (std::string name, Attribute value);
+ static bool BindFailSafe (std::string name, Attribute value);
static Iterator Begin (void);
static Iterator End (void);
--- a/src/core/object.cc Fri Mar 07 12:18:34 2008 -0800
+++ b/src/core/object.cc Sun Mar 09 21:07:14 2008 +0100
@@ -403,6 +403,18 @@
return TypeId (uid);
}
bool
+TypeId::LookupByNameFailSafe (std::string name, TypeId *tid)
+{
+ uint16_t uid = Singleton<IidManager>::Get ()->GetUid (name);
+ if (uid == 0)
+ {
+ return false;
+ }
+ *tid = TypeId (uid);
+ return true;
+}
+
+bool
TypeId::LookupAttributeByFullName (std::string fullName, struct TypeId::AttributeInfo *info)
{
std::string::size_type pos = fullName.find ("::");
@@ -412,7 +424,12 @@
}
std::string tidName = fullName.substr (0, pos);
std::string paramName = fullName.substr (pos+2, fullName.size () - (pos+2));
- TypeId tid = LookupByName (tidName);
+ TypeId tid;
+ bool ok = LookupByNameFailSafe (tidName, &tid);
+ if (!ok)
+ {
+ return false;
+ }
return tid.LookupAttributeByName (paramName, info);
}
uint32_t
@@ -683,7 +700,11 @@
{
std::string tidString;
is >> tidString;
- tid = TypeId::LookupByName (tidString);
+ bool ok = TypeId::LookupByNameFailSafe (tidString, &tid);
+ if (!ok)
+ {
+ is.setstate (std::ios_base::badbit);
+ }
return is;
}
@@ -735,8 +756,23 @@
Reset ();
}
+void
+AttributeList::Set (std::string name, Attribute value)
+{
+ struct TypeId::AttributeInfo info;
+ bool ok = TypeId::LookupAttributeByFullName (name, &info);
+ if (!ok)
+ {
+ NS_FATAL_ERROR ("Could not find attribute "<<name);
+ }
+ ok = DoSet (&info, value);
+ if (!ok)
+ {
+ NS_FATAL_ERROR ("Could not set value for attribute "<<name);
+ }
+}
bool
-AttributeList::Set (std::string name, Attribute value)
+AttributeList::SetFailSafe (std::string name, Attribute value)
{
struct TypeId::AttributeInfo info;
TypeId::LookupAttributeByFullName (name, &info);
--- a/src/core/object.h Fri Mar 07 12:18:34 2008 -0800
+++ b/src/core/object.h Sun Mar 09 21:07:14 2008 +0100
@@ -69,7 +69,7 @@
};
/**
- * \param name the name of the requested interface
+ * \param name the name of the requested TypeId
* \returns the unique id associated with the requested
* name.
*
@@ -77,6 +77,13 @@
* name is not a valid interface name.
*/
static TypeId LookupByName (std::string name);
+ /**
+ * \param name the name of the requested TypeId
+ * \param tid a pointer to the TypeId instance where the
+ * result of this function should be stored.
+ * \returns true if the requested name was found, false otherwise.
+ */
+ static bool LookupByNameFailSafe (std::string name, TypeId *tid);
/**
* \returns the number of TypeId instances constructed
@@ -316,7 +323,8 @@
* value of that attribute. If any of these checks fails,
* the program terminates with a message.
*/
- bool Set (std::string name, Attribute value);
+ void Set (std::string name, Attribute value);
+ bool SetFailSafe (std::string name, Attribute value);
bool SetWithTid (TypeId tid, std::string name, Attribute value);
bool SetWithTid (TypeId tid, uint32_t position, Attribute value);