Panda3D
asyncTaskCollection.cxx
1 // Filename: asyncTaskCollection.cxx
2 // Created by: drose (16Sep08)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "asyncTaskCollection.h"
16 #include "indent.h"
17 
18 ////////////////////////////////////////////////////////////////////
19 // Function: AsyncTaskCollection::Constructor
20 // Access: Published
21 // Description:
22 ////////////////////////////////////////////////////////////////////
23 AsyncTaskCollection::
24 AsyncTaskCollection() {
25 }
26 
27 ////////////////////////////////////////////////////////////////////
28 // Function: AsyncTaskCollection::Copy Constructor
29 // Access: Published
30 // Description:
31 ////////////////////////////////////////////////////////////////////
32 AsyncTaskCollection::
33 AsyncTaskCollection(const AsyncTaskCollection &copy) :
34  _tasks(copy._tasks)
35 {
36 }
37 
38 ////////////////////////////////////////////////////////////////////
39 // Function: AsyncTaskCollection::Copy Assignment Operator
40 // Access: Published
41 // Description:
42 ////////////////////////////////////////////////////////////////////
43 void AsyncTaskCollection::
44 operator = (const AsyncTaskCollection &copy) {
45  _tasks = copy._tasks;
46 }
47 
48 ////////////////////////////////////////////////////////////////////
49 // Function: AsyncTaskCollection::add_task
50 // Access: Published
51 // Description: Adds a new AsyncTask to the collection.
52 ////////////////////////////////////////////////////////////////////
55  // If the pointer to our internal array is shared by any other
56  // AsyncTaskCollections, we have to copy the array now so we won't
57  // inadvertently modify any of our brethren AsyncTaskCollection
58  // objects.
59  nassertv(task != (AsyncTask *)NULL);
60 
61  if (_tasks.get_ref_count() > 1) {
62  AsyncTasks old_tasks = _tasks;
63  _tasks = AsyncTasks::empty_array(0);
64  _tasks.v() = old_tasks.v();
65  }
66 
67  _tasks.push_back(task);
68 }
69 
70 ////////////////////////////////////////////////////////////////////
71 // Function: AsyncTaskCollection::remove_task
72 // Access: Published
73 // Description: Removes the indicated AsyncTask from the collection.
74 // Returns true if the task was removed, false if it was
75 // not a member of the collection.
76 ////////////////////////////////////////////////////////////////////
79  int task_index = -1;
80  for (int i = 0; task_index == -1 && i < (int)_tasks.size(); i++) {
81  if (_tasks[i] == task) {
82  task_index = i;
83  }
84  }
85 
86  if (task_index == -1) {
87  // The indicated task was not a member of the collection.
88  return false;
89  }
90 
91  // If the pointer to our internal array is shared by any other
92  // AsyncTaskCollections, we have to copy the array now so we won't
93  // inadvertently modify any of our brethren AsyncTaskCollection
94  // objects.
95 
96  if (_tasks.get_ref_count() > 1) {
97  AsyncTasks old_tasks = _tasks;
98  _tasks = AsyncTasks::empty_array(0);
99  _tasks.v() = old_tasks.v();
100  }
101 
102  _tasks.erase(_tasks.begin() + task_index);
103  return true;
104 }
105 
106 ////////////////////////////////////////////////////////////////////
107 // Function: AsyncTaskCollection::add_tasks_from
108 // Access: Published
109 // Description: Adds all the AsyncTasks indicated in the other
110 // collection to this task. The other tasks are simply
111 // appended to the end of the tasks in this list;
112 // duplicates are not automatically removed.
113 ////////////////////////////////////////////////////////////////////
116  int other_num_tasks = other.get_num_tasks();
117  for (int i = 0; i < other_num_tasks; i++) {
118  add_task(other.get_task(i));
119  }
120 }
121 
122 
123 ////////////////////////////////////////////////////////////////////
124 // Function: AsyncTaskCollection::remove_tasks_from
125 // Access: Published
126 // Description: Removes from this collection all of the AsyncTasks
127 // listed in the other collection.
128 ////////////////////////////////////////////////////////////////////
131  AsyncTasks new_tasks;
132  int num_tasks = get_num_tasks();
133  for (int i = 0; i < num_tasks; i++) {
134  PT(AsyncTask) task = get_task(i);
135  if (!other.has_task(task)) {
136  new_tasks.push_back(task);
137  }
138  }
139  _tasks = new_tasks;
140 }
141 
142 ////////////////////////////////////////////////////////////////////
143 // Function: AsyncTaskCollection::remove_duplicate_tasks
144 // Access: Published
145 // Description: Removes any duplicate entries of the same AsyncTasks
146 // on this collection. If a AsyncTask appears multiple
147 // times, the first appearance is retained; subsequent
148 // appearances are removed.
149 ////////////////////////////////////////////////////////////////////
152  AsyncTasks new_tasks;
153 
154  int num_tasks = get_num_tasks();
155  for (int i = 0; i < num_tasks; i++) {
156  PT(AsyncTask) task = get_task(i);
157  bool duplicated = false;
158 
159  for (int j = 0; j < i && !duplicated; j++) {
160  duplicated = (task == get_task(j));
161  }
162 
163  if (!duplicated) {
164  new_tasks.push_back(task);
165  }
166  }
167 
168  _tasks = new_tasks;
169 }
170 
171 ////////////////////////////////////////////////////////////////////
172 // Function: AsyncTaskCollection::has_task
173 // Access: Published
174 // Description: Returns true if the indicated AsyncTask appears in
175 // this collection, false otherwise.
176 ////////////////////////////////////////////////////////////////////
178 has_task(AsyncTask *task) const {
179  for (int i = 0; i < get_num_tasks(); i++) {
180  if (task == get_task(i)) {
181  return true;
182  }
183  }
184  return false;
185 }
186 
187 ////////////////////////////////////////////////////////////////////
188 // Function: AsyncTaskCollection::clear
189 // Access: Published
190 // Description: Removes all AsyncTasks from the collection.
191 ////////////////////////////////////////////////////////////////////
193 clear() {
194  _tasks.clear();
195 }
196 
197 ////////////////////////////////////////////////////////////////////
198 // Function: AsyncTaskCollection::find_task
199 // Access: Published
200 // Description: Returns the task in the collection with the
201 // indicated name, if any, or NULL if no task has
202 // that name.
203 ////////////////////////////////////////////////////////////////////
205 find_task(const string &name) const {
206  int num_tasks = get_num_tasks();
207  for (int i = 0; i < num_tasks; i++) {
208  AsyncTask *task = get_task(i);
209  if (task->get_name() == name) {
210  return task;
211  }
212  }
213  return NULL;
214 }
215 
216 ////////////////////////////////////////////////////////////////////
217 // Function: AsyncTaskCollection::get_num_tasks
218 // Access: Published
219 // Description: Returns the number of AsyncTasks in the collection.
220 ////////////////////////////////////////////////////////////////////
222 get_num_tasks() const {
223  return _tasks.size();
224 }
225 
226 ////////////////////////////////////////////////////////////////////
227 // Function: AsyncTaskCollection::get_task
228 // Access: Published
229 // Description: Returns the nth AsyncTask in the collection.
230 ////////////////////////////////////////////////////////////////////
232 get_task(int index) const {
233  nassertr(index >= 0 && index < (int)_tasks.size(), NULL);
234 
235  return _tasks[index];
236 }
237 
238 ////////////////////////////////////////////////////////////////////
239 // Function: AsyncTaskCollection::remove_task
240 // Access: Published
241 // Description: Removes the nth AsyncTask from the collection.
242 ////////////////////////////////////////////////////////////////////
244 remove_task(int index) {
245  // If the pointer to our internal array is shared by any other
246  // AsyncTaskCollections, we have to copy the array now so we won't
247  // inadvertently modify any of our brethren AsyncTaskCollection
248  // objects.
249 
250  if (_tasks.get_ref_count() > 1) {
251  AsyncTasks old_tasks = _tasks;
252  _tasks = AsyncTasks::empty_array(0);
253  _tasks.v() = old_tasks.v();
254  }
255 
256  nassertv(index >= 0 && index < (int)_tasks.size());
257  _tasks.erase(_tasks.begin() + index);
258 }
259 
260 ////////////////////////////////////////////////////////////////////
261 // Function: AsyncTaskCollection::operator []
262 // Access: Published
263 // Description: Returns the nth AsyncTask in the collection. This is
264 // the same as get_task(), but it may be a more
265 // convenient way to access it.
266 ////////////////////////////////////////////////////////////////////
268 operator [] (int index) const {
269  nassertr(index >= 0 && index < (int)_tasks.size(), NULL);
270 
271  return _tasks[index];
272 }
273 
274 ////////////////////////////////////////////////////////////////////
275 // Function: AsyncTaskCollection::size
276 // Access: Published
277 // Description: Returns the number of tasks in the collection. This
278 // is the same thing as get_num_tasks().
279 ////////////////////////////////////////////////////////////////////
281 size() const {
282  return _tasks.size();
283 }
284 
285 ////////////////////////////////////////////////////////////////////
286 // Function: AsyncTaskCollection::output
287 // Access: Published
288 // Description: Writes a brief one-line description of the
289 // AsyncTaskCollection to the indicated output stream.
290 ////////////////////////////////////////////////////////////////////
292 output(ostream &out) const {
293  if (get_num_tasks() == 1) {
294  out << "1 AsyncTask";
295  } else {
296  out << get_num_tasks() << " AsyncTasks";
297  }
298 }
299 
300 ////////////////////////////////////////////////////////////////////
301 // Function: AsyncTaskCollection::write
302 // Access: Published
303 // Description: Writes a complete multi-line description of the
304 // AsyncTaskCollection to the indicated output stream.
305 ////////////////////////////////////////////////////////////////////
307 write(ostream &out, int indent_level) const {
308  for (int i = 0; i < get_num_tasks(); i++) {
309  indent(out, indent_level) << *get_task(i) << "\n";
310  }
311 }
void clear()
Removes all AsyncTasks from the collection.
void add_task(AsyncTask *task)
Adds a new AsyncTask to the collection.
A list of tasks, for instance as returned by some of the AsyncTaskManager query functions.
AsyncTask * find_task(const string &name) const
Returns the task in the collection with the indicated name, if any, or NULL if no task has that name...
bool has_task(AsyncTask *task) const
Returns true if the indicated AsyncTask appears in this collection, false otherwise.
AsyncTask * get_task(int index) const
Returns the nth AsyncTask in the collection.
AsyncTask * operator[](int index) const
Returns the nth AsyncTask in the collection.
void write(ostream &out, int indent_level=0) const
Writes a complete multi-line description of the AsyncTaskCollection to the indicated output stream...
void add_tasks_from(const AsyncTaskCollection &other)
Adds all the AsyncTasks indicated in the other collection to this task.
void output(ostream &out) const
Writes a brief one-line description of the AsyncTaskCollection to the indicated output stream...
void remove_tasks_from(const AsyncTaskCollection &other)
Removes from this collection all of the AsyncTasks listed in the other collection.
int get_num_tasks() const
Returns the number of AsyncTasks in the collection.
void remove_duplicate_tasks()
Removes any duplicate entries of the same AsyncTasks on this collection.
This class represents a concrete task performed by an AsyncManager.
Definition: asyncTask.h:43
bool remove_task(AsyncTask *task)
Removes the indicated AsyncTask from the collection.
int size() const
Returns the number of tasks in the collection.