Panda3D
|
00001 // Filename: asyncTaskCollection.cxx 00002 // Created by: drose (16Sep08) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "asyncTaskCollection.h" 00016 #include "indent.h" 00017 00018 //////////////////////////////////////////////////////////////////// 00019 // Function: AsyncTaskCollection::Constructor 00020 // Access: Published 00021 // Description: 00022 //////////////////////////////////////////////////////////////////// 00023 AsyncTaskCollection:: 00024 AsyncTaskCollection() { 00025 } 00026 00027 //////////////////////////////////////////////////////////////////// 00028 // Function: AsyncTaskCollection::Copy Constructor 00029 // Access: Published 00030 // Description: 00031 //////////////////////////////////////////////////////////////////// 00032 AsyncTaskCollection:: 00033 AsyncTaskCollection(const AsyncTaskCollection ©) : 00034 _tasks(copy._tasks) 00035 { 00036 } 00037 00038 //////////////////////////////////////////////////////////////////// 00039 // Function: AsyncTaskCollection::Copy Assignment Operator 00040 // Access: Published 00041 // Description: 00042 //////////////////////////////////////////////////////////////////// 00043 void AsyncTaskCollection:: 00044 operator = (const AsyncTaskCollection ©) { 00045 _tasks = copy._tasks; 00046 } 00047 00048 //////////////////////////////////////////////////////////////////// 00049 // Function: AsyncTaskCollection::add_task 00050 // Access: Published 00051 // Description: Adds a new AsyncTask to the collection. 00052 //////////////////////////////////////////////////////////////////// 00053 void AsyncTaskCollection:: 00054 add_task(AsyncTask *task) { 00055 // If the pointer to our internal array is shared by any other 00056 // AsyncTaskCollections, we have to copy the array now so we won't 00057 // inadvertently modify any of our brethren AsyncTaskCollection 00058 // objects. 00059 nassertv(task != (AsyncTask *)NULL); 00060 00061 if (_tasks.get_ref_count() > 1) { 00062 AsyncTasks old_tasks = _tasks; 00063 _tasks = AsyncTasks::empty_array(0); 00064 _tasks.v() = old_tasks.v(); 00065 } 00066 00067 _tasks.push_back(task); 00068 } 00069 00070 //////////////////////////////////////////////////////////////////// 00071 // Function: AsyncTaskCollection::remove_task 00072 // Access: Published 00073 // Description: Removes the indicated AsyncTask from the collection. 00074 // Returns true if the task was removed, false if it was 00075 // not a member of the collection. 00076 //////////////////////////////////////////////////////////////////// 00077 bool AsyncTaskCollection:: 00078 remove_task(AsyncTask *task) { 00079 int task_index = -1; 00080 for (int i = 0; task_index == -1 && i < (int)_tasks.size(); i++) { 00081 if (_tasks[i] == task) { 00082 task_index = i; 00083 } 00084 } 00085 00086 if (task_index == -1) { 00087 // The indicated task was not a member of the collection. 00088 return false; 00089 } 00090 00091 // If the pointer to our internal array is shared by any other 00092 // AsyncTaskCollections, we have to copy the array now so we won't 00093 // inadvertently modify any of our brethren AsyncTaskCollection 00094 // objects. 00095 00096 if (_tasks.get_ref_count() > 1) { 00097 AsyncTasks old_tasks = _tasks; 00098 _tasks = AsyncTasks::empty_array(0); 00099 _tasks.v() = old_tasks.v(); 00100 } 00101 00102 _tasks.erase(_tasks.begin() + task_index); 00103 return true; 00104 } 00105 00106 //////////////////////////////////////////////////////////////////// 00107 // Function: AsyncTaskCollection::add_tasks_from 00108 // Access: Published 00109 // Description: Adds all the AsyncTasks indicated in the other 00110 // collection to this task. The other tasks are simply 00111 // appended to the end of the tasks in this list; 00112 // duplicates are not automatically removed. 00113 //////////////////////////////////////////////////////////////////// 00114 void AsyncTaskCollection:: 00115 add_tasks_from(const AsyncTaskCollection &other) { 00116 int other_num_tasks = other.get_num_tasks(); 00117 for (int i = 0; i < other_num_tasks; i++) { 00118 add_task(other.get_task(i)); 00119 } 00120 } 00121 00122 00123 //////////////////////////////////////////////////////////////////// 00124 // Function: AsyncTaskCollection::remove_tasks_from 00125 // Access: Published 00126 // Description: Removes from this collection all of the AsyncTasks 00127 // listed in the other collection. 00128 //////////////////////////////////////////////////////////////////// 00129 void AsyncTaskCollection:: 00130 remove_tasks_from(const AsyncTaskCollection &other) { 00131 AsyncTasks new_tasks; 00132 int num_tasks = get_num_tasks(); 00133 for (int i = 0; i < num_tasks; i++) { 00134 PT(AsyncTask) task = get_task(i); 00135 if (!other.has_task(task)) { 00136 new_tasks.push_back(task); 00137 } 00138 } 00139 _tasks = new_tasks; 00140 } 00141 00142 //////////////////////////////////////////////////////////////////// 00143 // Function: AsyncTaskCollection::remove_duplicate_tasks 00144 // Access: Published 00145 // Description: Removes any duplicate entries of the same AsyncTasks 00146 // on this collection. If a AsyncTask appears multiple 00147 // times, the first appearance is retained; subsequent 00148 // appearances are removed. 00149 //////////////////////////////////////////////////////////////////// 00150 void AsyncTaskCollection:: 00151 remove_duplicate_tasks() { 00152 AsyncTasks new_tasks; 00153 00154 int num_tasks = get_num_tasks(); 00155 for (int i = 0; i < num_tasks; i++) { 00156 PT(AsyncTask) task = get_task(i); 00157 bool duplicated = false; 00158 00159 for (int j = 0; j < i && !duplicated; j++) { 00160 duplicated = (task == get_task(j)); 00161 } 00162 00163 if (!duplicated) { 00164 new_tasks.push_back(task); 00165 } 00166 } 00167 00168 _tasks = new_tasks; 00169 } 00170 00171 //////////////////////////////////////////////////////////////////// 00172 // Function: AsyncTaskCollection::has_task 00173 // Access: Published 00174 // Description: Returns true if the indicated AsyncTask appears in 00175 // this collection, false otherwise. 00176 //////////////////////////////////////////////////////////////////// 00177 bool AsyncTaskCollection:: 00178 has_task(AsyncTask *task) const { 00179 for (int i = 0; i < get_num_tasks(); i++) { 00180 if (task == get_task(i)) { 00181 return true; 00182 } 00183 } 00184 return false; 00185 } 00186 00187 //////////////////////////////////////////////////////////////////// 00188 // Function: AsyncTaskCollection::clear 00189 // Access: Published 00190 // Description: Removes all AsyncTasks from the collection. 00191 //////////////////////////////////////////////////////////////////// 00192 void AsyncTaskCollection:: 00193 clear() { 00194 _tasks.clear(); 00195 } 00196 00197 //////////////////////////////////////////////////////////////////// 00198 // Function: AsyncTaskCollection::find_task 00199 // Access: Published 00200 // Description: Returns the task in the collection with the 00201 // indicated name, if any, or NULL if no task has 00202 // that name. 00203 //////////////////////////////////////////////////////////////////// 00204 AsyncTask *AsyncTaskCollection:: 00205 find_task(const string &name) const { 00206 int num_tasks = get_num_tasks(); 00207 for (int i = 0; i < num_tasks; i++) { 00208 AsyncTask *task = get_task(i); 00209 if (task->get_name() == name) { 00210 return task; 00211 } 00212 } 00213 return NULL; 00214 } 00215 00216 //////////////////////////////////////////////////////////////////// 00217 // Function: AsyncTaskCollection::get_num_tasks 00218 // Access: Published 00219 // Description: Returns the number of AsyncTasks in the collection. 00220 //////////////////////////////////////////////////////////////////// 00221 int AsyncTaskCollection:: 00222 get_num_tasks() const { 00223 return _tasks.size(); 00224 } 00225 00226 //////////////////////////////////////////////////////////////////// 00227 // Function: AsyncTaskCollection::get_task 00228 // Access: Published 00229 // Description: Returns the nth AsyncTask in the collection. 00230 //////////////////////////////////////////////////////////////////// 00231 AsyncTask *AsyncTaskCollection:: 00232 get_task(int index) const { 00233 nassertr(index >= 0 && index < (int)_tasks.size(), NULL); 00234 00235 return _tasks[index]; 00236 } 00237 00238 //////////////////////////////////////////////////////////////////// 00239 // Function: AsyncTaskCollection::remove_task 00240 // Access: Published 00241 // Description: Removes the nth AsyncTask from the collection. 00242 //////////////////////////////////////////////////////////////////// 00243 void AsyncTaskCollection:: 00244 remove_task(int index) { 00245 // If the pointer to our internal array is shared by any other 00246 // AsyncTaskCollections, we have to copy the array now so we won't 00247 // inadvertently modify any of our brethren AsyncTaskCollection 00248 // objects. 00249 00250 if (_tasks.get_ref_count() > 1) { 00251 AsyncTasks old_tasks = _tasks; 00252 _tasks = AsyncTasks::empty_array(0); 00253 _tasks.v() = old_tasks.v(); 00254 } 00255 00256 nassertv(index >= 0 && index < (int)_tasks.size()); 00257 _tasks.erase(_tasks.begin() + index); 00258 } 00259 00260 //////////////////////////////////////////////////////////////////// 00261 // Function: AsyncTaskCollection::operator [] 00262 // Access: Published 00263 // Description: Returns the nth AsyncTask in the collection. This is 00264 // the same as get_task(), but it may be a more 00265 // convenient way to access it. 00266 //////////////////////////////////////////////////////////////////// 00267 AsyncTask *AsyncTaskCollection:: 00268 operator [] (int index) const { 00269 nassertr(index >= 0 && index < (int)_tasks.size(), NULL); 00270 00271 return _tasks[index]; 00272 } 00273 00274 //////////////////////////////////////////////////////////////////// 00275 // Function: AsyncTaskCollection::size 00276 // Access: Published 00277 // Description: Returns the number of tasks in the collection. This 00278 // is the same thing as get_num_tasks(). 00279 //////////////////////////////////////////////////////////////////// 00280 int AsyncTaskCollection:: 00281 size() const { 00282 return _tasks.size(); 00283 } 00284 00285 //////////////////////////////////////////////////////////////////// 00286 // Function: AsyncTaskCollection::output 00287 // Access: Published 00288 // Description: Writes a brief one-line description of the 00289 // AsyncTaskCollection to the indicated output stream. 00290 //////////////////////////////////////////////////////////////////// 00291 void AsyncTaskCollection:: 00292 output(ostream &out) const { 00293 if (get_num_tasks() == 1) { 00294 out << "1 AsyncTask"; 00295 } else { 00296 out << get_num_tasks() << " AsyncTasks"; 00297 } 00298 } 00299 00300 //////////////////////////////////////////////////////////////////// 00301 // Function: AsyncTaskCollection::write 00302 // Access: Published 00303 // Description: Writes a complete multi-line description of the 00304 // AsyncTaskCollection to the indicated output stream. 00305 //////////////////////////////////////////////////////////////////// 00306 void AsyncTaskCollection:: 00307 write(ostream &out, int indent_level) const { 00308 for (int i = 0; i < get_num_tasks(); i++) { 00309 indent(out, indent_level) << *get_task(i) << "\n"; 00310 } 00311 }