diff --git a/src/libs/utils/algorithm.h b/src/libs/utils/algorithm.h index ae4a718c836..32ad6d3e46f 100644 --- a/src/libs/utils/algorithm.h +++ b/src/libs/utils/algorithm.h @@ -76,6 +76,8 @@ bool allOf(const T &container, F predicate); ///////////////////////// template void erase(T &container, F predicate); +template +bool eraseOne(T &container, F predicate); ///////////////////////// // contains @@ -442,7 +444,15 @@ void erase(T &container, F predicate) container.erase(std::remove_if(std::begin(container), std::end(container), predicate), std::end(container)); } - +template +bool eraseOne(T &container, F predicate) +{ + const auto it = std::find_if(std::begin(container), std::end(container), predicate); + if (it == std::end(container)) + return false; + container.erase(it); + return true; +} ////////////////// // contains diff --git a/src/plugins/projectexplorer/toolchainoptionspage.cpp b/src/plugins/projectexplorer/toolchainoptionspage.cpp index 7f0c17a5c63..b8dccb01998 100644 --- a/src/plugins/projectexplorer/toolchainoptionspage.cpp +++ b/src/plugins/projectexplorer/toolchainoptionspage.cpp @@ -363,27 +363,24 @@ ToolChainTreeItem *ToolChainOptionsWidget::insertToolChain(ToolChain *tc, bool c void ToolChainOptionsWidget::addToolChain(ToolChain *tc) { - for (int i = 0; i < m_toAddList.size(); ++i) { - if (m_toAddList.at(i)->toolChain == tc) { - // do not delete i element: Still used elsewhere! - m_toAddList.removeAt(i); - return; - } + if (Utils::eraseOne(m_toAddList, [tc](const ToolChainTreeItem *item) { + return item->toolChain == tc; })) { + // do not delete here! + return; } insertToolChain(tc); - updateState(); } void ToolChainOptionsWidget::removeToolChain(ToolChain *tc) { - for (int i = 0; i < m_toRemoveList.size(); ++i) { - if (m_toRemoveList.at(i)->toolChain == tc) { - m_toRemoveList.removeAt(i); - delete m_toRemoveList.at(i); - return; - } + if (auto it = std::find_if(m_toRemoveList.begin(), m_toRemoveList.end(), + [tc](const ToolChainTreeItem *item) { return item->toolChain == tc; }); + it != m_toRemoveList.end()) { + m_toRemoveList.erase(it); + delete *it; + return; } StaticTreeItem *parent = parentForToolChain(tc);