Panda3D
 All Classes Functions Variables Enumerations
asyncTask.I
00001 // Filename: asyncTask.I
00002 // Created by:  drose (23Aug06)
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 
00016 ////////////////////////////////////////////////////////////////////
00017 //     Function: AsyncTask::get_state
00018 //       Access: Published
00019 //  Description: Returns the current state of the task.
00020 ////////////////////////////////////////////////////////////////////
00021 INLINE AsyncTask::State AsyncTask::
00022 get_state() const {
00023   return _state;
00024 }
00025 
00026 ////////////////////////////////////////////////////////////////////
00027 //     Function: AsyncTask::is_alive
00028 //       Access: Published
00029 //  Description: Returns true if the task is currently active or
00030 //               sleeping on some task chain, meaning that it will be
00031 //               executed in its turn, or false if it is not active.
00032 //               If the task has recently been removed while it is in
00033 //               the middle of execution, this will return false,
00034 //               because the task will not run again once it finishes.
00035 ////////////////////////////////////////////////////////////////////
00036 INLINE bool AsyncTask::
00037 is_alive() const {
00038   switch (_state) {
00039   case S_active:
00040   case S_servicing:
00041   case S_sleeping:
00042   case S_active_nested:
00043     return true;
00044 
00045   case S_inactive:
00046   case S_servicing_removed:
00047     return false;
00048   }
00049 
00050   // Shouldn't get here.
00051   return false;
00052 }
00053 
00054 ////////////////////////////////////////////////////////////////////
00055 //     Function: AsyncTask::get_manager
00056 //       Access: Published
00057 //  Description: Returns the AsyncTaskManager that this task is active
00058 //               on.  This will be NULL if the state is S_inactive.
00059 ////////////////////////////////////////////////////////////////////
00060 INLINE AsyncTaskManager *AsyncTask::
00061 get_manager() const {
00062   return _manager;
00063 }
00064 
00065 ////////////////////////////////////////////////////////////////////
00066 //     Function: AsyncTask::set_delay
00067 //       Access: Published
00068 //  Description: Specifies the amount of time, in seconds, by which
00069 //               this task will be delayed after it has been added to
00070 //               the AsyncTaskManager.  At least the specified amount
00071 //               of time (and possibly more) will elapse before the
00072 //               task begins.
00073 //
00074 //               You may specify a delay of 0.0 to guarantee that the
00075 //               task will run in the next epoch following the one in
00076 //               which it is added.
00077 //
00078 //               Setting this value after the task has already been
00079 //               added will not affect the task's wake time; it will
00080 //               only affect the task if it is re-added to the queue
00081 //               in the future, for instance if the task returns
00082 //               DS_again.  However, see recalc_wake_time() if you wish
00083 //               to apply the delay effect immediately.
00084 ////////////////////////////////////////////////////////////////////
00085 INLINE void AsyncTask::
00086 set_delay(double delay) {
00087   _delay = delay;
00088   _has_delay = true;
00089 }
00090 
00091 ////////////////////////////////////////////////////////////////////
00092 //     Function: AsyncTask::clear_delay
00093 //       Access: Published
00094 //  Description: Removes any delay specified for the task.  The next
00095 //               time the task is added to the queue, it will run
00096 //               immediately.  This does not affect the task's wake
00097 //               time if it has already been added to the queue.
00098 ////////////////////////////////////////////////////////////////////
00099 INLINE void AsyncTask::
00100 clear_delay() {
00101   _delay = 0.0;
00102   _has_delay = false;
00103 }
00104 
00105 ////////////////////////////////////////////////////////////////////
00106 //     Function: AsyncTask::has_delay
00107 //       Access: Published
00108 //  Description: Returns true if a delay has been set for this task
00109 //               via set_delay(), or false otherwise.
00110 ////////////////////////////////////////////////////////////////////
00111 INLINE bool AsyncTask::
00112 has_delay() const {
00113   return _has_delay;
00114 }
00115 
00116 ////////////////////////////////////////////////////////////////////
00117 //     Function: AsyncTask::get_delay
00118 //       Access: Published
00119 //  Description: Returns the delay value that has been set via
00120 //               set_delay, if any.
00121 ////////////////////////////////////////////////////////////////////
00122 INLINE double AsyncTask::
00123 get_delay() const {
00124   return _delay;
00125 }
00126 
00127 ////////////////////////////////////////////////////////////////////
00128 //     Function: AsyncTask::get_start_time
00129 //       Access: Published
00130 //  Description: Returns the time at which the task was started,
00131 //               according to the task manager's clock.
00132 //
00133 //               It is only valid to call this if the task's status is
00134 //               not S_inactive.
00135 ////////////////////////////////////////////////////////////////////
00136 INLINE double AsyncTask::
00137 get_start_time() const {
00138   nassertr(_state != S_inactive, 0.0);
00139   return _start_time;
00140 }
00141 
00142 ////////////////////////////////////////////////////////////////////
00143 //     Function: AsyncTask::get_start_frame
00144 //       Access: Published
00145 //  Description: Returns the frame number at which the task was
00146 //               started, according to the task manager's clock.
00147 //
00148 //               It is only valid to call this if the task's status is
00149 //               not S_inactive.
00150 ////////////////////////////////////////////////////////////////////
00151 INLINE int AsyncTask::
00152 get_start_frame() const {
00153   nassertr(_state != S_inactive, 0);
00154   return _start_frame;
00155 }
00156 
00157 ////////////////////////////////////////////////////////////////////
00158 //     Function: AsyncTask::clear_name
00159 //       Access: Public
00160 //  Description: Resets the task's name to empty.
00161 ////////////////////////////////////////////////////////////////////
00162 INLINE void AsyncTask::
00163 clear_name() {
00164   set_name(string());
00165 }
00166 
00167 ////////////////////////////////////////////////////////////////////
00168 //     Function: AsyncTask::get_task_id
00169 //       Access: Public
00170 //  Description: Returns a number guaranteed to be unique for each
00171 //               different AsyncTask object in the universe.
00172 ////////////////////////////////////////////////////////////////////
00173 INLINE AtomicAdjust::Integer AsyncTask::
00174 get_task_id() const {
00175   return _task_id;
00176 }
00177 
00178 ////////////////////////////////////////////////////////////////////
00179 //     Function: AsyncTask::get_task_chain
00180 //       Access: Published
00181 //  Description: Returns the AsyncTaskChain on which this task will
00182 //               be running.  Each task chain runs tasks independently
00183 //               of the others.
00184 ////////////////////////////////////////////////////////////////////
00185 INLINE const string &AsyncTask::
00186 get_task_chain() const {
00187   return _chain_name;
00188 }
00189 
00190 
00191 ////////////////////////////////////////////////////////////////////
00192 //     Function: AsyncTask::get_sort
00193 //       Access: Published
00194 //  Description: Returns the task's current sort value.  See
00195 //               set_sort().
00196 ////////////////////////////////////////////////////////////////////
00197 INLINE int AsyncTask::
00198 get_sort() const {
00199   return _sort;
00200 }
00201 
00202 ////////////////////////////////////////////////////////////////////
00203 //     Function: AsyncTask::get_priority
00204 //       Access: Published
00205 //  Description: Returns the task's current priority value.  See
00206 //               set_priority().
00207 ////////////////////////////////////////////////////////////////////
00208 INLINE int AsyncTask::
00209 get_priority() const {
00210   return _priority;
00211 }
00212 
00213 ////////////////////////////////////////////////////////////////////
00214 //     Function: AsyncTask::set_done_event
00215 //       Access: Published
00216 //  Description: Sets the event name that will be triggered
00217 //               when the task finishes.  This should only be called
00218 //               before the task has been started, or after it has
00219 //               finished and before it is about to be restarted
00220 //               (i.e. when get_state() returns S_inactive).
00221 ////////////////////////////////////////////////////////////////////
00222 INLINE void AsyncTask::
00223 set_done_event(const string &done_event) {
00224   nassertv(_state == S_inactive);
00225   _done_event = done_event;
00226 }
00227 
00228 ////////////////////////////////////////////////////////////////////
00229 //     Function: AsyncTask::get_done_event
00230 //       Access: Published
00231 //  Description: Returns the event name that will be triggered
00232 //               when the task finishes.  See set_done_event().
00233 ////////////////////////////////////////////////////////////////////
00234 INLINE const string &AsyncTask::
00235 get_done_event() const {
00236   return _done_event;
00237 }
00238 
00239 #ifdef HAVE_PYTHON
00240 ////////////////////////////////////////////////////////////////////
00241 //     Function: AsyncTask::set_python_object
00242 //       Access: Published
00243 //  Description: Specifies an arbitrary Python object that will be
00244 //               piggybacked on the task object.
00245 ////////////////////////////////////////////////////////////////////
00246 INLINE void AsyncTask::
00247 set_python_object(PyObject *python_object) {
00248   Py_XINCREF(python_object);
00249   Py_XDECREF(_python_object);
00250   _python_object = python_object;
00251 }
00252 #endif  // HAVE_PYTHON
00253 
00254 #ifdef HAVE_PYTHON
00255 ////////////////////////////////////////////////////////////////////
00256 //     Function: AsyncTask::get_python_object
00257 //       Access: Published
00258 //  Description: Returns the Python object that was specified to
00259 //               set_python_object(), if any, or None if no object was
00260 //               specified.
00261 ////////////////////////////////////////////////////////////////////
00262 INLINE PyObject *AsyncTask::
00263 get_python_object() const {
00264   if (_python_object != (PyObject *)NULL) {
00265     Py_XINCREF(_python_object);
00266     return _python_object;
00267   }
00268   Py_INCREF(Py_None);
00269   return Py_None;
00270 }
00271 #endif  // HAVE_PYTHON
00272 
00273 ////////////////////////////////////////////////////////////////////
00274 //     Function: AsyncTask::get_dt
00275 //       Access: Published
00276 //  Description: Returns the amount of time elapsed during the task's
00277 //               previous run cycle, in seconds.
00278 ////////////////////////////////////////////////////////////////////
00279 INLINE double AsyncTask::
00280 get_dt() const {
00281   return _dt;
00282 }
00283 
00284 ////////////////////////////////////////////////////////////////////
00285 //     Function: AsyncTask::get_max_dt
00286 //       Access: Published
00287 //  Description: Returns the maximum amount of time elapsed during any
00288 //               one of the task's previous run cycles, in seconds.
00289 ////////////////////////////////////////////////////////////////////
00290 INLINE double AsyncTask::
00291 get_max_dt() const {
00292   return _max_dt;
00293 }
00294 
00295 ////////////////////////////////////////////////////////////////////
00296 //     Function: AsyncTask::get_average_dt
00297 //       Access: Published
00298 //  Description: Returns the average amount of time elapsed during
00299 //               each of the task's previous run cycles, in seconds.
00300 ////////////////////////////////////////////////////////////////////
00301 INLINE double AsyncTask::
00302 get_average_dt() const {
00303   if (_num_frames == 0) {
00304     return 0.0;
00305   } else {
00306     return _total_dt / _num_frames;
00307   }
00308 }
 All Classes Functions Variables Enumerations