Sun, 05 Mar 2017 13:33:37 +0200
Added a new type ResourceVector which manages object lifetimes, and used it to remove a bit of memory management.
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 | |
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
|
19 | #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
|
20 | |
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 | * 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
|
23 | */ |
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 | 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
|
25 | 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
|
26 | { |
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 | public: |
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 | ~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
|
29 | 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
|
30 | template<typename TT = T, typename... Args> |
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
|
31 | T* append(Args&&... args); |
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
|
32 | T** const begin() 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
|
33 | T** const end() 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
|
34 | template<typename TT = T, typename... Args> |
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
|
35 | T* insert(int position, Args&&... args); |
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 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
|
37 | 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
|
38 | 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
|
39 | int 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
|
40 | |
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
|
41 | 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
|
42 | 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
|
43 | }; |
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
|
44 | |
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
|
45 | 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
|
46 | 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
|
47 | |
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 | /* |
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 | * 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
|
50 | */ |
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
|
51 | 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
|
52 | 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
|
53 | { |
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 | 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
|
55 | } |
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
|
56 | |
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 | /* |
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 | * 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
|
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 | 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
|
61 | template<typename TT, typename... Args> |
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 | T* ResourceVector<T>::append(Args&&... args) |
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 | _data.append(new TT {args...}); |
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 | return _data[size() - 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
|
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 | |
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 | /* |
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 | * 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
|
70 | */ |
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 | 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
|
72 | T** const ResourceVector<T>::begin() 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
|
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 | 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
|
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 | |
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 | /* |
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
|
78 | * 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
|
79 | */ |
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
|
80 | 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
|
81 | T** const ResourceVector<T>::end() 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
|
82 | { |
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 | 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
|
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 | /* |
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 | * 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
|
88 | */ |
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
|
89 | 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
|
90 | template<typename TT, typename... Args> |
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 | T* ResourceVector<T>::insert(int position, Args&&... args) |
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 | _data.insert(position, new TT {args...}); |
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 | |
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 | * 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
|
98 | */ |
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 | 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
|
100 | 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
|
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 | 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
|
103 | 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
|
104 | |
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 | _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
|
106 | } |
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 | |
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
|
108 | /* |
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 | * 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
|
110 | */ |
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
|
111 | 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
|
112 | 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
|
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 | if (position < _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
|
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 | 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
|
117 | _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
|
118 | } |
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 | 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
|
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 | 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
|
122 | } |
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 | |
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 | * 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
|
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 | 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
|
129 | 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
|
130 | { |
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 | if (position < _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
|
132 | 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
|
133 | 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
|
134 | 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
|
135 | } |
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 | |
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 | * 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
|
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 | 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
|
141 | 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
|
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 | 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
|
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 | |
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 | * 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
|
148 | */ |
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 | 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
|
150 | 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
|
151 | { |
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 | 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
|
153 | } |