Google

Main Page   Class Hierarchy   Compound List   File List   Compound Members  

cseventq.h

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

Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000