![]() |
Public API Reference |
00001 /* 00002 Crystal Space 3D engine: Event Queue interface 00003 Copyright (C) 1998 by Andrew Zabolotny <bit@freya.etu.ru> 00004 Copyright (C) 2001 by Eric Sunshine <sunshine@sunshineco.com> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public 00017 License along with this library; if not, write to the Free 00018 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00019 */ 00020 00021 #ifndef __CS_CSEVENTQ_H__ 00022 #define __CS_CSEVENTQ_H__ 00023 00028 #include "csutil/csevent.h" 00029 #include "csutil/csevcord.h" 00030 #include "csutil/evoutlet.h" 00031 #include "csutil/garray.h" 00032 #include "csutil/refarr.h" 00033 #include "iutil/eventq.h" 00034 00035 struct iObjectRegistry; 00036 00041 #define DEF_EVENT_QUEUE_LENGTH 256 00042 00051 class csEventQueue : public iEventQueue 00052 { 00053 friend class csEventOutlet; 00054 friend class csPoolEvent; 00055 00056 private: 00057 struct Listener 00058 { 00059 iEventHandler* object; 00060 unsigned int trigger; 00061 }; 00062 typedef csDirtyAccessArray<Listener> ListenerVector; 00063 00064 int EventCordsFind (int cat, int subcat); 00065 00066 // Shared-object registry 00067 iObjectRegistry* Registry; 00068 // The queue itself 00069 volatile iEvent** EventQueue; 00070 // Queue head and tail pointers 00071 volatile size_t evqHead, evqTail; 00072 // The maximum queue length 00073 volatile size_t Length; 00074 // Protection against multiple threads accessing same event queue 00075 volatile int SpinLock; 00076 // Protection against delete while looping through the listeners. 00077 int busy_looping; 00078 /* 00079 * If a delete happened while busy_looping is true we set the 00080 * following to true. 00081 */ 00082 bool delete_occured; 00083 // Registered listeners. 00084 ListenerVector Listeners; 00085 // Array of allocated event outlets. 00086 csArray<csEventOutlet*> EventOutlets; 00087 // Array of allocated event cords. 00088 csRefArray<csEventCord> EventCords; 00089 // Pool of event objects 00090 csPoolEvent *EventPool; 00091 00092 // Enlarge the queue size. 00093 void Resize (size_t iLength); 00094 // Lock the queue for modifications: NESTED CALLS TO LOCK/UNLOCK NOT ALLOWED! 00095 inline void Lock () { while (SpinLock) {} SpinLock++; } 00096 // Unlock the queue 00097 inline void Unlock () { SpinLock--; } 00098 // Find a particular listener index; return -1 if listener is not registered. 00099 int FindListener (iEventHandler*) const; 00100 // Notify listeners of CSMASK_Nothing. 00101 void Notify (iEvent&); 00102 00103 /* 00104 * Start a loop. The purpose of this function is to protect 00105 * against modifications to the Listeners array while this array 00106 * is being processed. 00107 */ 00108 void StartLoop (); 00109 // End a loop. 00110 void EndLoop (); 00111 00112 public: 00113 SCF_DECLARE_IBASE; 00114 00116 csEventQueue (iObjectRegistry*, size_t iLength = DEF_EVENT_QUEUE_LENGTH); 00118 virtual ~csEventQueue (); 00119 00121 virtual void Process (); 00123 virtual void Dispatch (iEvent&); 00124 00126 virtual void RegisterListener (iEventHandler*, unsigned int trigger); 00128 virtual void RemoveListener (iEventHandler*); 00133 virtual void RemoveAllListeners (); 00135 virtual void ChangeListenerTrigger (iEventHandler*, unsigned int trigger); 00136 00138 virtual csPtr<iEventOutlet> CreateEventOutlet (iEventPlug*); 00140 virtual iEventOutlet* GetEventOutlet (); 00142 virtual iEventCord* GetEventCord (int Category, int Subcategory); 00143 00145 uint32 CountPool (); 00147 virtual csPtr<iEvent> CreateEvent (uint8 type); 00149 virtual void Post (iEvent*); 00151 virtual csPtr<iEvent> Get (); 00153 virtual void Clear (); 00155 virtual bool IsEmpty () { return evqHead == evqTail; } 00156 }; 00157 00158 #endif // __CS_CSEVENTQ_H__