Panda3D
 All Classes Functions Variables Enumerations
asyncTask.I
1 // Filename: asyncTask.I
2 // Created by: drose (23Aug06)
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 
16 ////////////////////////////////////////////////////////////////////
17 // Function: AsyncTask::get_state
18 // Access: Published
19 // Description: Returns the current state of the task.
20 ////////////////////////////////////////////////////////////////////
21 INLINE AsyncTask::State AsyncTask::
22 get_state() const {
23  return _state;
24 }
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: AsyncTask::is_alive
28 // Access: Published
29 // Description: Returns true if the task is currently active or
30 // sleeping on some task chain, meaning that it will be
31 // executed in its turn, or false if it is not active.
32 // If the task has recently been removed while it is in
33 // the middle of execution, this will return false,
34 // because the task will not run again once it finishes.
35 ////////////////////////////////////////////////////////////////////
36 INLINE bool AsyncTask::
37 is_alive() const {
38  switch (_state) {
39  case S_active:
40  case S_servicing:
41  case S_sleeping:
42  case S_active_nested:
43  return true;
44 
45  case S_inactive:
46  case S_servicing_removed:
47  return false;
48  }
49 
50  // Shouldn't get here.
51  return false;
52 }
53 
54 ////////////////////////////////////////////////////////////////////
55 // Function: AsyncTask::get_manager
56 // Access: Published
57 // Description: Returns the AsyncTaskManager that this task is active
58 // on. This will be NULL if the state is S_inactive.
59 ////////////////////////////////////////////////////////////////////
61 get_manager() const {
62  return _manager;
63 }
64 
65 ////////////////////////////////////////////////////////////////////
66 // Function: AsyncTask::set_delay
67 // Access: Published
68 // Description: Specifies the amount of time, in seconds, by which
69 // this task will be delayed after it has been added to
70 // the AsyncTaskManager. At least the specified amount
71 // of time (and possibly more) will elapse before the
72 // task begins.
73 //
74 // You may specify a delay of 0.0 to guarantee that the
75 // task will run in the next epoch following the one in
76 // which it is added.
77 //
78 // Setting this value after the task has already been
79 // added will not affect the task's wake time; it will
80 // only affect the task if it is re-added to the queue
81 // in the future, for instance if the task returns
82 // DS_again. However, see recalc_wake_time() if you wish
83 // to apply the delay effect immediately.
84 ////////////////////////////////////////////////////////////////////
85 INLINE void AsyncTask::
86 set_delay(double delay) {
87  _delay = delay;
88  _has_delay = true;
89 }
90 
91 ////////////////////////////////////////////////////////////////////
92 // Function: AsyncTask::clear_delay
93 // Access: Published
94 // Description: Removes any delay specified for the task. The next
95 // time the task is added to the queue, it will run
96 // immediately. This does not affect the task's wake
97 // time if it has already been added to the queue.
98 ////////////////////////////////////////////////////////////////////
99 INLINE void AsyncTask::
101  _delay = 0.0;
102  _has_delay = false;
103 }
104 
105 ////////////////////////////////////////////////////////////////////
106 // Function: AsyncTask::has_delay
107 // Access: Published
108 // Description: Returns true if a delay has been set for this task
109 // via set_delay(), or false otherwise.
110 ////////////////////////////////////////////////////////////////////
111 INLINE bool AsyncTask::
112 has_delay() const {
113  return _has_delay;
114 }
115 
116 ////////////////////////////////////////////////////////////////////
117 // Function: AsyncTask::get_delay
118 // Access: Published
119 // Description: Returns the delay value that has been set via
120 // set_delay, if any.
121 ////////////////////////////////////////////////////////////////////
122 INLINE double AsyncTask::
123 get_delay() const {
124  return _delay;
125 }
126 
127 ////////////////////////////////////////////////////////////////////
128 // Function: AsyncTask::get_start_time
129 // Access: Published
130 // Description: Returns the time at which the task was started,
131 // according to the task manager's clock.
132 //
133 // It is only valid to call this if the task's status is
134 // not S_inactive.
135 ////////////////////////////////////////////////////////////////////
136 INLINE double AsyncTask::
137 get_start_time() const {
138  nassertr(_state != S_inactive, 0.0);
139  return _start_time;
140 }
141 
142 ////////////////////////////////////////////////////////////////////
143 // Function: AsyncTask::get_start_frame
144 // Access: Published
145 // Description: Returns the frame number at which the task was
146 // started, according to the task manager's clock.
147 //
148 // It is only valid to call this if the task's status is
149 // not S_inactive.
150 ////////////////////////////////////////////////////////////////////
151 INLINE int AsyncTask::
153  nassertr(_state != S_inactive, 0);
154  return _start_frame;
155 }
156 
157 ////////////////////////////////////////////////////////////////////
158 // Function: AsyncTask::clear_name
159 // Access: Public
160 // Description: Resets the task's name to empty.
161 ////////////////////////////////////////////////////////////////////
162 INLINE void AsyncTask::
164  set_name(string());
165 }
166 
167 ////////////////////////////////////////////////////////////////////
168 // Function: AsyncTask::get_task_id
169 // Access: Public
170 // Description: Returns a number guaranteed to be unique for each
171 // different AsyncTask object in the universe.
172 ////////////////////////////////////////////////////////////////////
173 INLINE AtomicAdjust::Integer AsyncTask::
174 get_task_id() const {
175  return _task_id;
176 }
177 
178 ////////////////////////////////////////////////////////////////////
179 // Function: AsyncTask::get_task_chain
180 // Access: Published
181 // Description: Returns the AsyncTaskChain on which this task will
182 // be running. Each task chain runs tasks independently
183 // of the others.
184 ////////////////////////////////////////////////////////////////////
185 INLINE const string &AsyncTask::
186 get_task_chain() const {
187  return _chain_name;
188 }
189 
190 
191 ////////////////////////////////////////////////////////////////////
192 // Function: AsyncTask::get_sort
193 // Access: Published
194 // Description: Returns the task's current sort value. See
195 // set_sort().
196 ////////////////////////////////////////////////////////////////////
197 INLINE int AsyncTask::
198 get_sort() const {
199  return _sort;
200 }
201 
202 ////////////////////////////////////////////////////////////////////
203 // Function: AsyncTask::get_priority
204 // Access: Published
205 // Description: Returns the task's current priority value. See
206 // set_priority().
207 ////////////////////////////////////////////////////////////////////
208 INLINE int AsyncTask::
209 get_priority() const {
210  return _priority;
211 }
212 
213 ////////////////////////////////////////////////////////////////////
214 // Function: AsyncTask::set_done_event
215 // Access: Published
216 // Description: Sets the event name that will be triggered
217 // when the task finishes. This should only be called
218 // before the task has been started, or after it has
219 // finished and before it is about to be restarted
220 // (i.e. when get_state() returns S_inactive).
221 ////////////////////////////////////////////////////////////////////
222 INLINE void AsyncTask::
223 set_done_event(const string &done_event) {
224  nassertv(_state == S_inactive);
225  _done_event = done_event;
226 }
227 
228 ////////////////////////////////////////////////////////////////////
229 // Function: AsyncTask::get_done_event
230 // Access: Published
231 // Description: Returns the event name that will be triggered
232 // when the task finishes. See set_done_event().
233 ////////////////////////////////////////////////////////////////////
234 INLINE const string &AsyncTask::
235 get_done_event() const {
236  return _done_event;
237 }
238 
239 #ifdef HAVE_PYTHON
240 ////////////////////////////////////////////////////////////////////
241 // Function: AsyncTask::set_python_object
242 // Access: Published
243 // Description: Specifies an arbitrary Python object that will be
244 // piggybacked on the task object.
245 ////////////////////////////////////////////////////////////////////
246 INLINE void AsyncTask::
247 set_python_object(PyObject *python_object) {
248  Py_XINCREF(python_object);
249  Py_XDECREF(_python_object);
250  _python_object = python_object;
251 }
252 #endif // HAVE_PYTHON
253 
254 #ifdef HAVE_PYTHON
255 ////////////////////////////////////////////////////////////////////
256 // Function: AsyncTask::get_python_object
257 // Access: Published
258 // Description: Returns the Python object that was specified to
259 // set_python_object(), if any, or None if no object was
260 // specified.
261 ////////////////////////////////////////////////////////////////////
262 INLINE PyObject *AsyncTask::
263 get_python_object() const {
264  if (_python_object != (PyObject *)NULL) {
265  Py_XINCREF(_python_object);
266  return _python_object;
267  }
268  Py_INCREF(Py_None);
269  return Py_None;
270 }
271 #endif // HAVE_PYTHON
272 
273 ////////////////////////////////////////////////////////////////////
274 // Function: AsyncTask::get_dt
275 // Access: Published
276 // Description: Returns the amount of time elapsed during the task's
277 // previous run cycle, in seconds.
278 ////////////////////////////////////////////////////////////////////
279 INLINE double AsyncTask::
280 get_dt() const {
281  return _dt;
282 }
283 
284 ////////////////////////////////////////////////////////////////////
285 // Function: AsyncTask::get_max_dt
286 // Access: Published
287 // Description: Returns the maximum amount of time elapsed during any
288 // one of the task's previous run cycles, in seconds.
289 ////////////////////////////////////////////////////////////////////
290 INLINE double AsyncTask::
291 get_max_dt() const {
292  return _max_dt;
293 }
294 
295 ////////////////////////////////////////////////////////////////////
296 // Function: AsyncTask::get_average_dt
297 // Access: Published
298 // Description: Returns the average amount of time elapsed during
299 // each of the task's previous run cycles, in seconds.
300 ////////////////////////////////////////////////////////////////////
301 INLINE double AsyncTask::
302 get_average_dt() const {
303  if (_num_frames == 0) {
304  return 0.0;
305  } else {
306  return _total_dt / _num_frames;
307  }
308 }
const string & get_task_chain() const
Returns the AsyncTaskChain on which this task will be running.
Definition: asyncTask.I:186
double get_dt() const
Returns the amount of time elapsed during the task's previous run cycle, in seconds.
Definition: asyncTask.I:280
State get_state() const
Returns the current state of the task.
Definition: asyncTask.I:22
int get_priority() const
Returns the task's current priority value.
Definition: asyncTask.I:209
double get_start_time() const
Returns the time at which the task was started, according to the task manager's clock.
Definition: asyncTask.I:137
bool has_delay() const
Returns true if a delay has been set for this task via set_delay(), or false otherwise.
Definition: asyncTask.I:112
A class to manage a loose queue of isolated tasks, which can be performed either synchronously (in th...
AsyncTaskManager * get_manager() const
Returns the AsyncTaskManager that this task is active on.
Definition: asyncTask.I:61
int get_start_frame() const
Returns the frame number at which the task was started, according to the task manager's clock...
Definition: asyncTask.I:152
int get_sort() const
Returns the task's current sort value.
Definition: asyncTask.I:198
void clear_name()
Resets the task's name to empty.
Definition: asyncTask.I:163
double get_delay() const
Returns the delay value that has been set via set_delay, if any.
Definition: asyncTask.I:123
bool is_alive() const
Returns true if the task is currently active or sleeping on some task chain, meaning that it will be ...
Definition: asyncTask.I:37
const string & get_done_event() const
Returns the event name that will be triggered when the task finishes.
Definition: asyncTask.I:235
double get_average_dt() const
Returns the average amount of time elapsed during each of the task's previous run cycles...
Definition: asyncTask.I:302
void clear_delay()
Removes any delay specified for the task.
Definition: asyncTask.I:100
void set_delay(double delay)
Specifies the amount of time, in seconds, by which this task will be delayed after it has been added ...
Definition: asyncTask.I:86
double get_max_dt() const
Returns the maximum amount of time elapsed during any one of the task's previous run cycles...
Definition: asyncTask.I:291
AtomicAdjust::Integer get_task_id() const
Returns a number guaranteed to be unique for each different AsyncTask object in the universe...
Definition: asyncTask.I:174
void set_done_event(const string &done_event)
Sets the event name that will be triggered when the task finishes.
Definition: asyncTask.I:223