Panda3D

Loading...
Searching...
No Matches
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
43PUBLISHED:
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
76 typedef pset<EventFunction *> Functions;
77 typedef pmap<std::string, Functions> Hooks;
78 typedef std::pair<EventCallbackFunction*, void*> CallbackFunction;
79 typedef pset<CallbackFunction> CallbackFunctions;
80 typedef pmap<std::string, CallbackFunctions> CallbackHooks;
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() {
101 TypedObject::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
asyncFuture.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
AsyncFuture
This class represents a thread-safe handle to a promised future result of an asynchronous operation,...
Definition asyncFuture.h:61
EventHandler::process_events
void process_events()
The main processing loop of the EventHandler.
Definition eventHandler.cxx:58
EventHandler::remove_hooks_with
bool remove_hooks_with(void *data)
Removes all CallbackFunction hooks that have the indicated pointer as the associated data pointer.
Definition eventHandler.cxx:317
EventHandler::dispatch_event
virtual void dispatch_event(const Event *event)
Calls the hooks assigned to the indicated single event.
Definition eventHandler.cxx:68
EventHandler::remove_hooks
bool remove_hooks(const std::string &event_name)
Removes all functions from the named event hook.
Definition eventHandler.cxx:289
EventHandler::remove_all_hooks
void remove_all_hooks()
Removes all hooks assigned to all events.
Definition eventHandler.cxx:344
EventHandler::get_future
AsyncFuture * get_future(const std::string &event_name)
Returns a pending future that will be marked as done when the event is next fired.
Definition eventHandler.cxx:37
EventHandler::remove_hook
bool remove_hook(const std::string &event_name, EventFunction *function)
Removes the indicated function from the named event hook.
Definition eventHandler.cxx:264
EventHandler::has_hook
bool has_hook(const std::string &event_name) const
Returns true if there is any hook added on the indicated event name, false otherwise.
Definition eventHandler.cxx:197
EventHandler::add_hook
bool add_hook(const std::string &event_name, EventFunction *function)
Adds the indicated function to the list of those that will be called when the named event is thrown.
Definition eventHandler.cxx:166
EventHandler::get_global_event_handler
static EventHandler * get_global_event_handler(EventQueue *queue=nullptr)
Returns a pointer to the one global EventHandler object.
Definition eventHandler.I:19
EventQueue
A queue of pending events.
Definition eventQueue.h:29
Event
A named event, possibly with parameters.
Definition event.h:33
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
TypedObject::init_type
static void init_type()
This function is declared non-inline to work around a compiler bug in g++ 2.96.
Definition typedObject.cxx:44
eventHandler.I
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
event.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pandabase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pmap.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pset.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pt_Event.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
register_type
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