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