![]() |
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/csvector.h" 00031 #include "csutil/evoutlet.h" 00032 #include "csutil/garray.h" 00033 #include "iutil/eventq.h" 00034 struct iObjectRegistry; 00035 00040 #define DEF_EVENT_QUEUE_LENGTH 256 00041 00050 class csEventQueue : public iEventQueue 00051 { 00052 friend class csEventOutlet; 00053 friend class csPoolEvent; 00054 private: 00055 struct Listener 00056 { 00057 iEventHandler* object; 00058 unsigned int trigger; 00059 }; 00060 typedef csGrowingArray<Listener> ListenerVector; 00061 00062 /* 00063 * The array of all allocated event outlets. *NOTE* It is not the 00064 * responsibility of this class to free the contained event outlets, thus 00065 * this class does not override FreeItem(). Instead, it is the 00066 * responsibility of the caller of iEventQueue::CreateEventOutlet() to send 00067 * the outlet a DecRef() message (which, incidentally, will result in the 00068 * automatic removal of the outlet from this list if no references to the 00069 * outlet remain). 00070 */ 00071 class EventOutletsVector : public csVector 00072 { 00073 public: 00074 EventOutletsVector() : csVector (16, 16) {} 00075 virtual ~EventOutletsVector () { DeleteAll(); } 00076 csEventOutlet* Get(int i) 00077 { return (csEventOutlet*)csVector::Get(i); } 00078 }; 00079 00080 // The array of all allocated event cords. 00081 class EventCordsVector : public csVector 00082 { 00083 public: 00084 EventCordsVector() : csVector (16, 16) {} 00085 virtual ~EventCordsVector() { DeleteAll(); } 00086 virtual bool FreeItem(void* p) 00087 { ((csEventCord*)p)->DecRef(); return true; } 00088 csEventCord* Get(int i) 00089 { return (csEventCord*)csVector::Get(i); } 00090 int Find(int Category, int SubCategory); 00091 }; 00092 00093 // Shared-object registry 00094 iObjectRegistry* Registry; 00095 // The queue itself 00096 volatile iEvent** EventQueue; 00097 // Queue head and tail pointers 00098 volatile size_t evqHead, evqTail; 00099 // The maximum queue length 00100 volatile size_t Length; 00101 // Protection against multiple threads accessing same event queue 00102 volatile int SpinLock; 00103 // Protection against delete while looping through the listeners. 00104 int busy_looping; 00105 /* 00106 * If a delete happened while busy_looping is true we set the 00107 * following to true. 00108 */ 00109 bool delete_occured; 00110 // Registered listeners. 00111 ListenerVector Listeners; 00112 // Array of allocated event outlets. 00113 EventOutletsVector EventOutlets; 00114 // Array of allocated event cords. 00115 EventCordsVector EventCords; 00116 // Pool of event objects 00117 csPoolEvent *EventPool; 00118 00119 // Enlarge the queue size. 00120 void Resize(size_t iLength); 00121 // Lock the queue for modifications: NESTED CALLS TO LOCK/UNLOCK NOT ALLOWED! 00122 inline void Lock() { while (SpinLock) {} SpinLock++; } 00123 // Unlock the queue 00124 inline void Unlock() { SpinLock--; } 00125 // Find a particular listener index; return -1 if listener is not registered. 00126 int FindListener(iEventHandler*) const; 00127 // Notify listeners of CSMASK_Nothing. 00128 void Notify(iEvent&); 00129 00130 /* 00131 * Start a loop. The purpose of this function is to protect 00132 * against modifications to the Listeners array while this array 00133 * is being processed. 00134 */ 00135 void StartLoop (); 00136 // End a loop. 00137 void EndLoop (); 00138 00139 public: 00140 SCF_DECLARE_IBASE; 00141 00143 csEventQueue(iObjectRegistry*, size_t iLength = DEF_EVENT_QUEUE_LENGTH); 00145 virtual ~csEventQueue(); 00146 00148 virtual void Process(); 00150 virtual void Dispatch(iEvent&); 00151 00153 virtual void RegisterListener(iEventHandler*, unsigned int trigger); 00155 virtual void RemoveListener(iEventHandler*); 00160 virtual void RemoveAllListeners(); 00162 virtual void ChangeListenerTrigger(iEventHandler*, unsigned int trigger); 00163 00165 virtual csPtr<iEventOutlet> CreateEventOutlet(iEventPlug*); 00167 virtual iEventOutlet* GetEventOutlet(); 00169 virtual iEventCord* GetEventCord (int Category, int Subcategory); 00170 00172 uint32 CountPool(); 00174 virtual csPtr<iEvent> CreateEvent(uint8 type); 00176 virtual void Post(iEvent*); 00178 virtual csPtr<iEvent> Get(); 00180 virtual void Clear(); 00182 virtual bool IsEmpty() { return evqHead == evqTail; } 00183 }; 00184 00185 #endif // __CS_CSEVENTQ_H__