Panda3D
|
00001 // Filename: pythonTask.h 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 #ifndef PYTHONTASK_H 00016 #define PYTHONTASK_H 00017 00018 #include "pandabase.h" 00019 00020 #include "asyncTask.h" 00021 00022 #ifdef HAVE_PYTHON 00023 //////////////////////////////////////////////////////////////////// 00024 // Class : PythonTask 00025 // Description : This class exists to allow association of a Python 00026 // function with the AsyncTaskManager. 00027 //////////////////////////////////////////////////////////////////// 00028 class EXPCL_PANDA_PIPELINE PythonTask : public AsyncTask { 00029 PUBLISHED: 00030 PythonTask(PyObject *function = Py_None, const string &name = string()); 00031 virtual ~PythonTask(); 00032 ALLOC_DELETED_CHAIN(PythonTask); 00033 00034 void set_function(PyObject *function); 00035 PyObject *get_function(); 00036 00037 void set_args(PyObject *args, bool append_task); 00038 PyObject *get_args(); 00039 00040 void set_upon_death(PyObject *upon_death); 00041 PyObject *get_upon_death(); 00042 00043 void set_owner(PyObject *owner); 00044 PyObject *get_owner(); 00045 00046 int __setattr__(const string &attr_name, PyObject *v); 00047 int __setattr__(const string &attr_name); 00048 PyObject *__getattr__(const string &attr_name) const; 00049 00050 protected: 00051 virtual bool is_runnable(); 00052 virtual DoneStatus do_task(); 00053 DoneStatus do_python_task(); 00054 virtual void upon_birth(AsyncTaskManager *manager); 00055 virtual void upon_death(AsyncTaskManager *manager, bool clean_exit); 00056 00057 private: 00058 void register_to_owner(); 00059 void unregister_from_owner(); 00060 void call_owner_method(const char *method_name); 00061 void call_function(PyObject *function); 00062 00063 private: 00064 PyObject *_function; 00065 PyObject *_args; 00066 bool _append_task; 00067 PyObject *_upon_death; 00068 PyObject *_owner; 00069 bool _registered_to_owner; 00070 PyObject *_dict; 00071 00072 PyObject *_generator; 00073 00074 public: 00075 static TypeHandle get_class_type() { 00076 return _type_handle; 00077 } 00078 static void init_type() { 00079 AsyncTask::init_type(); 00080 register_type(_type_handle, "PythonTask", 00081 AsyncTask::get_class_type()); 00082 } 00083 virtual TypeHandle get_type() const { 00084 return get_class_type(); 00085 } 00086 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00087 00088 private: 00089 static TypeHandle _type_handle; 00090 }; 00091 00092 #include "pythonTask.I" 00093 00094 #endif // HAVE_PYTHON 00095 00096 #endif 00097