Commit 9b832f67 authored by Tobias Hunger's avatar Tobias Hunger

Utils: Implement more Utils::anyOf variations

Change-Id: I0cba5b58dde6003f5c5cb399142f985cbe83f0a7
Reviewed-by: Eike Ziller's avatarEike Ziller <eike.ziller@qt.io>
parent 8cd2c234
......@@ -40,6 +40,11 @@ namespace Utils
//////////////////
// anyOf
/////////////////
template<typename T, typename F>
bool anyOf(const T &container, F predicate)
{
return std::any_of(std::begin(container), std::end(container), predicate);
}
// anyOf taking a member function pointer
template<typename T, typename R, typename S>
......@@ -48,12 +53,17 @@ bool anyOf(const T &container, R (S::*predicate)() const)
return std::any_of(std::begin(container), std::end(container), std::mem_fn(predicate));
}
template<typename T, typename F>
bool anyOf(const T &container, F predicate)
// anyOf taking a member pointer
template<typename T, typename R, typename S>
bool anyOf(const T &container, R S::*member)
{
return std::any_of(std::begin(container), std::end(container), predicate);
return std::any_of(std::begin(container), std::end(container), std::mem_fn(member));
}
//////////////////
// count
/////////////////
template<typename T, typename F>
int count(const T &container, F predicate)
{
......
......@@ -38,6 +38,7 @@ class tst_Algorithm : public QObject
Q_OBJECT
private slots:
void anyOf();
void transform();
void sort();
void contains();
......@@ -63,6 +64,29 @@ struct Struct
};
}
void tst_Algorithm::anyOf()
{
{
const QList<QString> strings({"1", "3", "132"});
QVERIFY(Utils::anyOf(strings, [](const QString &s) { return s == "132"; }));
QVERIFY(!Utils::anyOf(strings, [](const QString &s) { return s == "1324"; }));
}
{
const QList<Struct> list({2, 4, 6, 8});
QVERIFY(Utils::anyOf(list, &Struct::isEven));
QVERIFY(!Utils::anyOf(list, &Struct::isOdd));
}
{
const QList<Struct> list({0, 0, 0, 0, 1, 0, 0});
QVERIFY(Utils::anyOf(list, &Struct::member));
}
{
const QList<Struct> list({0, 0, 0, 0, 0, 0, 0});
QVERIFY(!Utils::anyOf(list, &Struct::member));
}
}
void tst_Algorithm::transform()
{
// same container type
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment