forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasyncTaskCollection.cxx
More file actions
266 lines (233 loc) · 6.17 KB
/
asyncTaskCollection.cxx
File metadata and controls
266 lines (233 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file asyncTaskCollection.cxx
* @author drose
* @date 2008-09-16
*/
#include "asyncTaskCollection.h"
#include "indent.h"
/**
*
*/
AsyncTaskCollection::
AsyncTaskCollection() {
}
/**
*
*/
AsyncTaskCollection::
AsyncTaskCollection(const AsyncTaskCollection ©) :
_tasks(copy._tasks)
{
}
/**
*
*/
void AsyncTaskCollection::
operator = (const AsyncTaskCollection ©) {
_tasks = copy._tasks;
}
/**
* Adds a new AsyncTask to the collection.
*/
void AsyncTaskCollection::
add_task(AsyncTask *task) {
// If the pointer to our internal array is shared by any other
// AsyncTaskCollections, we have to copy the array now so we won't
// inadvertently modify any of our brethren AsyncTaskCollection objects.
nassertv(task != nullptr);
if (_tasks.get_ref_count() > 1) {
AsyncTasks old_tasks = _tasks;
_tasks = AsyncTasks::empty_array(0);
_tasks.v() = old_tasks.v();
}
_tasks.push_back(task);
}
/**
* Removes the indicated AsyncTask from the collection. Returns true if the
* task was removed, false if it was not a member of the collection.
*/
bool AsyncTaskCollection::
remove_task(AsyncTask *task) {
size_t task_index = (size_t)-1;
for (size_t i = 0; i < _tasks.size(); ++i) {
if (_tasks[i] == task) {
task_index = i;
break;
}
}
if (task_index == (size_t)-1) {
// The indicated task was not a member of the collection.
return false;
}
// If the pointer to our internal array is shared by any other
// AsyncTaskCollections, we have to copy the array now so we won't
// inadvertently modify any of our brethren AsyncTaskCollection objects.
if (_tasks.get_ref_count() > 1) {
AsyncTasks old_tasks = _tasks;
_tasks = AsyncTasks::empty_array(0);
_tasks.v() = old_tasks.v();
}
_tasks.erase(_tasks.begin() + task_index);
return true;
}
/**
* Adds all the AsyncTasks indicated in the other collection to this task.
* The other tasks are simply appended to the end of the tasks in this list;
* duplicates are not automatically removed.
*/
void AsyncTaskCollection::
add_tasks_from(const AsyncTaskCollection &other) {
int other_num_tasks = other.get_num_tasks();
for (int i = 0; i < other_num_tasks; i++) {
add_task(other.get_task(i));
}
}
/**
* Removes from this collection all of the AsyncTasks listed in the other
* collection.
*/
void AsyncTaskCollection::
remove_tasks_from(const AsyncTaskCollection &other) {
AsyncTasks new_tasks;
int num_tasks = get_num_tasks();
for (int i = 0; i < num_tasks; i++) {
PT(AsyncTask) task = get_task(i);
if (!other.has_task(task)) {
new_tasks.push_back(task);
}
}
_tasks = new_tasks;
}
/**
* Removes any duplicate entries of the same AsyncTasks on this collection.
* If a AsyncTask appears multiple times, the first appearance is retained;
* subsequent appearances are removed.
*/
void AsyncTaskCollection::
remove_duplicate_tasks() {
AsyncTasks new_tasks;
size_t num_tasks = get_num_tasks();
for (size_t i = 0; i < num_tasks; i++) {
PT(AsyncTask) task = get_task(i);
bool duplicated = false;
for (size_t j = 0; j < i && !duplicated; j++) {
duplicated = (task == get_task(j));
}
if (!duplicated) {
new_tasks.push_back(task);
}
}
_tasks = new_tasks;
}
/**
* Returns true if the indicated AsyncTask appears in this collection, false
* otherwise.
*/
bool AsyncTaskCollection::
has_task(AsyncTask *task) const {
for (size_t i = 0; i < get_num_tasks(); i++) {
if (task == get_task(i)) {
return true;
}
}
return false;
}
/**
* Removes all AsyncTasks from the collection.
*/
void AsyncTaskCollection::
clear() {
_tasks.clear();
}
/**
* Returns the task in the collection with the indicated name, if any, or NULL
* if no task has that name.
*/
AsyncTask *AsyncTaskCollection::
find_task(const std::string &name) const {
size_t num_tasks = get_num_tasks();
for (size_t i = 0; i < num_tasks; ++i) {
AsyncTask *task = get_task(i);
if (task->get_name() == name) {
return task;
}
}
return nullptr;
}
/**
* Returns the number of AsyncTasks in the collection.
*/
size_t AsyncTaskCollection::
get_num_tasks() const {
return _tasks.size();
}
/**
* Returns the nth AsyncTask in the collection.
*/
AsyncTask *AsyncTaskCollection::
get_task(size_t index) const {
nassertr(index < _tasks.size(), nullptr);
return _tasks[index];
}
/**
* Removes the nth AsyncTask from the collection.
*/
void AsyncTaskCollection::
remove_task(size_t index) {
// If the pointer to our internal array is shared by any other
// AsyncTaskCollections, we have to copy the array now so we won't
// inadvertently modify any of our brethren AsyncTaskCollection objects.
if (_tasks.get_ref_count() > 1) {
AsyncTasks old_tasks = _tasks;
_tasks = AsyncTasks::empty_array(0);
_tasks.v() = old_tasks.v();
}
nassertv(index < _tasks.size());
_tasks.erase(_tasks.begin() + index);
}
/**
* Returns the nth AsyncTask in the collection. This is the same as
* get_task(), but it may be a more convenient way to access it.
*/
AsyncTask *AsyncTaskCollection::
operator [] (size_t index) const {
nassertr(index < _tasks.size(), nullptr);
return _tasks[index];
}
/**
* Returns the number of tasks in the collection. This is the same thing as
* get_num_tasks().
*/
size_t AsyncTaskCollection::
size() const {
return _tasks.size();
}
/**
* Writes a brief one-line description of the AsyncTaskCollection to the
* indicated output stream.
*/
void AsyncTaskCollection::
output(std::ostream &out) const {
if (get_num_tasks() == 1) {
out << "1 AsyncTask";
} else {
out << get_num_tasks() << " AsyncTasks";
}
}
/**
* Writes a complete multi-line description of the AsyncTaskCollection to the
* indicated output stream.
*/
void AsyncTaskCollection::
write(std::ostream &out, int indent_level) const {
for (size_t i = 0; i < get_num_tasks(); i++) {
indent(out, indent_level) << *get_task(i) << "\n";
}
}