Panda3D
pythonTask.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file pythonTask.h
10  * @author drose
11  * @date 2008-09-16
12  */
13 
14 #ifndef PYTHONTASK_H
15 #define PYTHONTASK_H
16 
17 #include "pandabase.h"
18 
19 #include "asyncTask.h"
20 
21 #ifdef HAVE_PYTHON
22 #include "py_panda.h"
23 #include "extension.h"
24 
25 /**
26  * This class exists to allow association of a Python function or coroutine
27  * with the AsyncTaskManager.
28  */
29 class PythonTask final : public AsyncTask {
30 PUBLISHED:
31  PythonTask(PyObject *function = Py_None, const std::string &name = std::string());
32  virtual ~PythonTask();
33  ALLOC_DELETED_CHAIN(PythonTask);
34 
35  void set_function(PyObject *function);
36  INLINE PyObject *get_function();
37 
38  void set_args(PyObject *args, bool append_task);
39  PyObject *get_args();
40 
41  void set_upon_death(PyObject *upon_death);
42  INLINE PyObject *get_upon_death();
43 
44  void set_owner(PyObject *owner);
45  INLINE PyObject *get_owner() const;
46 
47  INLINE void set_result(PyObject *result);
48 
49 public:
50  // This is exposed only for the result() function in asyncFuture_ext.cxx
51  // to use, which is why it is not published.
52  PyObject *get_result() const;
53  //PyObject *exception() const;
54 
55 PUBLISHED:
56  int __setattr__(PyObject *self, PyObject *attr, PyObject *v);
57  int __delattr__(PyObject *self, PyObject *attr);
58  PyObject *__getattr__(PyObject *attr) const;
59 
60  int __traverse__(visitproc visit, void *arg);
61  int __clear__();
62 
63 PUBLISHED:
64  // The amount of seconds that have elapsed since the task was started,
65  // according to the task manager's clock.
66  MAKE_PROPERTY(time, get_elapsed_time);
67 
68  // If this task has been added to an AsyncTaskManager with a delay in
69  // effect, this contains the time at which the task is expected to awaken.
70  // It has no meaning of the task has not yet been added to a queue, or if
71  // there was no delay in effect at the time the task was added. If the
72  // task's status is not S_sleeping, this contains 0.0.
73  MAKE_PROPERTY(wake_time, get_wake_time);
74 
75  // Alias of wake_time.
76  MAKE_PROPERTY(wakeTime, get_wake_time);
77 
78  // The delay value that has been set on this task, if any, or None.
79  MAKE_PROPERTY2(delay_time, has_delay, get_delay, set_delay, clear_delay);
80 
81  // Alias of delay_time.
82  MAKE_PROPERTY2(delayTime, has_delay, get_delay, set_delay, clear_delay);
83 
84  // The number of frames that have elapsed since the task was started,
85  // according to the task manager's clock.
86  MAKE_PROPERTY(frame, get_elapsed_frames);
87 
88  // This is a special variable to hold the instance dictionary in which
89  // custom variables may be stored.
90  PyObject *__dict__;
91 
92 protected:
93  virtual bool is_runnable();
94  virtual DoneStatus do_task();
95  DoneStatus do_python_task();
96  virtual void upon_birth(AsyncTaskManager *manager);
97  virtual void upon_death(AsyncTaskManager *manager, bool clean_exit);
98 
99 private:
100  void register_to_owner();
101  void unregister_from_owner();
102  void call_owner_method(const char *method_name);
103  void call_function(PyObject *function);
104 
105 private:
106  PyObject *_function;
107  PyObject *_args;
108  PyObject *_upon_death;
109  PyObject *_owner;
110 
111  PyObject *_exception;
112  PyObject *_exc_value;
113  PyObject *_exc_traceback;
114 
115  PyObject *_generator;
116  PyObject *_future_done;
117 
118  bool _append_task;
119  bool _ignore_return;
120  bool _registered_to_owner;
121  mutable bool _retrieved_exception;
122 
123  friend class Extension<AsyncFuture>;
124 
125 public:
126  static TypeHandle get_class_type() {
127  return _type_handle;
128  }
129  static void init_type() {
130  AsyncTask::init_type();
131  register_type(_type_handle, "PythonTask",
132  AsyncTask::get_class_type());
133  }
134  virtual TypeHandle get_type() const {
135  return get_class_type();
136  }
137  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
138 
139 private:
140  static TypeHandle _type_handle;
141 };
142 
143 #include "pythonTask.I"
144 
145 #endif // HAVE_PYTHON
146 
147 #endif
This class represents a thread-safe handle to a promised future result of an asynchronous operation,...
Definition: asyncFuture.h:61
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A class to manage a loose queue of isolated tasks, which can be performed either synchronously (in th...
void register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypedObject * get_result() const
Returns this future's result.
Definition: asyncFuture.I:65
void set_result(std::nullptr_t)
Sets this future's result.
Definition: asyncFuture.I:92
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The default class template does not define any methods.
Definition: extension.h:34
This class represents a concrete task performed by an AsyncManager.
Definition: asyncTask.h:32
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81