KDDockWidgets API Documentation 2.0
Loading...
Searching...
No Matches
KDStlContainerAdaptor.h
Go to the documentation of this file.
1/****************************************************************************
2** MIT License
3**
4** Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
5** Author: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
6**
7** This file is part of KDToolBox (https://github.com/KDAB/KDToolBox).
8**
9** Permission is hereby granted, free of charge, to any person obtaining a copy
10** of this software and associated documentation files (the "Software"), to deal
11** in the Software without restriction, including without limitation the rights
12** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13** copies of the Software, ** and to permit persons to whom the Software is
14** furnished to do so, subject to the following conditions:
15**
16** The above copyright notice and this permission notice (including the next paragraph)
17** shall be included in all copies or substantial portions of the Software.
18**
19** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23** LIABILITY, WHETHER IN AN ACTION OF ** CONTRACT, TORT OR OTHERWISE,
24** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25** DEALINGS IN THE SOFTWARE.
26****************************************************************************/
27
28// SPDX-License-Identifier: MIT
29
30#ifndef KDTOOLBOX_STLCONTAINERADAPTOR_H
31#define KDTOOLBOX_STLCONTAINERADAPTOR_H
32#pragma once
33
34#include <vector>
35#include <algorithm>
36#include <iterator>
37
38namespace KDToolBox {
39namespace StlContainerAdaptor {
40
41template<typename T, typename... Args>
42struct StdVectorAdaptor : std::vector<T, Args...>
43{
44 using base_container = std::vector<T, Args...>;
45 using base_size_type = typename base_container::size_type;
46 using size_type = int;
47 using value_type = typename base_container::value_type;
48 // using ConstIterator = typename base_container::const_iterator;
49
50 // Construction / RO5
51 using base_container::base_container;
52
53 StdVectorAdaptor() = default; // work around broken compilers in C++17 mode
62
63 // It's a lot of churn to fix all cases
64 //[[deprecated("Use clone() instead; copies of non-Qt containers are not cheap")]]
66 //[[deprecated("Use assignFrom() instead; copies of non-Qt containers are not cheap")]]
68
71
72 ~StdVectorAdaptor() = default;
73
75 {
76 return *this;
77 }
78
80 {
81 return *this = other;
82 }
84 {
85 return *this = std::move(other);
86 }
87
88 // Iterators
89 decltype(auto) constBegin() const
90 {
91 return this->cbegin();
92 }
93 decltype(auto) constEnd() const
94 {
95 return this->cend();
96 }
97
98 // Data access
99 decltype(auto) constData() const
100 {
101 return this->data();
102 }
103 decltype(auto) at(size_type i) const
104 {
105 return base_container::operator[](base_size_type(i));
106 }
107 decltype(auto) operator[](size_type i)
108 {
109 return base_container::operator[](base_size_type(i));
110 }
111 decltype(auto) operator[](size_type i) const
112 {
113 return base_container::operator[](base_size_type(i));
114 }
115
116 auto value(size_type i, const T &defaultValue) const
117 {
118 if (i >= 0 && i < size())
119 return at(i);
120 return defaultValue;
121 }
122 auto value(size_type i) const
123 {
124 if (i >= 0 && i < size())
125 return at(i);
126 return value_type();
127 }
128
129 decltype(auto) first()
130 {
131 return this->front();
132 }
133 decltype(auto) first() const
134 {
135 return this->front();
136 }
137 decltype(auto) constFirst() const
138 {
139 return this->front();
140 }
141
142 decltype(auto) last()
143 {
144 return this->back();
145 }
146 decltype(auto) last() const
147 {
148 return this->back();
149 }
150 decltype(auto) constLast() const
151 {
152 return this->back();
153 }
154
155 // Size and capacity
156 decltype(auto) isEmpty() const
157 {
158 return this->empty();
159 }
160 decltype(auto) size() const
161 {
162 return size_type(base_container::size());
163 }
164 decltype(auto) count() const
165 {
166 return size();
167 }
168 decltype(auto) length() const
169 {
170 return size();
171 }
172 decltype(auto) capacity() const
173 {
174 return size_type(base_container::capacity());
175 }
177 {
178 base_container::reserve(base_size_type(s));
179 }
180 void squeeze()
181 {
182 this->shrink_to_fit();
183 }
184
185
186 // Insertion
187 void append(const value_type &v)
188 {
189 this->push_back(v);
190 }
192 {
193 this->push_back(std::move(v));
194 }
195 void append(const StdVectorAdaptor &other)
196 {
197 if (this != &other) {
198 this->insert(this->end(), other.begin(), other.end());
199 } else {
200 this->reserve(2 * size());
201 std::copy(this->begin(), this->end(), std::back_inserter(*this));
202 }
203 }
204 void prepend(const value_type &v)
205 {
206 this->insert(this->begin(), v);
207 }
209 {
210 this->insert(this->begin(), std::move(v));
211 }
212
213 using base_container::insert;
214 decltype(auto) insert(size_type position, const value_type &v)
215 {
216 return this->insert(this->begin() + position, v);
217 }
218 decltype(auto) insert(size_type position, value_type &&v)
219 {
220 return this->insert(this->begin() + position, std::move(v));
221 }
222
223
224 // Removal
226 {
227 this->erase(this->begin());
228 }
230 {
231 this->pop_back();
232 }
233 void remove(size_type position)
234 {
235 this->erase(this->begin() + position);
236 }
238 {
239 const auto b = this->begin();
240 this->erase(b + position, b + position + count);
241 }
242 void removeAt(size_type position)
243 {
244 this->erase(this->begin() + position);
245 }
246
247 template<typename AT>
248 decltype(auto) removeAll(const AT &v)
249 {
250 const auto b = this->begin();
251 const auto e = this->end();
252 const auto i = std::remove(b, e, v);
253 const auto result = size_type(e - i);
254 this->erase(i, e);
255 return result;
256 }
257
258 template<typename AT>
259 bool removeOne(const AT &v)
260 {
261 const auto b = this->begin();
262 const auto e = this->end();
263 const auto i = std::find(b, e, v);
264 if (i == e)
265 return false;
266 this->erase(i);
267 return true;
268 }
269
270 decltype(auto) takeAt(size_type i)
271 {
272 const auto it = this->begin() + i;
273 const auto result = std::move(*it);
274 this->erase(it);
275 return result;
276 }
277 decltype(auto) takeLast()
278 {
279 return takeAt(size() - 1);
280 }
281 decltype(auto) takeFirst()
282 {
283 return takeAt(0);
284 }
285
286
287 // Search
288 template<typename AT>
289 bool contains(const AT &v) const
290 {
291 return indexOf(v) >= 0;
292 }
293
294 template<typename AT>
295 size_type indexOf(const AT &v, size_type from = 0) const
296 {
297 const auto s = size();
298 if (from < 0)
299 from = std::max(from + s, 0);
300 if (from < s) {
301 const auto b = this->begin();
302 const auto e = this->end();
303 const auto i = std::find(b + from, e, v);
304 if (i != e)
305 return size_type(i - b);
306 }
307 return size_type(-1);
308 }
309
310 template<typename AT>
311 size_type lastIndexOf(const AT &v, size_type from = -1) const
312 {
313 const auto s = size();
314 if (from < 0)
315 from += s;
316 else if (from >= s)
317 from = s - 1;
318
319 if (from >= 0) {
320 const auto b = this->begin();
321
322 const auto revB = std::make_reverse_iterator(b + from + 1);
323 const auto revE = std::make_reverse_iterator(b);
324 const auto i = std::find(revB, revE, v);
325 if (i != revE)
326 return size_type(i.base() - b) - 1;
327 }
328 return size_type(-1);
329 }
330
331 template<typename AT>
332 bool startsWith(const AT &v) const
333 {
334 return this->front() == v;
335 }
336
337 template<typename AT>
338 bool endsWith(const AT &v) const
339 {
340 return this->back() == v;
341 }
342
343
344 // Miscellanea
346 {
347 if (i < 0)
348 i = size();
349 this->assign(base_size_type(i), v);
350 return *this;
351 }
352
354 {
355 const auto s = size();
356
357 if (len < 0)
358 len = s;
359 len = std::min(len, s - pos);
360
361 const auto b = this->begin() + pos;
362 const auto e = b + len;
363 return StdVectorAdaptor(b, e);
364 }
365
366 void move(size_type from, size_type to)
367 {
368 const auto b = this->begin();
369 if (from < to)
370 std::rotate(b + from, b + from + 1, b + to + 1);
371 else
372 std::rotate(b + to, b + from, b + from + 1);
373 }
374
375 void replace(size_type pos, const value_type &v)
376 {
377 at(pos) = v;
378 }
379
381 {
382 qSwap(at(i), at(j));
383 }
384
385
386 // Misc operators
387 template<typename AT>
388 decltype(auto) operator<<(const typename StdVectorAdaptor<AT>::value_type &v)
389 {
390 push_back(v);
391 return *this;
392 }
393 template<typename AT>
394 decltype(auto) operator<<(typename StdVectorAdaptor<AT>::value_type &&v)
395 {
396 push_back(std::move(v));
397 return *this;
398 }
399
400 template<typename AT>
401 decltype(auto) operator<<(const StdVectorAdaptor<AT> &rhs)
402 {
403 append(rhs);
404 return *this;
405 }
406
407 template<typename AT>
408 decltype(auto) operator+(const StdVectorAdaptor<AT> &rhs)
409 {
411 result.reserve(size() + rhs.size());
412 result.insert(result.end(), base_container::begin(), base_container::end());
413 result.insert(result.end(), rhs.begin(), rhs.end());
414 return result;
415 }
416
417 decltype(auto) operator+=(const StdVectorAdaptor &other)
418 {
419 append(other);
420 return *this;
421 }
422};
423
424} // namespace StlContainerAdaptor
425} // namespace KDToolBox
426
427namespace KDDockWidgets {
428
429template<typename T, typename... Args>
431
432}
433
434#endif // KDTOOLBOX_STLCONTAINERADAPTOR_H
Class to abstract QAction, so code still works with QtQuick and Flutter.
void replace(size_type pos, const value_type &v)
size_type lastIndexOf(const AT &v, size_type from=-1) const
StdVectorAdaptor(size_type count, const value_type &v)
StdVectorAdaptor mid(size_type pos, size_type len=-1)
decltype(auto) insert(size_type position, const value_type &v)
typename base_container::size_type base_size_type
StdVectorAdaptor & operator=(const StdVectorAdaptor &)=default
StdVectorAdaptor & fill(const value_type &v, size_type i=-1)
decltype(auto) insert(size_type position, value_type &&v)
size_type indexOf(const AT &v, size_type from=0) const
StdVectorAdaptor & operator=(StdVectorAdaptor &&)=default
void remove(size_type position, size_type count)
StdVectorAdaptor & assignFrom(StdVectorAdaptor &&other)
StdVectorAdaptor(const StdVectorAdaptor &)=default
StdVectorAdaptor & assignFrom(const StdVectorAdaptor &other)
StdVectorAdaptor(StdVectorAdaptor &&)=default
auto value(size_type i, const T &defaultValue) const

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDDockWidgets
Advanced Dock Widget Framework for Qt
https://www.kdab.com/development-resources/qt-tools/kddockwidgets/
Generated by doxygen 1.9.8