2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-29 13:07:50 +00:00

[#1928] Copy assignment for class dictionary

This commit is contained in:
Marcin Siodelski 2021-07-20 20:19:45 +02:00
parent 7e77245c57
commit 611fed36be
3 changed files with 58 additions and 0 deletions

View File

@ -361,6 +361,19 @@ ClientClassDictionary::toElement() const {
return (result);
}
ClientClassDictionary&
ClientClassDictionary::operator=(const ClientClassDictionary& rhs) {
if (this != &rhs) {
list_->clear();
map_->clear();
for (auto cclass : *(rhs.list_)) {
ClientClassDefPtr copy(new ClientClassDef(*cclass));
addClass(copy);
}
}
return (*this);
}
std::list<std::string>
builtinNames = {
// DROP is not in this list because it is special but not built-in.

View File

@ -391,6 +391,12 @@ public:
return (!equals(other));
}
/// @brief Copy assignment operator.
///
/// @param rhs Client class dictionary to be copied from.
/// @return Instance copy.
ClientClassDictionary& operator=(const ClientClassDictionary& rhs);
/// @brief Unparse a configuration object
///
/// @return a pointer to unparsed configuration

View File

@ -431,6 +431,45 @@ TEST(ClientClassDictionary, copyAndEquality) {
EXPECT_TRUE(*dictionary != *dictionary2);
}
// Verify that client class dictionaries are deep-copied.
TEST(ClientClassDictionary, copy) {
ClientClassDictionary dictionary;
ExpressionPtr expr;
CfgOptionPtr options;
// Get a client class dictionary and fill it.
ASSERT_NO_THROW(dictionary.addClass("one", expr, "", false,
false, options));
ASSERT_NO_THROW(dictionary.addClass("two", expr, "", false,
false, options));
ASSERT_NO_THROW(dictionary.addClass("three", expr, "", false,
false, options));
// Make a copy with a copy constructor. Expect it to be a deep copy.
ClientClassDictionary dictionary_copy(dictionary);
ASSERT_NO_THROW(dictionary.removeClass("one"));
ASSERT_NO_THROW(dictionary.removeClass("two"));
ASSERT_NO_THROW(dictionary.removeClass("three"));
EXPECT_TRUE(dictionary.empty());
EXPECT_FALSE(dictionary_copy.empty());
// Refill the client class dictionary.
ASSERT_NO_THROW(dictionary.addClass("one", expr, "", false,
false, options));
ASSERT_NO_THROW(dictionary.addClass("two", expr, "", false,
false, options));
ASSERT_NO_THROW(dictionary.addClass("three", expr, "", false,
false, options));
// Make a copy with operator=. Expect it to be a deep copy.
dictionary_copy = dictionary;
ASSERT_NO_THROW(dictionary.removeClass("one"));
ASSERT_NO_THROW(dictionary.removeClass("two"));
ASSERT_NO_THROW(dictionary.removeClass("three"));
EXPECT_TRUE(dictionary.empty());
EXPECT_FALSE(dictionary_copy.empty());
}
// Tests dependency.
TEST(ClientClassDictionary, dependency) {
ClientClassDictionaryPtr dictionary(new ClientClassDictionary());