Debian Codegraph @

Source/Utilities/Container/CQtManagedRestrictedModificationList.h

raw
1 /*
2 * Copyright (C) 2013-2015, 2019 by the Konclude Developer Team.
3 *
4 * This file is part of the reasoning system Konclude.
5 * For details and support, see <http://konclude.com/>.
6 *
7 * Konclude is free software: you can redistribute it and/or modify
8 * it under the terms of version 3 of the GNU Lesser General Public
9 * License (LGPLv3) as published by the Free Software Foundation.
10 *
11 * Konclude is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU (Lesser) General Public License for more details.
15 *
16 * You should have received a copy of the GNU (Lesser) General Public
17 * License along with Konclude. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * This file incorporates work covered by the following copyright and
20 * permission notice (to be compatible with LGPLv2.1,
21 * this code is available/released under GPLv3,
22 * see https://www.gnu.org/licenses/gpl-faq.html#AllCompatibility):
23 *
24 *
25 * Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
26 * Contact: http://www.qt-project.org/legal
27 *
28 * This file is part of the QtCore module of the Qt Toolkit.
29 *
30 * $QT_BEGIN_LICENSE:LGPL$
31 * Commercial License Usage
32 * Licensees holding valid commercial Qt licenses may use this file in
33 * accordance with the commercial license agreement provided with the
34 * Software or, alternatively, in accordance with the terms contained in
35 * a written agreement between you and Digia. For licensing terms and
36 * conditions see http://qt.digia.com/licensing. For further information
37 * use the contact form at http://qt.digia.com/contact-us.
38 *
39 * GNU Lesser General Public License Usage
40 * Alternatively, this file may be used under the terms of the GNU Lesser
41 * General Public License version 2.1 as published by the Free Software
42 * Foundation and appearing in the file LICENSE.LGPL included in the
43 * packaging of this file. Please review the following information to
44 * ensure the GNU Lesser General Public License version 2.1 requirements
45 * will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
46 *
47 * In addition, as a special exception, Digia gives you certain additional
48 * rights. These rights are described in the Digia Qt LGPL Exception
49 * version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
50 *
51 * GNU General Public License Usage
52 * Alternatively, this file may be used under the terms of the GNU
53 * General Public License version 3.0 as published by the Free Software
54 * Foundation and appearing in the file LICENSE.GPL included in the
55 * packaging of this file. Please review the following information to
56 * ensure the GNU General Public License version 3.0 requirements will be
57 * met: http://www.gnu.org/copyleft/gpl.html.
58 *
59 *
60 * $QT_END_LICENSE$
61 *
62 *
63 */
64
65 #ifndef KONCLUDE_UTILITIES_CONTAINER_CQTMANAGEDRESTRICTEDMODIFICATIONLIST_H
66 #define KONCLUDE_UTILITIES_CONTAINER_CQTMANAGEDRESTRICTEDMODIFICATIONLIST_H
67
68 // Namespace includes
69 #include "ContainerSettings.h"
70
71 #ifdef KONCLUDE_EMPLOY_QT_MANAGED_RESTRICTED_MODIFICATION_LIST_CONTAINER
72 #include "CQtManagedRestrictedModificationListData.h"
73 #include "CQtManagedRestrictedModificationListBase.h"
74
75 // Library includes
76 #include <QList>
77
78 // Other includes
79 #include "Context/CContext.h"
80
81
82 // Logger includes
83 #include "Logger/CLogger.h"
84
85
86 namespace Konclude {
87
88 using namespace Context;
89
90 namespace Utilities {
91
92 namespace Container {
93
94
95 /*!
96 *
97 * \class CQtManagedRestrictedModificationList
98 * \author Andreas Steigmiller
99 * \version 0.1
100 * \brief TODO
101 *
102 */
103 template <typename T>
104 class CQtManagedRestrictedModificationList : public CQtManagedRestrictedModificationListBase
105 {
106 struct Node { void *v;
107 #if defined(Q_CC_BOR)
108 Q_INLINE_TEMPLATE T &t();
109 #else
110 Q_INLINE_TEMPLATE T &t()
111 { return *reinterpret_cast<T*>(QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic
112 ? v : this); }
113 #endif
114 };
115
116 union { CQtManagedRestrictedModificationListData p; CQtManagedRestrictedModificationListData::Data *d; };
117 CContext* mContext;
118 CMemoryAllocationManager* mMemMan;
119
120 public:
121 inline CQtManagedRestrictedModificationList() : d(&CQtManagedRestrictedModificationListData::shared_null),mContext(nullptr) {
122 mMemMan = CContext::getMemoryAllocationManager(mContext);
123 /*d->ref.ref();*/
124 }
125 inline CQtManagedRestrictedModificationList(CContext* context) : d(&CQtManagedRestrictedModificationListData::shared_null),mContext(context) {
126 mMemMan = CContext::getMemoryAllocationManager(mContext);
127 /*d->ref.ref();*/
128 }
129 inline CQtManagedRestrictedModificationList(const CQtManagedRestrictedModificationList<T> &l) : d(l.d),mContext(nullptr) {
130 mMemMan = CContext::getMemoryAllocationManager(mContext);
131 /*d->ref.ref();*/ if (!d->sharable) detach_helper();
132 }
133 inline CQtManagedRestrictedModificationList(const CQtManagedRestrictedModificationList<T> &l, CContext* context) : d(l.d),mContext(context) {
134 mMemMan = CContext::getMemoryAllocationManager(mContext);
135 /*d->ref.ref();*/ if (!d->sharable) detach_helper();
136 }
137
138 ~CQtManagedRestrictedModificationList();
139
140 CQtManagedRestrictedModificationList<T>* init(CContext* context = nullptr);
141 CQtManagedRestrictedModificationList<T>* init(const CQtManagedRestrictedModificationList<T> &other, CContext* context = nullptr);
142 CQtManagedRestrictedModificationList<T>* init(const CQtManagedRestrictedModificationList<T> *other, CContext* context = nullptr);
143
144 CQtManagedRestrictedModificationList<T> &operator=(const CQtManagedRestrictedModificationList<T> &l);
145
146
147
148 bool operator==(const CQtManagedRestrictedModificationList<T> &l) const;
149 inline bool operator!=(const CQtManagedRestrictedModificationList<T> &l) const { return !(*this == l); }
150
151 inline int size() const { return p.size(); }
152
153 inline void detach() { if (d->refList != this/*d->ref != 1*/) detach_helper(); }
154
155 inline void detachShared()
156 {
157 // The "this->" qualification is needed for GCCE.
158 if (d->refList != this/*d->ref != 1*/ && this->d != &CQtManagedRestrictedModificationListData::shared_null)
159 detach_helper();
160 }
161
162 inline bool isDetached() const { return /*d->ref == 1*/d->refList == this; }
163 inline void setSharable(bool sharable) { if (!sharable) detach(); d->sharable = sharable; }
164 inline bool isSharedWith(const CQtManagedRestrictedModificationList<T> &other) const { return d == other.d; }
165
166 inline bool isEmpty() const { return p.isEmpty(); }
167
168 void clear();
169
170 const T &at(int i) const;
171 const T &operator[](int i) const;
172 T &operator[](int i);
173
174 void reserve(int size);
175 void append(const T &t);
176 void append(const CQtManagedRestrictedModificationList<T> &t);
177 void prepend(const T &t);
178 void insert(int i, const T &t);
179 void replace(int i, const T &t);
180 void removeAt(int i);
181 int removeAll(const T &t);
182 bool removeOne(const T &t);
183 T takeAt(int i);
184 T takeFirst();
185 T takeLast();
186 void move(int from, int to);
187 void swap(int i, int j);
188 int indexOf(const T &t, int from = 0) const;
189 int lastIndexOf(const T &t, int from = -1) const;
190 bool contains(const T &t) const;
191 int count(const T &t) const;
192
193 class const_iterator;
194
195 class iterator {
196 public:
197 Node *i;
198 typedef std::random_access_iterator_tag iterator_category;
199 typedef qptrdiff difference_type;
200 typedef T value_type;
201 typedef T *pointer;
202 typedef T &reference;
203
204 inline iterator() : i(0) {}
205 inline iterator(Node *n) : i(n) {}
206 inline iterator(const iterator &o): i(o.i){}
207 inline T &operator*() const { return i->t(); }
208 inline T *operator->() const { return &i->t(); }
209 inline T &operator[](int j) const { return i[j].t(); }
210 inline bool operator==(const iterator &o) const { return i == o.i; }
211 inline bool operator!=(const iterator &o) const { return i != o.i; }
212 inline bool operator<(const iterator& other) const { return i < other.i; }
213 inline bool operator<=(const iterator& other) const { return i <= other.i; }
214 inline bool operator>(const iterator& other) const { return i > other.i; }
215 inline bool operator>=(const iterator& other) const { return i >= other.i; }
216 #ifndef QT_STRICT_ITERATORS
217 inline bool operator==(const const_iterator &o) const
218 { return i == o.i; }
219 inline bool operator!=(const const_iterator &o) const
220 { return i != o.i; }
221 inline bool operator<(const const_iterator& other) const
222 { return i < other.i; }
223 inline bool operator<=(const const_iterator& other) const
224 { return i <= other.i; }
225 inline bool operator>(const const_iterator& other) const
226 { return i > other.i; }
227 inline bool operator>=(const const_iterator& other) const
228 { return i >= other.i; }
229 #endif
230 inline iterator &operator++() { ++i; return *this; }
231 inline iterator operator++(int) { Node *n = i; ++i; return n; }
232 inline iterator &operator--() { i--; return *this; }
233 inline iterator operator--(int) { Node *n = i; i--; return n; }
234 inline iterator &operator+=(int j) { i+=j; return *this; }
235 inline iterator &operator-=(int j) { i-=j; return *this; }
236 inline iterator operator+(int j) const { return iterator(i+j); }
237 inline iterator operator-(int j) const { return iterator(i-j); }
238 inline int operator-(iterator j) const { return int(i - j.i); }
239 };
240 friend class iterator;
241
242 class const_iterator {
243 public:
244 Node *i;
245 typedef std::random_access_iterator_tag iterator_category;
246 typedef qptrdiff difference_type;
247 typedef T value_type;
248 typedef const T *pointer;
249 typedef const T &reference;
250
251 inline const_iterator() : i(0) {}
252 inline const_iterator(Node *n) : i(n) {}
253 inline const_iterator(const const_iterator &o): i(o.i) {}
254 #ifdef QT_STRICT_ITERATORS
255 inline explicit const_iterator(const iterator &o): i(o.i) {}
256 #else
257 inline const_iterator(const iterator &o): i(o.i) {}
258 #endif
259 inline const T &operator*() const { return i->t(); }
260 inline const T *operator->() const { return &i->t(); }
261 inline const T &operator[](int j) const { return i[j].t(); }
262 inline bool operator==(const const_iterator &o) const { return i == o.i; }
263 inline bool operator!=(const const_iterator &o) const { return i != o.i; }
264 inline bool operator<(const const_iterator& other) const { return i < other.i; }
265 inline bool operator<=(const const_iterator& other) const { return i <= other.i; }
266 inline bool operator>(const const_iterator& other) const { return i > other.i; }
267 inline bool operator>=(const const_iterator& other) const { return i >= other.i; }
268 inline const_iterator &operator++() { ++i; return *this; }
269 inline const_iterator operator++(int) { Node *n = i; ++i; return n; }
270 inline const_iterator &operator--() { i--; return *this; }
271 inline const_iterator operator--(int) { Node *n = i; i--; return n; }
272 inline const_iterator &operator+=(int j) { i+=j; return *this; }
273 inline const_iterator &operator-=(int j) { i-=j; return *this; }
274 inline const_iterator operator+(int j) const { return const_iterator(i+j); }
275 inline const_iterator operator-(int j) const { return const_iterator(i-j); }
276 inline int operator-(const_iterator j) const { return i - j.i; }
277 };
278 friend class const_iterator;
279
280 // stl style
281 inline iterator begin() { detach(); return reinterpret_cast<Node *>(p.begin()); }
282 inline const_iterator begin() const { return reinterpret_cast<Node *>(p.begin()); }
283 inline const_iterator constBegin() const { return reinterpret_cast<Node *>(p.begin()); }
284 inline iterator end() { detach(); return reinterpret_cast<Node *>(p.end()); }
285 inline const_iterator end() const { return reinterpret_cast<Node *>(p.end()); }
286 inline const_iterator constEnd() const { return reinterpret_cast<Node *>(p.end()); }
287 iterator insert(iterator before, const T &t);
288 iterator erase(iterator pos);
289 iterator erase(iterator first, iterator last);
290
291 // more Qt
292 typedef iterator Iterator;
293 typedef const_iterator ConstIterator;
294 inline int count() const { return p.size(); }
295 inline int length() const { return p.size(); } // Same as count()
296 inline T& first() { Q_ASSERT(!isEmpty()); return *begin(); }
297 inline const T& first() const { Q_ASSERT(!isEmpty()); return at(0); }
298 T& last() { Q_ASSERT(!isEmpty()); return *(--end()); }
299 const T& last() const { Q_ASSERT(!isEmpty()); return at(count() - 1); }
300 inline void removeFirst() { Q_ASSERT(!isEmpty()); erase(begin()); }
301 inline void removeLast() { Q_ASSERT(!isEmpty()); erase(--end()); }
302 inline bool startsWith(const T &t) const { return !isEmpty() && first() == t; }
303 inline bool endsWith(const T &t) const { return !isEmpty() && last() == t; }
304 CQtManagedRestrictedModificationList<T> mid(int pos, int length = -1) const;
305
306 T value(int i) const;
307 T value(int i, const T &defaultValue) const;
308
309 // stl compatibility
310 inline void push_back(const T &t) { append(t); }
311 inline void push_front(const T &t) { prepend(t); }
312 inline T& front() { return first(); }
313 inline const T& front() const { return first(); }
314 inline T& back() { return last(); }
315 inline const T& back() const { return last(); }
316 inline void pop_front() { removeFirst(); }
317 inline void pop_back() { removeLast(); }
318 inline bool empty() const { return isEmpty(); }
319 typedef int size_type;
320 typedef T value_type;
321 typedef value_type *pointer;
322 typedef const value_type *const_pointer;
323 typedef value_type &reference;
324 typedef const value_type &const_reference;
325 typedef qptrdiff difference_type;
326
327 #ifdef QT3_SUPPORT
328 inline QT3_SUPPORT iterator remove(iterator pos) { return erase(pos); }
329 inline QT3_SUPPORT int remove(const T &t) { return removeAll(t); }
330 inline QT3_SUPPORT int findIndex(const T& t) const { return indexOf(t); }
331 inline QT3_SUPPORT iterator find(const T& t)
332 { int i = indexOf(t); return (i == -1 ? end() : (begin()+i)); }
333 inline QT3_SUPPORT const_iterator find (const T& t) const
334 { int i = indexOf(t); return (i == -1 ? end() : (begin()+i)); }
335 inline QT3_SUPPORT iterator find(iterator from, const T& t)
336 { int i = indexOf(t, from - begin()); return i == -1 ? end() : begin()+i; }
337 inline QT3_SUPPORT const_iterator find(const_iterator from, const T& t) const
338 { int i = indexOf(t, from - begin()); return i == -1 ? end() : begin()+i; }
339 #endif
340
341 // comfort
342 CQtManagedRestrictedModificationList<T> &operator+=(const CQtManagedRestrictedModificationList<T> &l);
343 inline CQtManagedRestrictedModificationList<T> operator+(const CQtManagedRestrictedModificationList<T> &l) const
344 { CQtManagedRestrictedModificationList n = *this; n += l; return n; }
345 inline CQtManagedRestrictedModificationList<T> &operator+=(const T &t)
346 { append(t); return *this; }
347 inline CQtManagedRestrictedModificationList<T> &operator<< (const T &t)
348 { append(t); return *this; }
349 inline CQtManagedRestrictedModificationList<T> &operator<<(const CQtManagedRestrictedModificationList<T> &l)
350 { *this += l; return *this; }
351
352 QVector<T> toVector() const;
353 QSet<T> toSet() const;
354
355 static CQtManagedRestrictedModificationList<T> fromVector(const QVector<T> &vector);
356 static CQtManagedRestrictedModificationList<T> fromSet(const QSet<T> &set);
357
358 #ifndef QT_NO_STL
359 static inline CQtManagedRestrictedModificationList<T> fromStdList(const std::list<T> &list)
360 { CQtManagedRestrictedModificationList<T> tmp; qCopy(list.begin(), list.end(), std::back_inserter(tmp)); return tmp; }
361 inline std::list<T> toStdList() const
362 { std::list<T> tmp; qCopy(constBegin(), constEnd(), std::back_inserter(tmp)); return tmp; }
363 #endif
364
365 private:
366 Node *detach_helper_grow(int i, int n);
367 void detach_helper(int alloc);
368 void detach_helper();
369 void free(CQtManagedRestrictedModificationListData::Data *d);
370
371 void node_construct(Node *n, const T &t);
372 void node_destruct(Node *n);
373 void node_copy(Node *from, Node *to, Node *src);
374 void node_destruct(Node *from, Node *to);
375 };
376
377
378 }; // end namespace Container
379
380 }; // end namespace Utilities
381
382 }; // end namespace Konclude
383
384 #include "CQtManagedRestrictedModificationList.cpp"
385
386 #endif // KONCLUDE_EMPLOY_QT_MANAGED_RESTRICTED_MODIFICATION_LIST_CONTAINER
387
388 #endif // KONCLUDE_UTILITIES_CONTAINER_CQTMANAGEDRESTRICTEDMODIFICATIONLIST_H