1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50
51 //! [0]
52 bool function(const T &t);
53 //! [0]
54
55
56 //! [1]
allLowerCase(const QString & string)57 bool allLowerCase(const QString &string)
58 {
59 return string.lowered() == string;
60 }
61
62 QStringList strings = ...;
63 QFuture<QString> lowerCaseStrings = QtConcurrent::filtered(strings, allLowerCase);
64 //! [1]
65
66
67 //! [2]
68 QStringList strings = ...;
69 QFuture<void> future = QtConcurrent::filter(strings, allLowerCase);
70 //! [2]
71
72
73 //! [3]
function(T & result,const U & intermediate)74 V function(T &result, const U &intermediate)
75 //! [3]
76
77
78 //! [4]
79 void addToDictionary(QSet<QString> &dictionary, const QString &string)
80 {
81 dictionary.insert(string);
82 }
83
84 QStringList strings = ...;
85 QFuture<QSet<QString> > dictionary = QtConcurrent::filteredReduced(strings, allLowerCase, addToDictionary);
86 //! [4]
87
88
89 //! [5]
90 QStringList strings = ...;
91 QFuture<QString> lowerCaseStrings = QtConcurrent::filtered(strings.constBegin(), strings.constEnd(), allLowerCase);
92
93 // filter in-place only works on non-const iterators
94 QFuture<void> future = QtConcurrent::filter(strings.begin(), strings.end(), allLowerCase);
95
96 QFuture<QSet<QString> > dictionary = QtConcurrent::filteredReduced(strings.constBegin(), strings.constEnd(), allLowerCase, addToDictionary);
97 //! [5]
98
99
100 //! [6]
101 QStringList strings = ...;
102
103 // each call blocks until the entire operation is finished
104 QStringList lowerCaseStrings = QtConcurrent::blockingFiltered(strings, allLowerCase);
105
106
107 QtConcurrent::blockingFilter(strings, allLowerCase);
108
109 QSet<QString> dictionary = QtConcurrent::blockingFilteredReduced(strings, allLowerCase, addToDictionary);
110 //! [6]
111
112
113 //! [7]
114 // keep only images with an alpha channel
115 QList<QImage> images = ...;
116 QFuture<void> alphaImages = QtConcurrent::filter(images, &QImage::hasAlphaChannel);
117
118 // retrieve gray scale images
119 QList<QImage> images = ...;
120 QFuture<QImage> grayscaleImages = QtConcurrent::filtered(images, &QImage::isGrayscale);
121
122 // create a set of all printable characters
123 QList<QChar> characters = ...;
124 QFuture<QSet<QChar> > set = QtConcurrent::filteredReduced(characters, &QChar::isPrint, &QSet<QChar>::insert);
125 //! [7]
126
127
128 //! [8]
129 // can mix normal functions and member functions with QtConcurrent::filteredReduced()
130
131 // create a dictionary of all lower cased strings
132 extern bool allLowerCase(const QString &string);
133 QStringList strings = ...;
134 QFuture<QSet<int> > averageWordLength = QtConcurrent::filteredReduced(strings, allLowerCase, QSet<QString>::insert);
135
136 // create a collage of all gray scale images
137 extern void addToCollage(QImage &collage, const QImage &grayscaleImage);
138 QList<QImage> images = ...;
139 QFuture<QImage> collage = QtConcurrent::filteredReduced(images, &QImage::isGrayscale, addToCollage);
140 //! [8]
141
142
143 //! [9]
144 bool QString::contains(const QRegularExpression ®exp) const;
145 //! [9]
146
147
148 //! [12]
149 QStringList strings = ...;
__anon55c5ca040102(const QString &str) 150 QFuture<QString> future = QtConcurrent::filtered(list, [](const QString &str) {
151 return str.contains(QRegularExpression("^\\S+$")); // matches strings without whitespace
152 });
153 //! [12]
154
155 //! [13]
156 struct StartsWith
157 {
StartsWithStartsWith158 StartsWith(const QString &string)
159 : m_string(string) { }
160
161 typedef bool result_type;
162
operator ()StartsWith163 bool operator()(const QString &testString)
164 {
165 return testString.startsWith(m_string);
166 }
167
168 QString m_string;
169 };
170
171 QList<QString> strings = ...;
172 QFuture<QString> fooString = QtConcurrent::filtered(strings, StartsWith(QLatin1String("Foo")));
173 //! [13]
174
175 //! [14]
176 struct StringTransform
177 {
178 void operator()(QString &result, const QString &value);
179 };
180
181 QFuture<QString> fooString =
182 QtConcurrent::filteredReduced<QString>(strings,
183 StartsWith(QLatin1String("Foo")),
184 StringTransform());
185 //! [14]
186