Panda3D
eventHandler.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 eventHandler.h
10  * @author drose
11  * @date 1999-02-08
12  */
13 
14 #ifndef EVENTHANDLER_H
15 #define EVENTHANDLER_H
16 
17 #include "pandabase.h"
18 
19 #include "event.h"
20 #include "pt_Event.h"
21 #include "asyncFuture.h"
22 
23 #include "pset.h"
24 #include "pmap.h"
25 
26 class EventQueue;
27 
28 /**
29  * A class to monitor events from the C++ side of things. It maintains a set
30  * of "hooks", function pointers assigned to event names, and calls the
31  * appropriate hooks when the matching event is detected.
32  *
33  * This class is not necessary when the hooks are detected and processed
34  * entirely by the scripting language, e.g. via Scheme hooks or the messenger
35  * in Python.
36  */
37 class EXPCL_PANDA_EVENT EventHandler : public TypedObject {
38 public:
39  // Define a function type suitable for receiving events.
40  typedef void EventFunction(const Event *);
41  typedef void EventCallbackFunction(const Event *, void *);
42 
43 PUBLISHED:
44  explicit EventHandler(EventQueue *ev_queue);
45  ~EventHandler() {}
46 
47  AsyncFuture *get_future(const std::string &event_name);
48 
49  void process_events();
50 
51  virtual void dispatch_event(const Event *event);
52 
53  void write(std::ostream &out) const;
54 
55  INLINE static EventHandler *get_global_event_handler(EventQueue *queue = nullptr);
56 
57 public:
58  bool add_hook(const std::string &event_name, EventFunction *function);
59  bool add_hook(const std::string &event_name, EventCallbackFunction *function,
60  void *data);
61  bool has_hook(const std::string &event_name) const;
62  bool has_hook(const std::string &event_name, EventFunction *function) const;
63  bool has_hook(const std::string &event_name, EventCallbackFunction *function,
64  void *data) const;
65  bool remove_hook(const std::string &event_name, EventFunction *function);
66  bool remove_hook(const std::string &event_name, EventCallbackFunction *function,
67  void *data);
68 
69  bool remove_hooks(const std::string &event_name);
70  bool remove_hooks_with(void *data);
71 
72  void remove_all_hooks();
73 
74 protected:
75 
78  typedef std::pair<EventCallbackFunction*, void*> CallbackFunction;
81  typedef pmap<std::string, PT(AsyncFuture)> Futures;
82 
83  Hooks _hooks;
84  CallbackHooks _cbhooks;
85  Futures _futures;
86  EventQueue &_queue;
87 
88  static EventHandler *_global_event_handler;
89  static void make_global_event_handler();
90 
91 private:
92  void write_hook(std::ostream &out, const Hooks::value_type &hook) const;
93  void write_cbhook(std::ostream &out, const CallbackHooks::value_type &hook) const;
94 
95 
96 public:
97  static TypeHandle get_class_type() {
98  return _type_handle;
99  }
100  static void init_type() {
102  register_type(_type_handle, "EventHandler",
103  TypedObject::get_class_type());
104  }
105  virtual TypeHandle get_type() const {
106  return get_class_type();
107  }
108  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
109 
110 private:
111  static TypeHandle _type_handle;
112 };
113 
114 #include "eventHandler.I"
115 
116 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static void init_type()
This function is declared non-inline to work around a compiler bug in g++ 2.96.
Definition: typedObject.cxx:44
This class represents a thread-safe handle to a promised future result of an asynchronous operation,...
Definition: asyncFuture.h:61
A class to monitor events from the C++ side of things.
Definition: eventHandler.h:37
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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 an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition: typedObject.h:88
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A queue of pending events.
Definition: eventQueue.h:29
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A named event, possibly with parameters.
Definition: event.h:33
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL set.
Definition: pset.h:49
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81