From fa6f83d87be0e0d5a648fb139ae965a41930fb48 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Thu, 4 Jan 2018 21:16:20 +0100 Subject: [PATCH] Ref T218, sequence replaceOrAdd overloads --- src/blackmisc/datacache.cpp | 2 +- src/blackmisc/network/urllist.cpp | 2 +- src/blackmisc/sequence.h | 17 +++++++++++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/blackmisc/datacache.cpp b/src/blackmisc/datacache.cpp index 237747cae..74bdf3658 100644 --- a/src/blackmisc/datacache.cpp +++ b/src/blackmisc/datacache.cpp @@ -798,7 +798,7 @@ namespace BlackMisc CProcessInfo currentProcess = CProcessInfo::currentProcess(); Q_ASSERT(currentProcess.exists()); - apps.replaceOrAdd(currentProcess, currentProcess); + apps.replaceOrAdd(currentProcess); json.insert("apps", apps.toJson()); json.insert("uuid", uuid.toString()); if (!(file.seek(0) && file.resize(0) && file.write(QJsonDocument(json).toJson()) && file.checkedClose())) diff --git a/src/blackmisc/network/urllist.cpp b/src/blackmisc/network/urllist.cpp index 752b48619..cc53e9eb2 100644 --- a/src/blackmisc/network/urllist.cpp +++ b/src/blackmisc/network/urllist.cpp @@ -106,7 +106,7 @@ namespace BlackMisc CUrlList withoutDuplicates; for (const CUrl &url : (*this)) { - withoutDuplicates.replaceOrAdd(url, url); + withoutDuplicates.replaceOrAdd(url); } return withoutDuplicates; } diff --git a/src/blackmisc/sequence.h b/src/blackmisc/sequence.h index 50bf33bf0..a5b41ff22 100644 --- a/src/blackmisc/sequence.h +++ b/src/blackmisc/sequence.h @@ -24,7 +24,6 @@ namespace BlackMisc { - /*! * Generic sequential container with value semantics. * \tparam T the type of elements contained. @@ -351,6 +350,21 @@ namespace BlackMisc else { push_back(replacement); } } + //! Replace elements matching the given element. If there is no match, push the new element on the end. + void replaceOrAdd(const T &replacement) + { + this->replaceOrAdd(replacement, replacement); + } + + //! Replace or add given elements + void replaceOrAdd(const CSequence &replacements) + { + for (const T &replacement : replacements) + { + this->replaceOrAdd(replacement, replacement); + } + } + //! Replace elements matching a particular key/value pair. If there is no match, push the new element on the end. //! \param key1 A pointer to a member function of T. //! \param value1 Will be compared to the return value of key1. @@ -505,7 +519,6 @@ namespace BlackMisc private: QVector m_impl; }; - } //namespace BlackMisc Q_DECLARE_METATYPE(BlackMisc::CSequence)