src/types/resourcevector.h

Mon, 06 Mar 2017 00:36:40 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Mon, 06 Mar 2017 00:36:40 +0200
changeset 1195
fead30e6c557
parent 1191
26b3f1e80a9c
permissions
-rw-r--r--

ColorSelector is no longer a HierarchyElement.

1185
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 * Copyright (C) 2013 - 2017 Teemu Piippo
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
1186
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
19 #pragma once
1185
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 #include <QVector>
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 /*
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 * A vector that allocates elements on the heap and manages their lifetime.
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 */
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 template<typename T>
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 class ResourceVector
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 {
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 public:
1186
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
29 using Filter = std::function<bool(T*, int)>;
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
30 using Callback = std::function<void(T*, int)>;
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
31
1191
26b3f1e80a9c Also removed the copy constructor and copy assignment because those only are bad news for a resource managing vector...
Teemu Piippo <teemu@hecknology.net>
parents: 1190
diff changeset
32 ResourceVector() = default;
26b3f1e80a9c Also removed the copy constructor and copy assignment because those only are bad news for a resource managing vector...
Teemu Piippo <teemu@hecknology.net>
parents: 1190
diff changeset
33 ResourceVector(const ResourceVector<T>&) = delete;
1185
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 ~ResourceVector();
1190
2edf54a5e56a and let's put a newline there...
Teemu Piippo <teemu@hecknology.net>
parents: 1189
diff changeset
35
1185
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 void append(T) = delete;
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 template<typename TT = T, typename... Args>
1186
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
38 TT* append(Args&&... args);
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
39 void assimilate(int position, T* resource);
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
40 T* const* begin() const;
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
41 T* const* end() const;
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
42 int indexOf(T* resource) const;
1185
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 template<typename TT = T, typename... Args>
1186
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
44 TT* insert(int position, Args&&... args);
1189
0509b2b5eaa6 Deleted the copy constructor version of ResourceVector<T>::insert, like with append
Teemu Piippo <teemu@hecknology.net>
parents: 1186
diff changeset
45 void insert(int, T) = delete;
1186
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
46 bool isEmpty() const;
1185
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 void clear();
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 void removeAt(int position);
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 T* operator[](int position) const;
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 int size() const;
1186
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
51 void swap(int position1, int position2);
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
52 void merge(ResourceVector<T>& other, int position = -1, Filter filter = nullptr, Callback callback = nullptr);
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
53 const QVector<T*>& toQVector() const;
1185
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54
1191
26b3f1e80a9c Also removed the copy constructor and copy assignment because those only are bad news for a resource managing vector...
Teemu Piippo <teemu@hecknology.net>
parents: 1190
diff changeset
55 void operator=(const ResourceVector<T>&) = delete;
26b3f1e80a9c Also removed the copy constructor and copy assignment because those only are bad news for a resource managing vector...
Teemu Piippo <teemu@hecknology.net>
parents: 1190
diff changeset
56
1185
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 private:
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 QVector<T*> _data;
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 };
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 template<typename T>
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 int countof(const ResourceVector<T>& vector);
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 /*
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 * Frees the resources when the vector is deleted.
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 */
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 template<typename T>
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 ResourceVector<T>::~ResourceVector()
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 {
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 clear();
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 }
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 /*
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 * Creates a new resource and adds it to the end of this vector.
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 */
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 template<typename T>
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 template<typename TT, typename... Args>
1186
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
78 TT* ResourceVector<T>::append(Args&&... args)
1185
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 {
1186
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
80 TT* resource = new TT {args...};
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
81 _data.append(resource);
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
82 return resource;
1185
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 }
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 /*
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 * Returns an iterator to the beginning of this vector.
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 */
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 template<typename T>
1186
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
89 T* const* ResourceVector<T>::begin() const
1185
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 {
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 return _data.cbegin();
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 }
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 /*
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 * Returns an iterator to the end of this vector.
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 */
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 template<typename T>
1186
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
98 T* const* ResourceVector<T>::end() const
1185
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 {
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 return _data.cend();
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101 }
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
103 /*
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104 * Creates a new resource and inserts it to the specified position in this vector.
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105 */
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 template<typename T>
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 template<typename TT, typename... Args>
1186
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
108 TT* ResourceVector<T>::insert(int position, Args&&... args)
1185
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 {
1186
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
110 TT* resource = new TT {args...};
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
111 _data.insert(position, resource);
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
112 return resource;
1185
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
113 }
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
114
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
115 /*
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
116 * Deletes all resources in this vector.
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
117 */
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
118 template<typename T>
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
119 void ResourceVector<T>::clear()
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
120 {
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
121 for (T* element : _data)
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
122 delete element;
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
123
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
124 _data.clear();
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
125 }
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
126
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
127 /*
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
128 * Removes the resource at the provided position. Throws an exception if the index is out of range.
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
129 */
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
130 template<typename T>
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
131 void ResourceVector<T>::removeAt(int position)
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
132 {
1186
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
133 if (position >= 0 and position < _data.size())
1185
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
134 {
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
135 delete _data[position];
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
136 _data.removeAt(position);
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
137 }
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
138 else
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
139 {
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
140 throw std::domain_error {"index out of range"};
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
141 }
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
142 }
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
143
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
144 /*
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
145 * Returns the resource at the provided position. Throws an exception if the index is out of range.
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
146 */
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
147 template<typename T>
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
148 T* ResourceVector<T>::operator[](int position) const
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
149 {
1186
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
150 if (position >= 0 and position < _data.size())
1185
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
151 return _data[position];
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
152 else
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
153 throw std::domain_error {"index out of range"};
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
154 }
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
155
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
156 /*
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
157 * Returns the amount of resources in this vector.
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
158 */
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
159 template<typename T>
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
160 int ResourceVector<T>::size() const
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
161 {
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
162 return _data.size();
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
163 }
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
164
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
165 /*
1186
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
166 * Inserts an already existing resource into the vector. The resource is assumed to not be managed by anything else!
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
167 * Please think twice before using this method.
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
168 */
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
169 template<typename T>
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
170 void ResourceVector<T>::assimilate(int position, T* resource)
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
171 {
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
172 _data.insert(position, resource);
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
173 }
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
174
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
175 /*
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
176 * Returns the index of the provided resource in the vector, or -1 if not found.
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
177 * This operation is of linear complexity O(n).
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
178 */
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
179 template<typename T>
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
180 int ResourceVector<T>::indexOf(T* resource) const
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
181 {
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
182 return _data.indexOf(resource);
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
183 }
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
184
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
185 /*
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
186 * Swaps the locations of two resource in this vector.
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
187 */
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
188 template<typename T>
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
189 void ResourceVector<T>::swap(int position1, int position2)
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
190 {
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
191 if (position1 >= 0 and position2 >= 0 and position1 < size() and position2 < size())
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
192 {
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
193 qSwap(_data[position1], _data[position2]);
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
194 }
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
195 else
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
196 {
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
197 throw std::domain_error {"index out of range"};
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
198 }
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
199 }
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
200
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
201 /*
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
202 * Returns whether or not the vector is empty.
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
203 */
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
204 template<typename T>
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
205 bool ResourceVector<T>::isEmpty() const
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
206 {
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
207 return size() == 0;
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
208 }
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
209
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
210 /*
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
211 * Migrates resources from the provided resource vector to this one.
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
212 * - `position` indicates where to insert the new resources. Default is to the end.
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
213 * - `filter` specifies a filter function for the migration. Objects not passing the filter will not be migrated.
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
214 * - `callback` specifies a function that is called for each successfully migrated object.
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
215 */
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
216 template<typename T>
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
217 void ResourceVector<T>::merge(ResourceVector<T>& other, int position, Filter filter, Callback callback)
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
218 {
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
219 if (position < 0)
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
220 position = size();
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
221
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
222 if (filter == nullptr)
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
223 filter = [](T*, int){return true;};
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
224
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
225 if (callback == nullptr)
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
226 callback = [](T*, int){};
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
227
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
228 for (int i = 0; i < other.size();)
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
229 {
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
230 T* resource = other[i];
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
231
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
232 if (filter(resource, i))
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
233 {
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
234 _data.insert(position, resource);
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
235 other._data.removeAt(i);
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
236 callback(resource, position);
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
237 position += 1;
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
238 }
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
239 else
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
240 {
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
241 i += 1;
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
242 }
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
243 }
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
244 }
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
245
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
246 /*
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
247 * Returns the underlying vector.
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
248 */
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
249 template<typename T>
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
250 const QVector<T*>& ResourceVector<T>::toQVector() const
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
251 {
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
252 return _data;
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
253 }
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
254
eae8b3bce545 Moved LDObject lifetime management from Model to ResourceVector. This is a large refactor that removes some hacks from the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1185
diff changeset
255 /*
1185
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
256 * countof() overload for resource vectors. Returns the amount of resources in the given vector.
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
257 */
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
258 template<typename T>
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
259 int countof(const ResourceVector<T>& vector)
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
260 {
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
261 return vector.size();
c2e0db52ea07 Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
262 }

mercurial