add FailSafe versions of setters which could fail.
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sun, 09 Mar 2008 21:07:14 +0100
changeset 257015d5421022a1
parent 2569 d5cff2968984
child 2571 fd0fa0cec413
add FailSafe versions of setters which could fail.
src/core/attribute-test.cc
src/core/config.cc
src/core/config.h
src/core/global-value.cc
src/core/global-value.h
src/core/object.cc
src/core/object.h
     1.1 --- a/src/core/attribute-test.cc	Fri Mar 07 12:18:34 2008 -0800
     1.2 +++ b/src/core/attribute-test.cc	Sun Mar 09 21:07:14 2008 +0100
     1.3 @@ -240,7 +240,7 @@
     1.4  
     1.5    AttributeList params;
     1.6    Ptr<AttributeObjectTest> p;
     1.7 -  NS_TEST_ASSERT (params.Set ("AttributeObjectTest::TestBoolName", String ("false")));
     1.8 +  NS_TEST_ASSERT (params.SetFailSafe ("AttributeObjectTest::TestBoolName", String ("false")));
     1.9    p = CreateObject<AttributeObjectTest> (params);
    1.10    CHECK_GET_STR (p, "TestBoolName", "false");
    1.11    CHECK_GET_PARAM (p, "TestBoolName", Boolean, false);
    1.12 @@ -408,12 +408,12 @@
    1.13      NS_TEST_ASSERT_EQUAL (vector.GetN (), 2);
    1.14    }
    1.15  
    1.16 -  NS_TEST_ASSERT (AttributeList::GetGlobal ()->Set ("AttributeObjectTest::TestBoolName", String ("true")));
    1.17 +  NS_TEST_ASSERT (AttributeList::GetGlobal ()->SetFailSafe ("AttributeObjectTest::TestBoolName", String ("true")));
    1.18    p = CreateObjectWith<AttributeObjectTest> ();
    1.19    Boolean boolV = p->GetAttribute ("TestBoolName");
    1.20    NS_TEST_ASSERT_EQUAL (boolV, Boolean (true));
    1.21  
    1.22 -  NS_TEST_ASSERT (AttributeList::GetGlobal ()->Set ("AttributeObjectTest::TestBoolName", String ("false")));
    1.23 +  NS_TEST_ASSERT (AttributeList::GetGlobal ()->SetFailSafe ("AttributeObjectTest::TestBoolName", String ("false")));
    1.24    p = CreateObjectWith<AttributeObjectTest> ();
    1.25    boolV = p->GetAttribute ("TestBoolName");
    1.26    NS_TEST_ASSERT_EQUAL (boolV, Boolean (false));
     2.1 --- a/src/core/config.cc	Fri Mar 07 12:18:34 2008 -0800
     2.2 +++ b/src/core/config.cc	Sun Mar 09 21:07:14 2008 +0100
     2.3 @@ -409,10 +409,18 @@
     2.4  {
     2.5    AttributeList::GetGlobal ()->Set (name, value);
     2.6  }
     2.7 +bool SetDefaultFailSafe (std::string name, Attribute value)
     2.8 +{
     2.9 +  return AttributeList::GetGlobal ()->SetFailSafe (name, value);
    2.10 +}
    2.11  void SetGlobal (std::string name, Attribute value)
    2.12  {
    2.13    GlobalValue::Bind (name, value);
    2.14  }
    2.15 +bool SetGlobalFailSafe (std::string name, Attribute value)
    2.16 +{
    2.17 +  return GlobalValue::BindFailSafe (name, value);
    2.18 +}
    2.19  void Connect (std::string path, const CallbackBase &cb)
    2.20  {
    2.21    Singleton<ConfigImpl>::Get ()->Connect (path, cb);
     3.1 --- a/src/core/config.h	Fri Mar 07 12:18:34 2008 -0800
     3.2 +++ b/src/core/config.h	Sun Mar 09 21:07:14 2008 +0100
     3.3 @@ -12,7 +12,9 @@
     3.4  
     3.5  void Set (std::string path, Attribute value);
     3.6  void SetDefault (std::string name, Attribute value);
     3.7 +bool SetDefaultFailSafe (std::string name, Attribute value);
     3.8  void SetGlobal (std::string name, Attribute value);
     3.9 +bool SetGlobalFailSafe (std::string name, Attribute value);
    3.10  void Connect (std::string path, const CallbackBase &cb);
    3.11  void Disconnect (std::string path, const CallbackBase &cb);
    3.12  void ConnectWithContext (std::string path, const CallbackBase &cb);
     4.1 --- a/src/core/global-value.cc	Fri Mar 07 12:18:34 2008 -0800
     4.2 +++ b/src/core/global-value.cc	Sun Mar 09 21:07:14 2008 +0100
     4.3 @@ -40,14 +40,15 @@
     4.4    return m_checker;
     4.5  }
     4.6    
     4.7 -void 
     4.8 +bool
     4.9  GlobalValue::SetValue (Attribute value)
    4.10  {
    4.11    if (!m_checker->Check (value))
    4.12      {
    4.13 -      NS_FATAL_ERROR ("Invalid new value.");
    4.14 +      return false;
    4.15      }
    4.16    m_initialValue = value;
    4.17 +  return true;
    4.18  }
    4.19  
    4.20  void 
    4.21 @@ -57,10 +58,26 @@
    4.22      {
    4.23        if ((*i)->GetName () == name)
    4.24  	{
    4.25 -	  (*i)->SetValue (value);
    4.26 +	  if (!(*i)->SetValue (value))
    4.27 +	    {
    4.28 +	      NS_FATAL_ERROR ("Invalid new value for global value: "<<name);
    4.29 +	    }
    4.30  	  return;
    4.31  	}
    4.32      }
    4.33 +  NS_FATAL_ERROR ("Non-existant global value: "<<name);
    4.34 +}
    4.35 +bool 
    4.36 +GlobalValue::BindFailSafe (std::string name, Attribute value)
    4.37 +{
    4.38 +  for (Iterator i = Begin (); i != End (); i++)
    4.39 +    {
    4.40 +      if ((*i)->GetName () == name)
    4.41 +	{
    4.42 +	  return (*i)->SetValue (value);
    4.43 +	}
    4.44 +    }
    4.45 +  return false;
    4.46  }
    4.47  GlobalValue::Iterator 
    4.48  GlobalValue::Begin (void)
     5.1 --- a/src/core/global-value.h	Fri Mar 07 12:18:34 2008 -0800
     5.2 +++ b/src/core/global-value.h	Sun Mar 09 21:07:14 2008 +0100
     5.3 @@ -24,9 +24,10 @@
     5.4    Attribute GetValue (void) const;
     5.5    Ptr<const AttributeChecker> GetChecker (void) const;
     5.6    
     5.7 -  void SetValue (Attribute value);
     5.8 +  bool SetValue (Attribute value);
     5.9  
    5.10    static void Bind (std::string name, Attribute value);
    5.11 +  static bool BindFailSafe (std::string name, Attribute value);
    5.12  
    5.13    static Iterator Begin (void);
    5.14    static Iterator End (void);
     6.1 --- a/src/core/object.cc	Fri Mar 07 12:18:34 2008 -0800
     6.2 +++ b/src/core/object.cc	Sun Mar 09 21:07:14 2008 +0100
     6.3 @@ -403,6 +403,18 @@
     6.4    return TypeId (uid);
     6.5  }
     6.6  bool
     6.7 +TypeId::LookupByNameFailSafe (std::string name, TypeId *tid)
     6.8 +{
     6.9 +  uint16_t uid = Singleton<IidManager>::Get ()->GetUid (name);
    6.10 +  if (uid == 0)
    6.11 +    {
    6.12 +      return false;
    6.13 +    }
    6.14 +  *tid = TypeId (uid);
    6.15 +  return true;
    6.16 +}
    6.17 +
    6.18 +bool
    6.19  TypeId::LookupAttributeByFullName (std::string fullName, struct TypeId::AttributeInfo *info)
    6.20  {
    6.21    std::string::size_type pos = fullName.find ("::");
    6.22 @@ -412,7 +424,12 @@
    6.23      }
    6.24    std::string tidName = fullName.substr (0, pos);
    6.25    std::string paramName = fullName.substr (pos+2, fullName.size () - (pos+2));
    6.26 -  TypeId tid = LookupByName (tidName);
    6.27 +  TypeId tid;
    6.28 +  bool ok = LookupByNameFailSafe (tidName, &tid);
    6.29 +  if (!ok)
    6.30 +    {
    6.31 +      return false;
    6.32 +    }
    6.33    return tid.LookupAttributeByName (paramName, info);
    6.34  }
    6.35  uint32_t 
    6.36 @@ -683,7 +700,11 @@
    6.37  {
    6.38    std::string tidString;
    6.39    is >> tidString;
    6.40 -  tid = TypeId::LookupByName (tidString);
    6.41 +  bool ok = TypeId::LookupByNameFailSafe (tidString, &tid);
    6.42 +  if (!ok)
    6.43 +    {
    6.44 +      is.setstate (std::ios_base::badbit);
    6.45 +    }
    6.46    return is;
    6.47  }
    6.48  
    6.49 @@ -735,8 +756,23 @@
    6.50    Reset ();
    6.51  }
    6.52  
    6.53 +void
    6.54 +AttributeList::Set (std::string name, Attribute value)
    6.55 +{
    6.56 +  struct TypeId::AttributeInfo info;
    6.57 +  bool ok = TypeId::LookupAttributeByFullName (name, &info);
    6.58 +  if (!ok)
    6.59 +    {
    6.60 +      NS_FATAL_ERROR ("Could not find attribute "<<name);
    6.61 +    }
    6.62 +  ok = DoSet (&info, value);
    6.63 +  if (!ok)
    6.64 +    {
    6.65 +      NS_FATAL_ERROR ("Could not set value for attribute "<<name);
    6.66 +    }
    6.67 +}
    6.68  bool 
    6.69 -AttributeList::Set (std::string name, Attribute value)
    6.70 +AttributeList::SetFailSafe (std::string name, Attribute value)
    6.71  {
    6.72    struct TypeId::AttributeInfo info;
    6.73    TypeId::LookupAttributeByFullName (name, &info);
     7.1 --- a/src/core/object.h	Fri Mar 07 12:18:34 2008 -0800
     7.2 +++ b/src/core/object.h	Sun Mar 09 21:07:14 2008 +0100
     7.3 @@ -69,7 +69,7 @@
     7.4    };
     7.5  
     7.6    /**
     7.7 -   * \param name the name of the requested interface
     7.8 +   * \param name the name of the requested TypeId
     7.9     * \returns the unique id associated with the requested
    7.10     *          name. 
    7.11     *
    7.12 @@ -77,6 +77,13 @@
    7.13     * name is not a valid interface name.
    7.14     */
    7.15    static TypeId LookupByName (std::string name);
    7.16 +  /**
    7.17 +   * \param name the name of the requested TypeId
    7.18 +   * \param tid a pointer to the TypeId instance where the 
    7.19 +   *        result of this function should be stored.
    7.20 +   * \returns true if the requested name was found, false otherwise.
    7.21 +   */
    7.22 +  static bool LookupByNameFailSafe (std::string name, TypeId *tid);
    7.23  
    7.24    /**
    7.25     * \returns the number of TypeId instances constructed
    7.26 @@ -316,7 +323,8 @@
    7.27     * value of that attribute. If any of these checks fails,
    7.28     * the program terminates with a message.
    7.29     */
    7.30 -  bool Set (std::string name, Attribute value);
    7.31 +  void Set (std::string name, Attribute value);
    7.32 +  bool SetFailSafe (std::string name, Attribute value);
    7.33  
    7.34    bool SetWithTid (TypeId tid, std::string name, Attribute value);
    7.35    bool SetWithTid (TypeId tid, uint32_t position, Attribute value);