csutil/cseventq.h
Go to the documentation of this file.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 "csevent.h" 00029 #include "csevcord.h" 00030 #include "evoutlet.h" 00031 #include "array.h" 00032 #include "ref.h" 00033 #include "refarr.h" 00034 #include "thread.h" 00035 #include "iutil/eventq.h" 00036 00037 struct iObjectRegistry; 00038 00043 #define DEF_EVENT_QUEUE_LENGTH 256 00044 00050 class csEventQueue : public iEventQueue 00051 { 00052 friend class csEventOutlet; 00053 friend class csPoolEvent; 00054 00055 private: 00056 struct Listener 00057 { 00058 iEventHandler* object; 00059 unsigned int trigger; 00060 }; 00061 typedef csArray<Listener> ListenerVector; 00062 00063 int EventCordsFind (int cat, int subcat); 00064 00065 // Shared-object registry 00066 iObjectRegistry* Registry; 00067 // The queue itself 00068 volatile iEvent** EventQueue; 00069 // Queue head and tail pointers 00070 volatile size_t evqHead, evqTail; 00071 // The maximum queue length 00072 volatile size_t Length; 00073 // Protection against multiple threads accessing same event queue 00074 csRef<csMutex> Mutex; 00075 // Protection against delete while looping through the listeners. 00076 int busy_looping; 00077 /* 00078 * If a delete happened while busy_looping is true we set the 00079 * following to true. 00080 */ 00081 bool delete_occured; 00082 // Registered listeners. 00083 ListenerVector Listeners; 00084 // Array of allocated event outlets. 00085 csArray<csEventOutlet*> EventOutlets; 00086 // Array of allocated event cords. 00087 csRefArray<csEventCord> EventCords; 00088 // Pool of event objects 00089 csPoolEvent* EventPool; 00090 00091 // Enlarge the queue size. 00092 void Resize (size_t iLength); 00093 // Lock the queue for modifications: NESTED CALLS TO LOCK/UNLOCK NOT ALLOWED! 00094 inline void Lock () { Mutex->LockWait(); } 00095 // Unlock the queue 00096 inline void Unlock () { Mutex->Release(); } 00097 // Find a particular listener index; return -1 if listener is not registered. 00098 int FindListener (iEventHandler*) const; 00099 // Send listeners of CSMASK_FrameProcess one of the frame processing 00100 // pseudo-events. 00101 void Notify (unsigned int pseudo_event); 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__
Generated for Crystal Space by doxygen 1.2.18