Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions include/fkYAML/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,32 @@ class basic_node {
return {*this};
}

/// @brief Remove a mapping entry by key.
/// @tparam KeyType A type for the input key (any type convertible to node).
/// @param key A key identifying the mapping entry to remove.
/// @return true if an entry was found and removed, false otherwise.
template <typename KeyType>
bool remove(KeyType&& key)
{
basic_node key_node = std::forward<KeyType>(key);
basic_node& act_node = resolve_reference();
if FK_YAML_UNLIKELY (!act_node.is_mapping_impl())
{
throw type_error("remove() cannot be called on a non-mapping node.", get_type());
}

auto& map = *act_node.m_value.p_map;
for (auto itr = map.begin(); itr != map.end(); ++itr)
{
if (itr->first == key_node)
{
map.erase(itr);
return true;
}
}
return false;
}

private:
/// @brief Resolves anchor/alias reference and returns reference to an actual value node.
/// @return Reference to an actual value node.
Expand Down
26 changes: 26 additions & 0 deletions single_include/fkYAML/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14375,6 +14375,32 @@ class basic_node {
return {*this};
}

/// @brief Erase a mapping entry by key.
/// @tparam KeyType A type for the input key (any type convertible to node).
/// @param key A key identifying the mapping entry to erase.
/// @return true if an entry was found and erased, false otherwise.
template <typename KeyType>
bool erase(KeyType&& key)
{
basic_node key_node = std::forward<KeyType>(key);
basic_node& act_node = resolve_reference();
if FK_YAML_UNLIKELY (!act_node.is_mapping_impl())
{
throw type_error("remove() cannot be called on a non-mapping node.", get_type());
}

auto& map = *act_node.m_value.p_map;
for (auto itr = map.begin(); itr != map.end(); ++itr)
{
if (itr->first == key_node)
{
map.erase(itr);
return true;
}
}
return false;
}

private:
/// @brief Resolves anchor/alias reference and returns reference to an actual value node.
/// @return Reference to an actual value node.
Expand Down