# HG changeset patch # User Mathieu Lacage # Date 1205093234 -3600 # Node ID 15d5421022a1d15e3346f103be5dbe052e913831 # Parent d5cff2968984d69d09e4ad5826a5f3588761a72d add FailSafe versions of setters which could fail. diff -r d5cff2968984 -r 15d5421022a1 src/core/attribute-test.cc --- 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 p; - NS_TEST_ASSERT (params.Set ("AttributeObjectTest::TestBoolName", String ("false"))); + NS_TEST_ASSERT (params.SetFailSafe ("AttributeObjectTest::TestBoolName", String ("false"))); p = CreateObject (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 (); 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 (); boolV = p->GetAttribute ("TestBoolName"); NS_TEST_ASSERT_EQUAL (boolV, Boolean (false)); diff -r d5cff2968984 -r 15d5421022a1 src/core/config.cc --- 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::Get ()->Connect (path, cb); diff -r d5cff2968984 -r 15d5421022a1 src/core/config.h --- 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); diff -r d5cff2968984 -r 15d5421022a1 src/core/global-value.cc --- 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: "<GetName () == name) + { + return (*i)->SetValue (value); + } + } + return false; } GlobalValue::Iterator GlobalValue::Begin (void) diff -r d5cff2968984 -r 15d5421022a1 src/core/global-value.h --- 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 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); diff -r d5cff2968984 -r 15d5421022a1 src/core/object.cc --- 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::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 "<