Skip to content

Commit

Permalink
Methods for removing attributes from an element (#555)
Browse files Browse the repository at this point in the history
Signed-off-by: Jenn Nguyen <[email protected]>
  • Loading branch information
jennuine authored Apr 29, 2021
1 parent 066b2fe commit 9858c22
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/sdf/Element.hh
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ namespace sdf
/// \return True if the attribute is set, false otherwise.
public: bool GetAttributeSet(const std::string &_key) const;

/// \brief Remove an attribute.
/// \param[in] _key the key of the attribute.
public: void RemoveAttribute(const std::string &_key);

/// \brief Removes all attributes.
public: void RemoveAllAttributes();

/// \brief Get the param of the elements value
/// return A Param pointer to the value of this element.
public: ParamPtr GetValue() const;
Expand Down
21 changes: 21 additions & 0 deletions src/Element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,27 @@ bool Element::GetAttributeSet(const std::string &_key) const
return result;
}

/////////////////////////////////////////////////
void Element::RemoveAttribute(const std::string &_key)
{
Param_V::const_iterator iter;
for (iter = this->dataPtr->attributes.begin();
iter != this->dataPtr->attributes.end(); ++iter)
{
if ((*iter)->GetKey() == _key)
{
this->dataPtr->attributes.erase(iter);
break;
}
}
}

/////////////////////////////////////////////////
void Element::RemoveAllAttributes()
{
this->dataPtr->attributes.clear();
}

/////////////////////////////////////////////////
ParamPtr Element::GetAttribute(const std::string &_key) const
{
Expand Down
34 changes: 34 additions & 0 deletions src/Element_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,40 @@ TEST(Element, GetAttributeSet)
EXPECT_TRUE(elem.GetAttributeSet("test"));
}

/////////////////////////////////////////////////
TEST(Element, RemoveAttribute)
{
sdf::Element elem;
ASSERT_EQ(elem.GetAttributeCount(), 0UL);

elem.AddAttribute("test", "string", "foo", false, "foo description");
elem.AddAttribute("attr", "float", "0.0", false, "float description");
ASSERT_EQ(elem.GetAttributeCount(), 2UL);

elem.RemoveAttribute("test");
EXPECT_EQ(elem.GetAttributeCount(), 1UL);
EXPECT_EQ(elem.GetAttribute("test"), nullptr);
EXPECT_NE(elem.GetAttribute("attr"), nullptr);
}

/////////////////////////////////////////////////
TEST(Element, RemoveAllAttributes)
{
sdf::Element elem;
ASSERT_EQ(elem.GetAttributeCount(), 0UL);

elem.AddAttribute("test", "string", "foo", false, "foo description");
elem.AddAttribute("test2", "string", "foo", false, "foo description");
elem.AddAttribute("attr", "float", "0.0", false, "float description");
ASSERT_EQ(elem.GetAttributeCount(), 3UL);

elem.RemoveAllAttributes();
EXPECT_EQ(elem.GetAttributeCount(), 0UL);
EXPECT_EQ(elem.GetAttribute("test"), nullptr);
EXPECT_EQ(elem.GetAttribute("test2"), nullptr);
EXPECT_EQ(elem.GetAttribute("attr"), nullptr);
}

/////////////////////////////////////////////////
TEST(Element, Include)
{
Expand Down

0 comments on commit 9858c22

Please sign in to comment.