Skip to content

Commit

Permalink
Add Identifiable::removeProperty() (#416)
Browse files Browse the repository at this point in the history
Signed-off-by: Sébastien LAIGRE <[email protected]>
  • Loading branch information
sebalaig committed May 24, 2022
1 parent 61bb7f4 commit 7c21172
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
2 changes: 2 additions & 0 deletions include/powsybl/iidm/Identifiable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class Identifiable : public virtual Validable, public Extendable, public MultiVa

void removeAlias(const std::string& alias);

bool removeProperty(const std::string& key);

virtual void setFictitious(bool fictitious);

stdcxx::optional<std::string> setProperty(const std::string& key, const std::string& value);
Expand Down
2 changes: 1 addition & 1 deletion include/powsybl/stdcxx/Properties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Properties {

bool isEmpty() const;

Properties& remove(const std::string& key);
stdcxx::optional<std::string> remove(const std::string& key);

stdcxx::optional<std::string> set(const std::string& key, const std::string& value);

Expand Down
4 changes: 4 additions & 0 deletions src/iidm/Identifiable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ void Identifiable::removeAlias(const std::string& alias) {
}
}

bool Identifiable::removeProperty(const std::string& key) {
return static_cast<bool>(m_properties.remove(key));
}

void Identifiable::setFictitious(bool fictitious) {
m_fictitious = fictitious;
}
Expand Down
11 changes: 8 additions & 3 deletions src/stdcxx/Properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,14 @@ bool Properties::isEmpty() const {
return m_properties.empty();
}

Properties& Properties::remove(const std::string& key) {
m_properties.erase(key);
return *this;
stdcxx::optional<std::string> Properties::remove(const std::string& key) {
auto it = m_properties.find(key);
if (it != m_properties.end()) {
std::string oldValue = it->second;
m_properties.erase(it);
return oldValue;
}
return {};
}

stdcxx::optional<std::string> Properties::set(const std::string& key, const std::string& value) {
Expand Down
11 changes: 9 additions & 2 deletions test/iidm/NetworkTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,16 @@ BOOST_AUTO_TEST_CASE(Properties) {

BOOST_CHECK_EQUAL("value1", n.getProperty("key1"));

POWSYBL_ASSERT_THROW(n.getProperty("key3"),
stdcxx::PropertyNotFoundException, "Property key3 does not exist");
POWSYBL_ASSERT_THROW(n.getProperty("key3"), stdcxx::PropertyNotFoundException, "Property key3 does not exist");
BOOST_CHECK_EQUAL("value3", n.getProperty("key3", "value3"));

BOOST_CHECK_EQUAL(2, boost::size(n.getPropertyNames()));
BOOST_CHECK(n.removeProperty("key2"));
BOOST_CHECK_EQUAL(1, boost::size(n.getPropertyNames()));

BOOST_CHECK(!n.hasProperty("key2"));

BOOST_CHECK(!n.removeProperty("badKey"));
}

BOOST_AUTO_TEST_CASE(MultiVariant) {
Expand Down
8 changes: 4 additions & 4 deletions test/stdcxx/PropertiesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ BOOST_AUTO_TEST_CASE(RemoveProperty) {
props.set("key4", "value4");
BOOST_CHECK_EQUAL(4UL, props.size());

props.remove("key3");
BOOST_CHECK_EQUAL("value3", *props.remove("key3"));
BOOST_CHECK_EQUAL(3UL, props.size());

props.remove("key4");
BOOST_CHECK_EQUAL("value4", *props.remove("key4"));
BOOST_CHECK_EQUAL(2UL, props.size());

// removing a key that does not exists does nothing
props.remove("key5");
// removing a key that does not exists does nothing but returning an invalid stdcxx::optional
BOOST_CHECK(!props.remove("key5"));
BOOST_CHECK_EQUAL(2UL, props.size());

char suffix = '1';
Expand Down

0 comments on commit 7c21172

Please sign in to comment.