config: Add copy_or_remove_attributes()

Unlike copy_attributes(), this erases attributes in the destination that
don't exist in the source.
This commit is contained in:
Iris Morelle 2021-11-12 16:28:34 -03:00 committed by Gunter Labes
parent b79d99844e
commit 9b23565b0c

View file

@ -525,6 +525,12 @@ public:
}
}
/**
* Copies attributes that exist in the source config.
*
* @param from Source config to copy attributes from.
* @param keys Attribute names.
*/
template<typename... T>
void copy_attributes(const config& from, T... keys)
{
@ -536,6 +542,27 @@ public:
}
}
/**
* Copies or deletes attributes to match the source config.
*
* Attributes that do not exist in the source are fully erased rather than
* set to the unspecified/default attribute value.
*
* @param from Source config to copy attributes from.
* @param keys Attribute names.
*/
template<typename... T>
void copy_or_remove_attributes(const config& from, T... keys)
{
for(const auto& key : {keys...}) {
if(from.has_attribute(key)) {
(*this)[key] = from[key];
} else {
remove_attribute(key);
}
}
}
const_attr_itors attribute_range() const;
attr_itors attribute_range();