diff options
| author | Tobias Hunger <tobias.hunger@qt.io> | 2018-01-09 15:32:47 +0100 |
|---|---|---|
| committer | Tobias Hunger <tobias.hunger@qt.io> | 2018-03-12 14:14:01 +0000 |
| commit | 831b84a6fd7fda254a9eeece8f27275c3cf9ff66 (patch) | |
| tree | ca5c2172f478c95cb51a6a8184f19deea03da464 | |
| parent | f873ad54e18ea08a68aefede22305c538e10db82 (diff) | |
| download | qt-creator-831b84a6fd7fda254a9eeece8f27275c3cf9ff66.tar.gz | |
Utils: Add take
Add a new algorithmn to take the first match in a container out of the container.
It takes a pointer to something and will try to match against a smart pointer
stored in the container. Besides that it also accepts the usual like a predicate,
member variable or member function.
Change-Id: I4aabd4d43aa076a534da6488d0f9c3695ba79c09
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
| -rw-r--r-- | src/libs/utils/algorithm.h | 33 | ||||
| -rw-r--r-- | tests/auto/algorithm/tst_algorithm.cpp | 26 |
2 files changed, 59 insertions, 0 deletions
diff --git a/src/libs/utils/algorithm.h b/src/libs/utils/algorithm.h index b94d90f821..e89b3aa8c6 100644 --- a/src/libs/utils/algorithm.h +++ b/src/libs/utils/algorithm.h @@ -26,6 +26,7 @@ #pragma once #include "predicates.h" +#include "optional.h" #include <qcompilerdetection.h> // for Q_REQUIRED_RESULT @@ -801,4 +802,36 @@ auto toConstReferences(const SourceContainer &sources) return transform(sources, [] (const auto &value) { return std::cref(value); }); } +////////////////// +// take: +///////////////// + +template<class C, typename P> +Q_REQUIRED_RESULT Utils::optional<typename C::value_type> take(C &container, P predicate) +{ + const auto end = std::end(container); + + const auto it = std::find_if(std::begin(container), end, predicate); + if (it == end) + return Utils::nullopt; + + Utils::optional<typename C::value_type> result = Utils::make_optional(std::move(*it)); + container.erase(it); + return result; } + +// pointer to member +template <typename C, typename R, typename S> +Q_REQUIRED_RESULT decltype(auto) take(C &container, R S::*member) +{ + return take(container, std::mem_fn(member)); +} + +// pointer to member function +template <typename C, typename R, typename S> +Q_REQUIRED_RESULT decltype(auto) take(C &container, R (S::*function)() const) +{ + return take(container, std::mem_fn(function)); +} + +} // namespace Utils diff --git a/tests/auto/algorithm/tst_algorithm.cpp b/tests/auto/algorithm/tst_algorithm.cpp index 87e3b7ecbd..88845d7e01 100644 --- a/tests/auto/algorithm/tst_algorithm.cpp +++ b/tests/auto/algorithm/tst_algorithm.cpp @@ -50,6 +50,7 @@ private slots: void findOrDefault(); void toRawPointer(); void toReferences(); + void take(); }; @@ -612,6 +613,31 @@ void tst_Algorithm::toReferences() } } +void tst_Algorithm::take() +{ + { + QList<Struct> v {1, 3, 5, 6, 7, 8, 9, 11, 13, 15, 13, 16, 17}; + Utils::optional<Struct> r1 = Utils::take(v, [](const Struct &s) { return s.member == 13; }); + QVERIFY(r1); + QCOMPARE(r1.value(), 13); + Utils::optional<Struct> r2 = Utils::take(v, [](const Struct &s) { return s.member == 13; }); + QVERIFY(r2); + QCOMPARE(r2.value(), 13); + Utils::optional<Struct> r3 = Utils::take(v, [](const Struct &s) { return s.member == 13; }); + QVERIFY(!r3); + + Utils::optional<Struct> r4 = Utils::take(v, &Struct::isEven); + QVERIFY(r4); + QCOMPARE(r4.value(), 6); + } + { + QList<Struct> v {0, 0, 0, 0, 0, 0, 1, 2, 3}; + Utils::optional<Struct> r1 = Utils::take(v, &Struct::member); + QVERIFY(r1); + QCOMPARE(r1.value(), 1); + } +} + QTEST_MAIN(tst_Algorithm) #include "tst_algorithm.moc" |
