Panda3D

genericAsyncTask.cxx

00001 // Filename: genericAsyncTask.cxx
00002 // Created by:  drose (03Oct08)
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 "genericAsyncTask.h"
00016 #include "pnotify.h"
00017 
00018 TypeHandle GenericAsyncTask::_type_handle;
00019 
00020 ////////////////////////////////////////////////////////////////////
00021 //     Function: GenericAsyncTask::Constructor
00022 //       Access: Published
00023 //  Description:
00024 ////////////////////////////////////////////////////////////////////
00025 GenericAsyncTask::
00026 GenericAsyncTask(const string &name) :
00027   AsyncTask(name)
00028 {
00029   _function = NULL;
00030   _upon_birth = NULL;
00031   _upon_death = NULL;
00032   _user_data = NULL;
00033 }
00034 
00035 ////////////////////////////////////////////////////////////////////
00036 //     Function: GenericAsyncTask::Constructor
00037 //       Access: Published
00038 //  Description:
00039 ////////////////////////////////////////////////////////////////////
00040 GenericAsyncTask::
00041 GenericAsyncTask(const string &name, GenericAsyncTask::TaskFunc *function, void *user_data) :
00042   AsyncTask(name),
00043   _function(function),
00044   _user_data(user_data)
00045 {
00046   _upon_birth = NULL;
00047   _upon_death = NULL;
00048 }
00049 
00050 ////////////////////////////////////////////////////////////////////
00051 //     Function: GenericAsyncTask::is_runnable
00052 //       Access: Protected, Virtual
00053 //  Description: Override this function to return true if the task can
00054 //               be successfully executed, false if it cannot.  Mainly
00055 //               intended as a sanity check when attempting to add the
00056 //               task to a task manager.
00057 //
00058 //               This function is called with the lock held.
00059 ////////////////////////////////////////////////////////////////////
00060 bool GenericAsyncTask::
00061 is_runnable() {
00062   return _function != NULL;
00063 }
00064 
00065 ////////////////////////////////////////////////////////////////////
00066 //     Function: GenericAsyncTask::do_task
00067 //       Access: Protected, Virtual
00068 //  Description: Override this function to do something useful for the
00069 //               task.
00070 //
00071 //               This function is called with the lock *not* held.
00072 ////////////////////////////////////////////////////////////////////
00073 AsyncTask::DoneStatus GenericAsyncTask::
00074 do_task() {
00075   nassertr(_function != NULL, DS_interrupt);
00076   return (*_function)(this, _user_data);
00077 }
00078 
00079 ////////////////////////////////////////////////////////////////////
00080 //     Function: GenericAsyncTask::upon_birth
00081 //       Access: Protected, Virtual
00082 //  Description: Override this function to do something useful when the
00083 //               task has been added to the active queue.
00084 //
00085 //               This function is called with the lock *not* held.
00086 ////////////////////////////////////////////////////////////////////
00087 void GenericAsyncTask::
00088 upon_birth(AsyncTaskManager *manager) {
00089   AsyncTask::upon_birth(manager);
00090 
00091   if (_upon_birth != NULL) {
00092     (*_upon_birth)(this, _user_data);
00093   }
00094 }
00095 
00096 ////////////////////////////////////////////////////////////////////
00097 //     Function: GenericAsyncTask::upon_death
00098 //       Access: Protected, Virtual
00099 //  Description: Override this function to do something useful when the
00100 //               task has been removed from the active queue.  The
00101 //               parameter clean_exit is true if the task has been
00102 //               removed because it exited normally (returning
00103 //               DS_done), or false if it was removed for some other
00104 //               reason (e.g. AsyncTaskManager::remove()).  By the
00105 //               time this method is called, _manager has been
00106 //               cleared, so the parameter manager indicates the
00107 //               original AsyncTaskManager that owned this task.
00108 //
00109 //               The normal behavior is to throw the done_event only
00110 //               if clean_exit is true.
00111 //
00112 //               This function is called with the lock *not* held.
00113 ////////////////////////////////////////////////////////////////////
00114 void GenericAsyncTask::
00115 upon_death(AsyncTaskManager *manager, bool clean_exit) {
00116   AsyncTask::upon_death(manager, clean_exit);
00117 
00118   if (_upon_death != NULL) {
00119     (*_upon_death)(this, clean_exit, _user_data);
00120   }
00121 }
 All Classes Functions Variables Enumerations