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 QtQml module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
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 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | #ifndef VECTOR_H |
40 | #define VECTOR_H |
41 | |
42 | #include <vector> |
43 | #include <wtf/Assertions.h> |
44 | #include <wtf/NotFound.h> |
45 | #include <qalgorithms.h> |
46 | |
47 | enum WTF_UnusedOverflowMode { |
48 | UnsafeVectorOverflow |
49 | }; |
50 | |
51 | namespace WTF { |
52 | |
53 | template <typename T, int capacity = 1, int overflowMode = UnsafeVectorOverflow> |
54 | class Vector : public std::vector<T> { |
55 | public: |
56 | Vector() {} |
57 | Vector(int initialSize) : std::vector<T>(initialSize) {} |
58 | Vector(std::initializer_list<T> list) : std::vector<T>(list) {} |
59 | |
60 | inline void append(const T& value) |
61 | { this->push_back(value); } |
62 | |
63 | template <typename OtherType> |
64 | inline void append(const OtherType& other) |
65 | { this->push_back(T(other)); } |
66 | |
67 | inline void append(T&& other) |
68 | { this->push_back(std::move(other)); } |
69 | |
70 | inline void append(const Vector<T>& vector) |
71 | { |
72 | this->insert(this->end(), vector.begin(), vector.end()); |
73 | } |
74 | |
75 | inline void append(const T* ptr, size_t count) |
76 | { |
77 | for (size_t i = 0; i < count; ++i) |
78 | this->push_back(T(ptr[i])); |
79 | } |
80 | |
81 | inline void append(typename std::vector<T>::const_iterator it, size_t count) |
82 | { |
83 | for (size_t i = 0; i < count; ++i, ++it) |
84 | this->push_back(*it); |
85 | } |
86 | |
87 | unsigned size() const { return static_cast<unsigned>(std::vector<T>::size()); } |
88 | |
89 | using std::vector<T>::insert; |
90 | |
91 | inline void reserveInitialCapacity(size_t size) { this->reserve(size); } |
92 | |
93 | inline void insert(size_t position, T value) |
94 | { this->insert(this->begin() + position, value); } |
95 | |
96 | inline void grow(size_t size) |
97 | { this->resize(size); } |
98 | |
99 | inline void shrink(size_t size) |
100 | { this->erase(this->begin() + size, this->end()); } |
101 | |
102 | inline void shrinkToFit() |
103 | { this->shrink(this->size()); } |
104 | |
105 | inline void remove(size_t position) |
106 | { this->erase(this->begin() + position); } |
107 | |
108 | inline bool isEmpty() const { return this->empty(); } |
109 | |
110 | inline T &last() { return *(this->begin() + this->size() - 1); } |
111 | |
112 | bool contains(const T &value) const |
113 | { |
114 | for (const T &inVector : *this) { |
115 | if (inVector == value) |
116 | return true; |
117 | } |
118 | return false; |
119 | } |
120 | }; |
121 | |
122 | template <typename T, int capacity> |
123 | void deleteAllValues(const Vector<T, capacity> &vector) |
124 | { |
125 | qDeleteAll(vector); |
126 | } |
127 | |
128 | } |
129 | |
130 | using WTF::Vector; |
131 | using WTF::deleteAllValues; |
132 | |
133 | #endif // VECTOR_H |
134 | |