Panda3D
 All Classes Functions Variables Enumerations
pythonTask.h
1 // Filename: pythonTask.h
2 // Created by: drose (16Sep08)
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 #ifndef PYTHONTASK_H
16 #define PYTHONTASK_H
17 
18 #include "pandabase.h"
19 
20 #include "asyncTask.h"
21 
22 #ifdef HAVE_PYTHON
23 #include "py_panda.h"
24 
25 ////////////////////////////////////////////////////////////////////
26 // Class : PythonTask
27 // Description : This class exists to allow association of a Python
28 // function with the AsyncTaskManager.
29 ////////////////////////////////////////////////////////////////////
30 class PythonTask : public AsyncTask {
31 PUBLISHED:
32  PythonTask(PyObject *function = Py_None, const string &name = string());
33  virtual ~PythonTask();
34  ALLOC_DELETED_CHAIN(PythonTask);
35 
36  void set_function(PyObject *function);
37  PyObject *get_function();
38 
39  void set_args(PyObject *args, bool append_task);
40  PyObject *get_args();
41 
42  void set_upon_death(PyObject *upon_death);
43  PyObject *get_upon_death();
44 
45  void set_owner(PyObject *owner);
46  PyObject *get_owner();
47 
48  int __setattr__(PyObject *self, PyObject *attr, PyObject *v);
49  int __delattr__(PyObject *self, PyObject *attr);
50  PyObject *__getattr__(PyObject *attr) const;
51 
52  int __traverse__(visitproc visit, void *arg);
53  int __clear__();
54 
55  INLINE void set_delay(PyObject *delay);
56  INLINE PyObject *get_delay() const;
57 
58 PUBLISHED:
59  // The name of this task.
60  MAKE_PROPERTY(name, get_name, set_name);
61 
62  // The amount of seconds that have elapsed since the task was
63  // started, according to the task manager's clock.
64  MAKE_PROPERTY(time, get_elapsed_time);
65 
66  // If this task has been added to an AsyncTaskManager with a delay
67  // in effect, this contains the time at which the task is expected
68  // to awaken. It has no meaning of the task has not yet been added
69  // to a queue, or if there was no delay in effect at the time the
70  // task was added.
71  //
72  // If the task's status is not S_sleeping, this contains 0.0.
73  MAKE_PROPERTY(wake_time, get_wake_time);
74 
75  // The delay value that has been set on this task, if any, or None.
76  MAKE_PROPERTY(delay_time, get_delay, set_delay);
77 
78  // The number of frames that have elapsed since the task was
79  // started, according to the task manager's clock.
80  MAKE_PROPERTY(frame, get_elapsed_frames);
81 
82  // This is a number guaranteed to be unique for each different
83  // AsyncTask object in the universe.
84  MAKE_PROPERTY(id, get_task_id);
85 
86  // This is a special variable to hold the instance dictionary in
87  // which custom variables may be stored.
88  PyObject *__dict__;
89 
90 protected:
91  virtual bool is_runnable();
92  virtual DoneStatus do_task();
93  DoneStatus do_python_task();
94  virtual void upon_birth(AsyncTaskManager *manager);
95  virtual void upon_death(AsyncTaskManager *manager, bool clean_exit);
96 
97 private:
98  void register_to_owner();
99  void unregister_from_owner();
100  void call_owner_method(const char *method_name);
101  void call_function(PyObject *function);
102 
103 private:
104  PyObject *_function;
105  PyObject *_args;
106  bool _append_task;
107  PyObject *_upon_death;
108  PyObject *_owner;
109  bool _registered_to_owner;
110 
111  PyObject *_generator;
112 
113 public:
114  static TypeHandle get_class_type() {
115  return _type_handle;
116  }
117  static void init_type() {
118  AsyncTask::init_type();
119  register_type(_type_handle, "PythonTask",
120  AsyncTask::get_class_type());
121  }
122  virtual TypeHandle get_type() const {
123  return get_class_type();
124  }
125  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
126 
127 private:
128  static TypeHandle _type_handle;
129 };
130 
131 #include "pythonTask.I"
132 
133 #endif // HAVE_PYTHON
134 
135 #endif
136 
A class to manage a loose queue of isolated tasks, which can be performed either synchronously (in th...
double get_delay() const
Returns the delay value that has been set via set_delay, if any.
Definition: asyncTask.I:123
This class represents a concrete task performed by an AsyncManager.
Definition: asyncTask.h:43
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
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85