Browse Source

add has_hook

David Rose 22 years ago
parent
commit
70d1868ce3
2 changed files with 33 additions and 5 deletions
  1. 29 2
      panda/src/event/eventHandler.cxx
  2. 4 3
      panda/src/event/eventHandler.h

+ 29 - 2
panda/src/event/eventHandler.cxx

@@ -163,10 +163,37 @@ add_hook(const string &event_name, EventFunction *function) {
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 bool EventHandler::
 bool EventHandler::
 add_hook(const string &event_name, EventCallbackFunction *function,
 add_hook(const string &event_name, EventCallbackFunction *function,
-     void *data) {
+         void *data) {
   return _cbhooks[event_name].insert(CallbackFunction(function, data)).second;
   return _cbhooks[event_name].insert(CallbackFunction(function, data)).second;
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: EventHandler::has_hook
+//       Access: Public
+//  Description: Returns true if there is any hook added on the
+//               indicated event name, false otherwise.
+////////////////////////////////////////////////////////////////////
+bool EventHandler::
+has_hook(const string &event_name) const {
+  Hooks::const_iterator hi;
+  hi = _hooks.find(event_name);
+  if (hi != _hooks.end()) {
+    if (!(*hi).second.empty()) {
+      return true;
+    }
+  }
+
+  CallbackHooks::const_iterator chi;
+  chi = _cbhooks.find(event_name);
+  if (chi != _cbhooks.end()) {
+    if (!(*chi).second.empty()) {
+      return true;
+    }
+  }
+
+  return false;
+}
+
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: EventHandler::remove_hook
 //     Function: EventHandler::remove_hook
@@ -191,7 +218,7 @@ remove_hook(const string &event_name, EventFunction *function) {
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 bool EventHandler::
 bool EventHandler::
 remove_hook(const string &event_name, EventCallbackFunction *function,
 remove_hook(const string &event_name, EventCallbackFunction *function,
-        void *data) {
+            void *data) {
   return _cbhooks[event_name].erase(CallbackFunction(function, data)) != 0;
   return _cbhooks[event_name].erase(CallbackFunction(function, data)) != 0;
 }
 }
 
 

+ 4 - 3
panda/src/event/eventHandler.h

@@ -45,7 +45,7 @@ class EXPCL_PANDAEXPRESS EventHandler : public TypedObject {
 public:
 public:
   // Define a function type suitable for receiving events.
   // Define a function type suitable for receiving events.
   typedef void EventFunction(CPT_Event);
   typedef void EventFunction(CPT_Event);
-  typedef void EventCallbackFunction(CPT(Event), void*);
+  typedef void EventCallbackFunction(CPT_Event, void *);
 
 
 PUBLISHED:
 PUBLISHED:
   EventHandler(EventQueue *queue);
   EventHandler(EventQueue *queue);
@@ -59,10 +59,11 @@ PUBLISHED:
 public:
 public:
   bool add_hook(const string &event_name, EventFunction *function);
   bool add_hook(const string &event_name, EventFunction *function);
   bool add_hook(const string &event_name, EventCallbackFunction *function,
   bool add_hook(const string &event_name, EventCallbackFunction *function,
-                void*);
+                void *data);
+  bool has_hook(const string &event_name) const;
   bool remove_hook(const string &event_name, EventFunction *function);
   bool remove_hook(const string &event_name, EventFunction *function);
   bool remove_hook(const string &event_name, EventCallbackFunction *function,
   bool remove_hook(const string &event_name, EventCallbackFunction *function,
-                   void*);
+                   void *data);
 
 
   void remove_all_hooks();
   void remove_all_hooks();