forked from qt-creator/qt-creator
Utils: Add pointeralgorithm.h
Change-Id: I3e81bdbf22808efbe1fb5fab13bef24c8f73f404 Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -45,6 +45,7 @@
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
|
||||
//////////////////
|
||||
// anyOf
|
||||
/////////////////
|
||||
@@ -107,61 +108,21 @@ bool contains(const T &container, F function)
|
||||
return anyOf(container, function);
|
||||
}
|
||||
|
||||
// Contains for normal pointers in std::vector<std::unique_ptr>
|
||||
template<template<typename, typename...> class C, typename T, typename... Args>
|
||||
bool contains(const C<T, Args...> &container, typename T::element_type *other)
|
||||
{
|
||||
return anyOf(container, [other](const typename C<T, Args...>::value_type &value) { return value.get() == other; });
|
||||
}
|
||||
|
||||
template<typename T, typename R, typename S>
|
||||
bool contains(const T &container, R (S::*function)() const)
|
||||
{
|
||||
return anyOf(container, function);
|
||||
}
|
||||
|
||||
template<template<typename, typename...> class C, typename T, typename R, typename S, typename... Args>
|
||||
bool contains(const C<T, Args...> &container, R (S::*function)() const)
|
||||
template<typename C, typename R, typename S>
|
||||
bool contains(const C &container, R S::*member)
|
||||
{
|
||||
return anyOf(container, function);
|
||||
return anyOf(container, std::mem_fn(member));
|
||||
}
|
||||
|
||||
//////////////////
|
||||
// findOr
|
||||
/////////////////
|
||||
|
||||
// Containers containing std::unique_ptr:
|
||||
template<template<typename, typename...> class C,
|
||||
typename T, typename D,
|
||||
typename F,
|
||||
typename... Args>
|
||||
Q_REQUIRED_RESULT
|
||||
T *findOr(const C<std::unique_ptr<T, D>, Args...> &container, T *other, F function)
|
||||
{
|
||||
auto end = std::end(container);
|
||||
auto it = std::find_if(std::begin(container), end, function);
|
||||
return (it == end) ? other : it->get();
|
||||
}
|
||||
|
||||
template<template<typename, typename...> class C,
|
||||
typename T, typename D,
|
||||
typename R, typename S,
|
||||
typename... Args>
|
||||
Q_REQUIRED_RESULT
|
||||
T *findOr(const C<std::unique_ptr<T, D>, Args...> &container, T *other, R (S::*function)() const)
|
||||
{
|
||||
return findOr(container, other, std::mem_fn(function));
|
||||
}
|
||||
|
||||
template<template<typename, typename...> class C, typename... Args,
|
||||
typename T, typename D,
|
||||
typename R, typename S>
|
||||
Q_REQUIRED_RESULT
|
||||
T *findOr(const C<std::unique_ptr<T, D>, Args...> &container, T *other, R S::*member)
|
||||
{
|
||||
return findOr(container, other, std::mem_fn(member));
|
||||
}
|
||||
|
||||
template<typename C, typename F>
|
||||
Q_REQUIRED_RESULT
|
||||
typename C::value_type findOr(const C &container, typename C::value_type other, F function)
|
||||
@@ -190,62 +151,31 @@ typename T::value_type findOr(const T &container, typename T::value_type other,
|
||||
//////////////////
|
||||
// findOrDefault
|
||||
//////////////////
|
||||
|
||||
// Containers containing std::unique_ptr:
|
||||
template<template<typename, typename...> class C,
|
||||
typename T, typename D,
|
||||
typename F,
|
||||
typename... Args>
|
||||
Q_REQUIRED_RESULT
|
||||
T *findOrDefault(const C<std::unique_ptr<T, D>, Args...> &container, F function)
|
||||
{
|
||||
return findOr(container, static_cast<T*>(nullptr), function);
|
||||
}
|
||||
|
||||
template<template<typename, typename...> class C,
|
||||
typename T, typename D,
|
||||
typename R, typename S,
|
||||
typename... Args>
|
||||
Q_REQUIRED_RESULT
|
||||
T *findOrDefault(const C<std::unique_ptr<T, D>, Args...> &container, R (S::*function)() const)
|
||||
{
|
||||
return findOr(container, static_cast<T*>(nullptr), std::mem_fn(function));
|
||||
}
|
||||
|
||||
template<template<typename, typename...> class C,
|
||||
typename T, typename D,
|
||||
typename R, typename S,
|
||||
typename... Args>
|
||||
Q_REQUIRED_RESULT
|
||||
T *findOrDefault(const C<std::unique_ptr<T, D>, Args...> &container, R S::*member)
|
||||
{
|
||||
return findOr(container, static_cast<T*>(nullptr), std::mem_fn(member));
|
||||
}
|
||||
|
||||
|
||||
// Default implementation:
|
||||
template<typename C, typename F>
|
||||
Q_REQUIRED_RESULT
|
||||
typename C::value_type findOrDefault(const C &container, F function)
|
||||
typename std::enable_if_t<std::is_copy_assignable<typename C::value_type>::value, typename C::value_type>
|
||||
findOrDefault(const C &container, F function)
|
||||
{
|
||||
return findOr(container, typename C::value_type(), function);
|
||||
}
|
||||
|
||||
template<typename C, typename R, typename S>
|
||||
Q_REQUIRED_RESULT
|
||||
typename C::value_type findOrDefault(const C &container, R (S::*function)() const)
|
||||
typename std::enable_if_t<std::is_copy_assignable<typename C::value_type>::value, typename C::value_type>
|
||||
findOrDefault(const C &container, R (S::*function)() const)
|
||||
{
|
||||
return findOr(container, typename C::value_type(), std::mem_fn(function));
|
||||
}
|
||||
|
||||
template<typename C, typename R, typename S>
|
||||
Q_REQUIRED_RESULT
|
||||
typename C::value_type findOrDefault(const C &container, R S::*member)
|
||||
typename std::enable_if_t<std::is_copy_assignable<typename C::value_type>::value, typename C::value_type>
|
||||
findOrDefault(const C &container, R S::*member)
|
||||
{
|
||||
return findOr(container, typename C::value_type(), std::mem_fn(member));
|
||||
}
|
||||
|
||||
|
||||
//////////////////
|
||||
// index of:
|
||||
//////////////////
|
||||
@@ -746,30 +676,6 @@ inline void reverseForeach(const Container &c, const Op &operation)
|
||||
operation(*it);
|
||||
}
|
||||
|
||||
//////////////////
|
||||
// toRawPointer
|
||||
/////////////////
|
||||
template <typename ResultContainer,
|
||||
typename SourceContainer>
|
||||
ResultContainer toRawPointer(const SourceContainer &sources)
|
||||
{
|
||||
return transform<ResultContainer>(sources, [] (const auto &pointer) { return pointer.get(); });
|
||||
}
|
||||
|
||||
template <template<typename...> class ResultContainer,
|
||||
template<typename...> class SourceContainer,
|
||||
typename... SCArgs>
|
||||
auto toRawPointer(const SourceContainer<SCArgs...> &sources)
|
||||
{
|
||||
return transform<ResultContainer, const SourceContainer<SCArgs...> &>(sources, [] (const auto &pointer) { return pointer.get(); });
|
||||
}
|
||||
|
||||
template <class SourceContainer>
|
||||
auto toRawPointer(const SourceContainer &sources)
|
||||
{
|
||||
return transform(sources, [] (const auto &pointer) { return pointer.get(); });
|
||||
}
|
||||
|
||||
//////////////////
|
||||
// toReferences
|
||||
/////////////////
|
||||
|
305
src/libs/utils/pointeralgorithm.h
Normal file
305
src/libs/utils/pointeralgorithm.h
Normal file
@@ -0,0 +1,305 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2018 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "algorithm.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
|
||||
template<typename C>
|
||||
using ValueType = typename C::value_type;
|
||||
|
||||
template<typename C>
|
||||
using PointerType = typename C::value_type::element_type*;
|
||||
|
||||
//////////////////
|
||||
// anyOf
|
||||
/////////////////
|
||||
template<typename C>
|
||||
bool anyOf(const C &container, PointerType<C> p)
|
||||
{
|
||||
return anyOf(container, [p](const ValueType<C> &v) { return v.get() == p; });
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
bool anyOf(const C &container, std::nullptr_t)
|
||||
{
|
||||
return anyOf(container, static_cast<PointerType<C>>(nullptr));
|
||||
}
|
||||
|
||||
//////////////////
|
||||
// count
|
||||
/////////////////
|
||||
template<typename C>
|
||||
int count(const C &container, PointerType<C> p)
|
||||
{
|
||||
return count(container, [p](const ValueType<C> &v) { return v.get() == p; });
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
int count(const C &container, std::nullptr_t)
|
||||
{
|
||||
return count(container, static_cast<PointerType<C>>(nullptr));
|
||||
}
|
||||
|
||||
//////////////////
|
||||
// allOf
|
||||
/////////////////
|
||||
template<typename C>
|
||||
bool allOf(const C &container, PointerType<C> p)
|
||||
{
|
||||
return allOf(container, [p](const ValueType<C> &v) { return v.get() == p; });
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
int allOf(const C &container, std::nullptr_t)
|
||||
{
|
||||
return allOf(container, static_cast<PointerType<C>>(nullptr));
|
||||
}
|
||||
|
||||
//////////////////
|
||||
// erase
|
||||
/////////////////
|
||||
template<typename C>
|
||||
void erase(C &container, PointerType<C> p)
|
||||
{
|
||||
return erase(container, [p](const ValueType<C> &v) { return v.get() == p; });
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
int erase(const C &container, std::nullptr_t)
|
||||
{
|
||||
return erase(container, static_cast<PointerType<C>>(nullptr));
|
||||
}
|
||||
|
||||
//////////////////
|
||||
// contains
|
||||
/////////////////
|
||||
template<typename C>
|
||||
bool contains(const C &container, PointerType<C> p)
|
||||
{
|
||||
return anyOf(container, p);
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
bool contains(const C &container, std::nullptr_t)
|
||||
{
|
||||
return anyOf(container, nullptr);
|
||||
}
|
||||
|
||||
//////////////////
|
||||
// findOr
|
||||
/////////////////
|
||||
|
||||
template<typename C, typename F>
|
||||
Q_REQUIRED_RESULT
|
||||
PointerType<C> findOr(const C &container, PointerType<C> other, F function)
|
||||
{
|
||||
typename C::const_iterator begin = std::begin(container);
|
||||
typename C::const_iterator end = std::end(container);
|
||||
|
||||
typename C::const_iterator it = std::find_if(begin, end, function);
|
||||
return it == end ? other : it->get();
|
||||
}
|
||||
|
||||
template<typename C, typename R, typename S>
|
||||
Q_REQUIRED_RESULT
|
||||
PointerType<C> findOr(const C &container, PointerType<C> other, R (S::*function)() const)
|
||||
{
|
||||
return findOr(container, other, std::mem_fn(function));
|
||||
}
|
||||
|
||||
template<typename C, typename R, typename S>
|
||||
Q_REQUIRED_RESULT
|
||||
PointerType<C> findOr(const C &container, PointerType<C> other, R S::*member)
|
||||
{
|
||||
return findOr(container, other, std::mem_fn(member));
|
||||
}
|
||||
|
||||
|
||||
template<typename C, typename F>
|
||||
Q_REQUIRED_RESULT
|
||||
PointerType<C> findOr(const C &container, std::nullptr_t, F function)
|
||||
{
|
||||
return findOr(container, static_cast<PointerType<C>>(nullptr), function);
|
||||
}
|
||||
|
||||
template<typename C, typename R, typename S>
|
||||
Q_REQUIRED_RESULT
|
||||
PointerType<C> findOr(const C &container, std::nullptr_t, R (S::*function)() const)
|
||||
{
|
||||
return findOr(container, static_cast<PointerType<C>>(nullptr), function);
|
||||
}
|
||||
|
||||
template<typename C, typename R, typename S>
|
||||
Q_REQUIRED_RESULT
|
||||
PointerType<C> findOr(const C &container, std::nullptr_t, R S::*member)
|
||||
{
|
||||
return findOr(container, static_cast<PointerType<C>>(nullptr), member);
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
Q_REQUIRED_RESULT
|
||||
PointerType<C> findOr(const C &container, PointerType<C> other, PointerType<C> p)
|
||||
{
|
||||
return findOr(container, other, [p](const ValueType<C> &v) { return v.get() == p; });
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
Q_REQUIRED_RESULT
|
||||
PointerType<C> findOr(const C &container, PointerType<C> other, std::nullptr_t)
|
||||
{
|
||||
return findOr(container, other, static_cast<PointerType<C>>(nullptr));
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
Q_REQUIRED_RESULT
|
||||
PointerType<C> findOr(const C &container, std::nullptr_t, PointerType<C> p)
|
||||
{
|
||||
return findOr(container, static_cast<PointerType<C>>(nullptr), p);
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
Q_REQUIRED_RESULT
|
||||
PointerType<C> findOr(const C &container, std::nullptr_t, std::nullptr_t)
|
||||
{
|
||||
return findOr(container, static_cast<PointerType<C>>(nullptr), static_cast<PointerType<C>>(nullptr));
|
||||
}
|
||||
|
||||
//////////////////
|
||||
// findOrDefault
|
||||
/////////////////
|
||||
template<typename C,typename F>
|
||||
Q_REQUIRED_RESULT
|
||||
PointerType<C> findOrDefault(const C &container, F function)
|
||||
{
|
||||
return findOr(container, static_cast<PointerType<C>>(nullptr), function);
|
||||
}
|
||||
|
||||
template<typename C, typename R, typename S>
|
||||
Q_REQUIRED_RESULT
|
||||
PointerType<C> findOrDefault(const C &container, R (S::*function)() const)
|
||||
{
|
||||
return findOr(container, static_cast<PointerType<C>>(nullptr), std::mem_fn(function));
|
||||
}
|
||||
|
||||
template<typename C, typename R, typename S>
|
||||
Q_REQUIRED_RESULT
|
||||
PointerType<C> findOrDefault(const C &container, R S::*member)
|
||||
{
|
||||
return findOr(container, static_cast<PointerType<C>>(nullptr), std::mem_fn(member));
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
Q_REQUIRED_RESULT
|
||||
PointerType<C> findOrDefault(const C &container, PointerType<C> p)
|
||||
{
|
||||
return findOr(container, static_cast<PointerType<C>>(nullptr), p);
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
Q_REQUIRED_RESULT
|
||||
PointerType<C> findOrDefault(const C &container, std::nullptr_t)
|
||||
{
|
||||
return findOr(container, static_cast<PointerType<C>>(nullptr), static_cast<PointerType<C>>(nullptr));
|
||||
}
|
||||
|
||||
//////////////////
|
||||
// index of:
|
||||
//////////////////
|
||||
template<typename C>
|
||||
Q_REQUIRED_RESULT
|
||||
int indexOf(const C& container, PointerType<C> p)
|
||||
{
|
||||
return indexOf(container, [p](const ValueType<C> &v) { return v.get() == p; });
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
Q_REQUIRED_RESULT
|
||||
int indexOf(const C& container, std::nullptr_t)
|
||||
{
|
||||
return indexOf(container, static_cast<PointerType<C>>(nullptr));
|
||||
}
|
||||
|
||||
//////////////////
|
||||
// toRawPointer
|
||||
/////////////////
|
||||
template <typename ResultContainer,
|
||||
typename SourceContainer>
|
||||
ResultContainer toRawPointer(const SourceContainer &sources)
|
||||
{
|
||||
return transform<ResultContainer>(sources, [] (const auto &pointer) { return pointer.get(); });
|
||||
}
|
||||
|
||||
template <template<typename...> class ResultContainer,
|
||||
template<typename...> class SourceContainer,
|
||||
typename... SCArgs>
|
||||
auto toRawPointer(const SourceContainer<SCArgs...> &sources)
|
||||
{
|
||||
return transform<ResultContainer, const SourceContainer<SCArgs...> &>(sources, [] (const auto &pointer) { return pointer.get(); });
|
||||
}
|
||||
|
||||
template <class SourceContainer>
|
||||
auto toRawPointer(const SourceContainer &sources)
|
||||
{
|
||||
return transform(sources, [] (const auto &pointer) { return pointer.get(); });
|
||||
}
|
||||
|
||||
//////////////////
|
||||
// take:
|
||||
/////////////////
|
||||
template<typename C>
|
||||
Q_REQUIRED_RESULT Utils::optional<ValueType<C>> take(C &container, ValueType<C> *p)
|
||||
{
|
||||
return take(container, [p](const ValueType<C> &v) { return v.get() == p; });
|
||||
}
|
||||
|
||||
template <typename C>
|
||||
Q_REQUIRED_RESULT Utils::optional<ValueType<C>> take(C &container, std::nullptr_t)
|
||||
{
|
||||
return take(container, static_cast<ValueType<C> *>(nullptr));
|
||||
}
|
||||
|
||||
//////////////////
|
||||
// takeOrDefault:
|
||||
/////////////////
|
||||
template<typename C>
|
||||
Q_REQUIRED_RESULT ValueType<C> takeOrDefault(C &container, ValueType<C> *p)
|
||||
{
|
||||
auto result = take(container, [p](const ValueType<C> &v) { return v.get() == p; });
|
||||
return bool(result) ? result.value() : Utils::make_optional<ValueType<C>>(nullptr);
|
||||
}
|
||||
|
||||
template <typename C>
|
||||
Q_REQUIRED_RESULT ValueType<C> takeOrDefault(C &container, std::nullptr_t)
|
||||
{
|
||||
return takeOrDefault(container, static_cast<ValueType<C> *>(nullptr));
|
||||
}
|
||||
|
||||
} // namespace Utils
|
@@ -131,6 +131,7 @@ HEADERS += \
|
||||
$$PWD/environment.h \
|
||||
$$PWD/environmentmodel.h \
|
||||
$$PWD/environmentdialog.h \
|
||||
$$PWD/pointeralgorithm.h \
|
||||
$$PWD/qtcprocess.h \
|
||||
$$PWD/utils_global.h \
|
||||
$$PWD/reloadpromptutils.h \
|
||||
|
@@ -167,6 +167,7 @@ Project {
|
||||
"persistentsettings.cpp",
|
||||
"persistentsettings.h",
|
||||
"predicates.h",
|
||||
"pointeralgorithm.h",
|
||||
"port.cpp",
|
||||
"port.h",
|
||||
"portlist.cpp",
|
||||
|
@@ -48,7 +48,6 @@ private slots:
|
||||
void contains();
|
||||
void findOr();
|
||||
void findOrDefault();
|
||||
void toRawPointer();
|
||||
void toReferences();
|
||||
void take();
|
||||
};
|
||||
@@ -476,93 +475,31 @@ void tst_Algorithm::contains()
|
||||
QList<Struct> structQlist = {2, 4, 6, 8};
|
||||
QVERIFY(Utils::contains(structQlist, &Struct::isEven));
|
||||
QVERIFY(!Utils::contains(structQlist, &Struct::isOdd));
|
||||
std::vector<std::unique_ptr<int>> v2;
|
||||
v2.emplace_back(std::make_unique<int>(1));
|
||||
v2.emplace_back(std::make_unique<int>(2));
|
||||
v2.emplace_back(std::make_unique<int>(3));
|
||||
v2.emplace_back(std::make_unique<int>(4));
|
||||
QVERIFY(Utils::contains(v2, [](const std::unique_ptr<int> &ip) { return *ip == 2; }));
|
||||
QVERIFY(!Utils::contains(v2, [](const std::unique_ptr<int> &ip) { return *ip == 5; }));
|
||||
// Find pointers in unique_ptrs:
|
||||
QVERIFY(Utils::contains(v2, v2.back().get()));
|
||||
int foo = 42;
|
||||
QVERIFY(!Utils::contains(v2, &foo));
|
||||
}
|
||||
|
||||
void tst_Algorithm::findOr()
|
||||
{
|
||||
std::vector<int> v1{1, 2, 3, 4};
|
||||
QCOMPARE(Utils::findOr(v1, 6, [](int i) { return i == 2; }), 2);
|
||||
QCOMPARE(Utils::findOr(v1, 6, [](int i) { return i == 5; }), 6);
|
||||
std::vector<std::unique_ptr<int>> v2;
|
||||
v2.emplace_back(std::make_unique<int>(1));
|
||||
v2.emplace_back(std::make_unique<int>(2));
|
||||
v2.emplace_back(std::make_unique<int>(3));
|
||||
v2.emplace_back(std::make_unique<int>(4));
|
||||
int def = 6;
|
||||
QCOMPARE(Utils::findOr(v2, &def, [](const std::unique_ptr<int> &ip) { return *ip == 2; }), v2.at(1).get());
|
||||
QCOMPARE(Utils::findOr(v2, &def, [](const std::unique_ptr<int> &ip) { return *ip == 5; }), &def);
|
||||
std::vector<std::unique_ptr<Struct>> v3;
|
||||
v3.emplace_back(std::make_unique<Struct>(1));
|
||||
v3.emplace_back(std::make_unique<Struct>(3));
|
||||
v3.emplace_back(std::make_unique<Struct>(5));
|
||||
v3.emplace_back(std::make_unique<Struct>(7));
|
||||
Struct defS(6);
|
||||
QCOMPARE(Utils::findOr(v3, &defS, &Struct::isOdd), v3.at(0).get());
|
||||
QCOMPARE(Utils::findOr(v3, &defS, &Struct::isEven), &defS);
|
||||
|
||||
std::vector<std::shared_ptr<Struct>> v4;
|
||||
v4.emplace_back(std::make_shared<Struct>(1));
|
||||
v4.emplace_back(std::make_shared<Struct>(3));
|
||||
v4.emplace_back(std::make_shared<Struct>(5));
|
||||
v4.emplace_back(std::make_shared<Struct>(7));
|
||||
std::shared_ptr<Struct> sharedDefS = std::make_shared<Struct>(6);
|
||||
QCOMPARE(Utils::findOr(v4, sharedDefS, &Struct::isOdd), v4.at(0));
|
||||
QCOMPARE(Utils::findOr(v4, sharedDefS, &Struct::isEven), sharedDefS);
|
||||
QCOMPARE(Utils::findOr(v1, 10, [](int i) { return i == 2; }), 2);
|
||||
QCOMPARE(Utils::findOr(v1, 10, [](int i) { return i == 5; }), 10);
|
||||
}
|
||||
|
||||
void tst_Algorithm::findOrDefault()
|
||||
{
|
||||
std::vector<int> v1{1, 2, 3, 4};
|
||||
QCOMPARE(Utils::findOrDefault(v1, [](int i) { return i == 2; }), 2);
|
||||
QCOMPARE(Utils::findOrDefault(v1, [](int i) { return i == 5; }), 0);
|
||||
std::vector<std::unique_ptr<int>> v2;
|
||||
v2.emplace_back(std::make_unique<int>(1));
|
||||
v2.emplace_back(std::make_unique<int>(2));
|
||||
v2.emplace_back(std::make_unique<int>(3));
|
||||
v2.emplace_back(std::make_unique<int>(4));
|
||||
QCOMPARE(Utils::findOrDefault(v2, [](const std::unique_ptr<int> &ip) { return *ip == 2; }), v2.at(1).get());
|
||||
QCOMPARE(Utils::findOrDefault(v2, [](const std::unique_ptr<int> &ip) { return *ip == 5; }), static_cast<int*>(0));
|
||||
std::vector<std::unique_ptr<Struct>> v3;
|
||||
v3.emplace_back(std::make_unique<Struct>(1));
|
||||
v3.emplace_back(std::make_unique<Struct>(3));
|
||||
v3.emplace_back(std::make_unique<Struct>(5));
|
||||
v3.emplace_back(std::make_unique<Struct>(7));
|
||||
QCOMPARE(Utils::findOrDefault(v3, &Struct::isOdd), v3.at(0).get());
|
||||
QCOMPARE(Utils::findOrDefault(v3, &Struct::isEven), static_cast<Struct*>(nullptr));
|
||||
|
||||
std::vector<std::shared_ptr<Struct>> v4;
|
||||
v4.emplace_back(std::make_shared<Struct>(1));
|
||||
v4.emplace_back(std::make_shared<Struct>(3));
|
||||
v4.emplace_back(std::make_shared<Struct>(5));
|
||||
v4.emplace_back(std::make_shared<Struct>(7));
|
||||
QCOMPARE(Utils::findOrDefault(v4, &Struct::isOdd), v4.at(0));
|
||||
QCOMPARE(Utils::findOrDefault(v4, &Struct::isEven), std::shared_ptr<Struct>());
|
||||
}
|
||||
|
||||
void tst_Algorithm::toRawPointer()
|
||||
{
|
||||
const std::vector<std::unique_ptr<Struct>> v;
|
||||
|
||||
// same result container
|
||||
const std::vector<Struct *> x1 = Utils::toRawPointer(v);
|
||||
// different result container
|
||||
const std::vector<Struct *> x2 = Utils::toRawPointer<std::vector>(v);
|
||||
const QVector<Struct *> x3 = Utils::toRawPointer<QVector>(v);
|
||||
const std::list<Struct *> x4 = Utils::toRawPointer<std::list>(v);
|
||||
// different fully specified result container
|
||||
const std::vector<BaseStruct *> x5 = Utils::toRawPointer<std::vector<BaseStruct *>>(v);
|
||||
const QVector<BaseStruct *> x6 = Utils::toRawPointer<QVector<BaseStruct *>>(v);
|
||||
{
|
||||
std::vector<int> v1{1, 2, 3, 4};
|
||||
QCOMPARE(Utils::findOrDefault(v1, [](int i) { return i == 2; }), 2);
|
||||
QCOMPARE(Utils::findOrDefault(v1, [](int i) { return i == 5; }), 0);
|
||||
}
|
||||
{
|
||||
std::vector<std::shared_ptr<Struct>> v4;
|
||||
v4.emplace_back(std::make_shared<Struct>(1));
|
||||
v4.emplace_back(std::make_shared<Struct>(3));
|
||||
v4.emplace_back(std::make_shared<Struct>(5));
|
||||
v4.emplace_back(std::make_shared<Struct>(7));
|
||||
QCOMPARE(Utils::findOrDefault(v4, &Struct::isOdd), v4.at(0));
|
||||
QCOMPARE(Utils::findOrDefault(v4, &Struct::isEven), std::shared_ptr<Struct>());
|
||||
}
|
||||
}
|
||||
|
||||
void tst_Algorithm::toReferences()
|
||||
|
@@ -12,6 +12,7 @@ SUBDIRS += \
|
||||
externaltool \
|
||||
environment \
|
||||
generichighlighter \
|
||||
pointeralgorithm \
|
||||
profilewriter \
|
||||
treeviewfind \
|
||||
toolchaincache \
|
||||
|
4
tests/auto/pointeralgorithm/pointeralgorithm.pro
Normal file
4
tests/auto/pointeralgorithm/pointeralgorithm.pro
Normal file
@@ -0,0 +1,4 @@
|
||||
include(../qttest.pri)
|
||||
|
||||
SOURCES += tst_pointeralgorithm.cpp
|
||||
OTHER_FILES += $$IDE_SOURCE_TREE/src/libs/utils/pointeralgorithm.h
|
10
tests/auto/pointeralgorithm/pointeralgorithm.qbs
Normal file
10
tests/auto/pointeralgorithm/pointeralgorithm.qbs
Normal file
@@ -0,0 +1,10 @@
|
||||
import qbs
|
||||
|
||||
QtcAutotest {
|
||||
name: "PointerAlgorithm autotest"
|
||||
|
||||
cpp.includePaths: [project.ide_source_tree + "/src/libs"]
|
||||
files: [
|
||||
"tst_pointeralgorithm.cpp",
|
||||
]
|
||||
}
|
310
tests/auto/pointeralgorithm/tst_pointeralgorithm.cpp
Normal file
310
tests/auto/pointeralgorithm/tst_pointeralgorithm.cpp
Normal file
@@ -0,0 +1,310 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2018 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QtTest>
|
||||
|
||||
#include <array>
|
||||
#include <deque>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <valarray>
|
||||
|
||||
// must get included after the containers above or gcc4.9 will have a problem using
|
||||
// initializer_list related code on the templates inside algorithm.h
|
||||
#include <utils/pointeralgorithm.h>
|
||||
|
||||
class tst_PointerAlgorithm : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void anyOf();
|
||||
void count();
|
||||
void contains();
|
||||
void findOr();
|
||||
void findOrDefault();
|
||||
void toRawPointer();
|
||||
void toReferences();
|
||||
void take();
|
||||
};
|
||||
|
||||
|
||||
int stringToInt(const QString &s)
|
||||
{
|
||||
return s.toInt();
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
struct BaseStruct
|
||||
{
|
||||
BaseStruct(int m) : member(m) {}
|
||||
bool operator==(const BaseStruct &other) const { return member == other.member; }
|
||||
|
||||
int member;
|
||||
};
|
||||
|
||||
struct Struct : public BaseStruct
|
||||
{
|
||||
Struct(int m) : BaseStruct(m) {}
|
||||
bool isOdd() const { return member % 2 == 1; }
|
||||
bool isEven() const { return !isOdd(); }
|
||||
|
||||
int getMember() const { return member; }
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
void tst_PointerAlgorithm::anyOf()
|
||||
{
|
||||
{
|
||||
std::vector<std::unique_ptr<int>> vector;
|
||||
vector.emplace_back(std::make_unique<int>(5));
|
||||
vector.emplace_back(std::make_unique<int>(6));
|
||||
vector.emplace_back(std::make_unique<int>(7));
|
||||
vector.emplace_back(std::unique_ptr<int>());
|
||||
std::vector<int *> ptrVector = Utils::toRawPointer(vector);
|
||||
|
||||
QVERIFY(Utils::anyOf(vector, ptrVector.at(0)));
|
||||
int foo = 42;
|
||||
QVERIFY(!Utils::anyOf(vector, &foo));
|
||||
QVERIFY(Utils::anyOf(vector, nullptr));
|
||||
}
|
||||
{
|
||||
std::vector<std::unique_ptr<int>> vector;
|
||||
vector.emplace_back(std::make_unique<int>(5));
|
||||
vector.emplace_back(std::make_unique<int>(6));
|
||||
vector.emplace_back(std::make_unique<int>(7));
|
||||
std::vector<int *> ptrVector = Utils::toRawPointer(vector);
|
||||
|
||||
QVERIFY(!Utils::anyOf(vector, nullptr));
|
||||
}
|
||||
}
|
||||
|
||||
void tst_PointerAlgorithm::count()
|
||||
{
|
||||
std::vector<std::unique_ptr<int>> vector;
|
||||
vector.emplace_back(std::make_unique<int>(5));
|
||||
vector.emplace_back(std::unique_ptr<int>());
|
||||
vector.emplace_back(std::make_unique<int>(6));
|
||||
vector.emplace_back(std::make_unique<int>(7));
|
||||
vector.emplace_back(std::unique_ptr<int>());
|
||||
std::vector<int *> ptrVector = Utils::toRawPointer(vector);
|
||||
|
||||
QCOMPARE(Utils::count(vector, ptrVector.at(0)), 1);
|
||||
int foo = 42;
|
||||
QCOMPARE(Utils::count(vector, &foo), 0);
|
||||
QCOMPARE(Utils::count(vector, nullptr), 2);
|
||||
}
|
||||
|
||||
void tst_PointerAlgorithm::contains()
|
||||
{
|
||||
std::vector<std::unique_ptr<int>> vector;
|
||||
vector.emplace_back(std::make_unique<int>(5));
|
||||
vector.emplace_back(std::make_unique<int>(6));
|
||||
vector.emplace_back(std::make_unique<int>(7));
|
||||
vector.emplace_back(std::unique_ptr<int>());
|
||||
std::vector<int *> ptrVector = Utils::toRawPointer(vector);
|
||||
|
||||
QVERIFY(Utils::contains(vector, ptrVector.at(0)));
|
||||
int foo = 42;
|
||||
QVERIFY(!Utils::contains(vector, &foo));
|
||||
QVERIFY(Utils::contains(vector, nullptr));
|
||||
}
|
||||
|
||||
void tst_PointerAlgorithm::findOr()
|
||||
{
|
||||
{
|
||||
std::vector<std::unique_ptr<int>> vector;
|
||||
vector.emplace_back(std::make_unique<int>(5));
|
||||
vector.emplace_back(std::make_unique<int>(2));
|
||||
vector.emplace_back(std::make_unique<int>(6));
|
||||
vector.emplace_back(std::make_unique<int>(7));
|
||||
vector.emplace_back(std::unique_ptr<int>());
|
||||
std::vector<int *> ptrVector = Utils::toRawPointer(vector);
|
||||
|
||||
int foo = 42;
|
||||
int bar = 23;
|
||||
QVERIFY(Utils::findOr(vector, &foo, ptrVector.at(0)) == ptrVector.at(0));
|
||||
QVERIFY(Utils::findOr(vector, nullptr, &foo) == nullptr);
|
||||
QVERIFY(Utils::findOr(vector, &foo, nullptr) == nullptr);
|
||||
QVERIFY(Utils::findOr(vector, &foo, &bar) == &foo);
|
||||
|
||||
QCOMPARE(Utils::findOr(vector, &foo,
|
||||
[](const std::unique_ptr<int> &ip) { return ip && *ip == 2; }),
|
||||
ptrVector.at(1));
|
||||
QCOMPARE(Utils::findOr(vector, &foo,
|
||||
[](const std::unique_ptr<int> &ip) { return ip && *ip == 43; }),
|
||||
&foo);
|
||||
}
|
||||
{
|
||||
std::vector<std::unique_ptr<Struct>> v3;
|
||||
v3.emplace_back(std::make_unique<Struct>(1));
|
||||
v3.emplace_back(std::make_unique<Struct>(3));
|
||||
v3.emplace_back(std::make_unique<Struct>(5));
|
||||
v3.emplace_back(std::make_unique<Struct>(7));
|
||||
Struct defS(6);
|
||||
QCOMPARE(Utils::findOr(v3, &defS, &Struct::isOdd), v3.at(0).get());
|
||||
QCOMPARE(Utils::findOr(v3, &defS, &Struct::isEven), &defS);
|
||||
}
|
||||
{
|
||||
std::vector<std::shared_ptr<Struct>> v4;
|
||||
v4.emplace_back(std::make_shared<Struct>(1));
|
||||
v4.emplace_back(std::make_shared<Struct>(3));
|
||||
v4.emplace_back(std::make_shared<Struct>(5));
|
||||
v4.emplace_back(std::make_shared<Struct>(7));
|
||||
std::shared_ptr<Struct> sharedDefS = std::make_shared<Struct>(6);
|
||||
QCOMPARE(Utils::findOr(v4, sharedDefS, &Struct::isOdd), v4.at(0));
|
||||
QCOMPARE(Utils::findOr(v4, sharedDefS, &Struct::isEven), sharedDefS);
|
||||
}
|
||||
}
|
||||
|
||||
void tst_PointerAlgorithm::findOrDefault()
|
||||
{
|
||||
{
|
||||
std::vector<std::unique_ptr<int>> vector;
|
||||
vector.emplace_back(std::make_unique<int>(5));
|
||||
vector.emplace_back(std::make_unique<int>(6));
|
||||
vector.emplace_back(std::make_unique<int>(7));
|
||||
vector.emplace_back(std::unique_ptr<int>());
|
||||
std::vector<int *> ptrVector = Utils::toRawPointer(vector);
|
||||
|
||||
int foo = 42;
|
||||
QVERIFY(Utils::findOrDefault(vector, ptrVector.at(0)) == ptrVector.at(0));
|
||||
QVERIFY(Utils::findOrDefault(vector, &foo) == nullptr);
|
||||
}
|
||||
{
|
||||
std::vector<std::unique_ptr<int>> v2;
|
||||
v2.emplace_back(std::make_unique<int>(1));
|
||||
v2.emplace_back(std::make_unique<int>(2));
|
||||
v2.emplace_back(std::make_unique<int>(3));
|
||||
v2.emplace_back(std::make_unique<int>(4));
|
||||
QCOMPARE(Utils::findOrDefault(v2, [](const std::unique_ptr<int> &ip) { return *ip == 2; }), v2.at(1).get());
|
||||
QCOMPARE(Utils::findOrDefault(v2, [](const std::unique_ptr<int> &ip) { return *ip == 5; }), static_cast<int*>(nullptr));
|
||||
}
|
||||
{
|
||||
std::vector<std::unique_ptr<Struct>> v3;
|
||||
v3.emplace_back(std::make_unique<Struct>(1));
|
||||
v3.emplace_back(std::make_unique<Struct>(3));
|
||||
v3.emplace_back(std::make_unique<Struct>(5));
|
||||
v3.emplace_back(std::make_unique<Struct>(7));
|
||||
QCOMPARE(Utils::findOrDefault(v3, &Struct::isOdd), v3.at(0).get());
|
||||
QCOMPARE(Utils::findOrDefault(v3, &Struct::isEven), static_cast<Struct*>(nullptr));
|
||||
}
|
||||
}
|
||||
|
||||
void tst_PointerAlgorithm::toRawPointer()
|
||||
{
|
||||
const std::vector<std::unique_ptr<Struct>> v;
|
||||
|
||||
// same result container
|
||||
const std::vector<Struct *> x1 = Utils::toRawPointer(v);
|
||||
// different result container
|
||||
const std::vector<Struct *> x2 = Utils::toRawPointer<std::vector>(v);
|
||||
const QVector<Struct *> x3 = Utils::toRawPointer<QVector>(v);
|
||||
const std::list<Struct *> x4 = Utils::toRawPointer<std::list>(v);
|
||||
// different fully specified result container
|
||||
const std::vector<BaseStruct *> x5 = Utils::toRawPointer<std::vector<BaseStruct *>>(v);
|
||||
const QVector<BaseStruct *> x6 = Utils::toRawPointer<QVector<BaseStruct *>>(v);
|
||||
}
|
||||
|
||||
void tst_PointerAlgorithm::toReferences()
|
||||
{
|
||||
// toReference
|
||||
{
|
||||
// std::vector -> std::vector
|
||||
std::vector<Struct> v;
|
||||
const std::vector<std::reference_wrapper<Struct>> x = Utils::toReferences(v);
|
||||
}
|
||||
{
|
||||
// QList -> std::vector
|
||||
QList<Struct> v;
|
||||
const std::vector<std::reference_wrapper<Struct>> x = Utils::toReferences<std::vector>(v);
|
||||
}
|
||||
{
|
||||
// std::vector -> QList
|
||||
std::vector<Struct> v;
|
||||
const QList<std::reference_wrapper<Struct>> x = Utils::toReferences<QList>(v);
|
||||
}
|
||||
{
|
||||
// std::vector -> std::list
|
||||
std::vector<Struct> v;
|
||||
const std::list<std::reference_wrapper<Struct>> x = Utils::toReferences<std::list>(v);
|
||||
}
|
||||
// toConstReference
|
||||
{
|
||||
// std::vector -> std::vector
|
||||
const std::vector<Struct> v;
|
||||
const std::vector<std::reference_wrapper<const Struct>> x = Utils::toConstReferences(v);
|
||||
}
|
||||
{
|
||||
// QList -> std::vector
|
||||
const QList<Struct> v;
|
||||
const std::vector<std::reference_wrapper<const Struct>> x
|
||||
= Utils::toConstReferences<std::vector>(v);
|
||||
}
|
||||
{
|
||||
// std::vector -> QList
|
||||
const std::vector<Struct> v;
|
||||
const QList<std::reference_wrapper<const Struct>> x = Utils::toConstReferences<QList>(v);
|
||||
}
|
||||
{
|
||||
// std::vector -> std::list
|
||||
const std::vector<Struct> v;
|
||||
const std::list<std::reference_wrapper<const Struct>> x
|
||||
= Utils::toConstReferences<std::list>(v);
|
||||
}
|
||||
}
|
||||
|
||||
void tst_PointerAlgorithm::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_PointerAlgorithm)
|
||||
|
||||
#include "tst_pointeralgorithm.moc"
|
Reference in New Issue
Block a user