Panda3D
pythonCallbackObject.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 pythonCallbackObject.h
10  * @author drose
11  * @date 2009-03-13
12  */
13 
14 #ifndef PYTHONCALLBACKOBJECT_H
15 #define PYTHONCALLBACKOBJECT_H
16 
17 #include "pandabase.h"
18 
19 #ifdef HAVE_PYTHON
20 
21 #include "py_panda.h"
22 #include "callbackObject.h"
23 
24 /**
25  * This is a specialization on CallbackObject to allow a callback to directly
26  * call an arbitarary Python function. Powerful! But use with caution.
27  */
28 class PythonCallbackObject : public CallbackObject {
29 PUBLISHED:
30  PythonCallbackObject(PyObject *function = Py_None);
31  virtual ~PythonCallbackObject();
32  ALLOC_DELETED_CHAIN(PythonCallbackObject);
33 
34  void set_function(PyObject *function);
35  PyObject *get_function();
36 
37  MAKE_PROPERTY(function, get_function, set_function);
38 
39 public:
40  virtual void do_callback(CallbackData *cbdata);
41 
42 private:
43  void do_python_callback(CallbackData *cbdata);
44 
45  PyObject *_function;
46 
47 public:
48  static TypeHandle get_class_type() {
49  return _type_handle;
50  }
51  static void init_type() {
52  CallbackObject::init_type();
53  register_type(_type_handle, "PythonCallbackObject",
54  CallbackObject::get_class_type());
55  }
56  virtual TypeHandle get_type() const {
57  return get_class_type();
58  }
59  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
60 
61 private:
62  static TypeHandle _type_handle;
63 };
64 
65 #include "pythonCallbackObject.I"
66 
67 #endif // HAVE_PYTHON
68 
69 #endif
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.
This is a generic data block that is passed along to a CallbackObject when a callback is made.
Definition: callbackData.h:29
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual void do_callback(CallbackData *cbdata)
This method called when the callback is triggered; it *replaces* the original function.
This is a generic object that can be assigned to a callback at various points in the rendering proces...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.