Krita Source Code Documentation
Loading...
Searching...
No Matches
krita_container_utils.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#ifndef KRITA_CONTAINER_UTILS_H
8#define KRITA_CONTAINER_UTILS_H
9
10#include <functional>
11#include <QList>
12
13namespace KritaUtils
14{
15
16template <class T>
17 bool compareListsUnordered(const QList<T> &a, const QList<T> &b) {
18 if (a.size() != b.size()) return false;
19
20 Q_FOREACH(const T &t, a) {
21 if (!b.contains(t)) return false;
22 }
23
24 return true;
25}
26
27template <class C>
28 void makeContainerUnique(C &container) {
29 std::sort(container.begin(), container.end());
30 auto newEnd = std::unique(container.begin(), container.end());
31
32 while (newEnd != container.end()) {
33 newEnd = container.erase(newEnd);
34 }
35}
36
37
38template <class C, typename KeepIfFunction>
39 auto filterContainer(C &container, KeepIfFunction keepIf)
40 -> decltype(bool(keepIf(container[0])), void()) {
41
42 auto newEnd = std::remove_if(container.begin(), container.end(), [keepIf] (typename C::reference p) { return !keepIf(p); });
43 while (newEnd != container.end()) {
44 newEnd = container.erase(newEnd);
45 }
46}
47
48template<typename T>
50{
51 typedef typename std::remove_const<T>::type test_type;
52
53 template<typename A>
54 static constexpr bool test(
55 A *pointer,
56 A const *const_pointer = nullptr,
57 decltype(pointer->begin()) * = nullptr,
58 decltype(pointer->end()) * = nullptr,
59 decltype(const_pointer->begin()) * = nullptr,
60 decltype(const_pointer->end()) * = nullptr,
61 typename A::iterator * it = nullptr,
62 typename A::const_iterator * constIt = nullptr,
63 typename A::value_type * = nullptr) {
64
65 typedef typename A::iterator iterator;
66 typedef typename A::const_iterator const_iterator;
67 typedef typename A::value_type value_type;
68 return std::is_same<decltype(pointer->begin()),iterator>::value &&
69 std::is_same<decltype(pointer->end()),iterator>::value &&
70 std::is_same<decltype(const_pointer->begin()),const_iterator>::value &&
71 std::is_same<decltype(const_pointer->end()),const_iterator>::value &&
72 std::is_same<decltype(**it),value_type &>::value &&
73 std::is_same<decltype(**constIt),value_type const &>::value;
74
75 }
76
77 template<typename A>
78 static constexpr bool test(...) {
79 return false;
80 }
81
82 static const bool value = test<test_type>(nullptr);
83};
84
85template<typename T>
87{
88 typedef typename std::remove_const<T>::type test_type;
89
90 template<typename A, typename R = decltype(std::declval<A&>().push_back(std::declval<typename A::value_type>()))>
91 static constexpr bool test(A */*pointer*/) {
92 return is_container<A>::value && std::is_same<R, void>::value;
93 }
94
95 template<typename A>
96 static constexpr bool test(...) {
97 return false;
98 }
99
100 static const bool value = test<test_type>(nullptr);
101};
102
103}
104
105
106#endif // KRITA_CONTAINER_UTILS_H
107
const Params2D p
#define C(i, j)
typedef void(QOPENGLF_APIENTRYP PFNGLINVALIDATEBUFFERDATAPROC)(GLuint buffer)
auto filterContainer(C &container, KeepIfFunction keepIf) -> decltype(bool(keepIf(container[0])), void())
bool compareListsUnordered(const QList< T > &a, const QList< T > &b)
void makeContainerUnique(C &container)
std::remove_const< T >::type test_type
std::remove_const< T >::type test_type
static constexpr bool test(...)
static constexpr bool test(A *pointer, A const *const_pointer=nullptr, decltype(pointer->begin()) *=nullptr, decltype(pointer->end()) *=nullptr, decltype(const_pointer->begin()) *=nullptr, decltype(const_pointer->end()) *=nullptr, typename A::iterator *it=nullptr, typename A::const_iterator *constIt=nullptr, typename A::value_type *=nullptr)