Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | Related Pages

eventqueue.h

00001 /*
00002   libwftk - Worldforge Toolkit - a widget library
00003   Copyright (C) 2002 Malcolm Walker <malcolm@worldforge.org>
00004   Based on code copyright  (C) 1999-2002  Karsten Laux 
00005 
00006   This library is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU Lesser General Public
00008   License as published by the Free Software Foundation; either
00009   version 2.1 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   Lesser General Public License for more details.
00015   
00016   You should have received a copy of the GNU Lesser General Public
00017   License along with this library; if not, write to the
00018   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019   Boston, MA  02111-1307, SA.
00020 */
00021 
00022 #ifndef EVENT_QUEUE_H
00023 #define EVENT_QUEUE_H
00024 
00025 #include <cassert>
00026 #include <queue>
00027 
00028 #include <sigc++/object.h>
00029 #if SIGC_MAJOR_VERSION == 1 && SIGC_MINOR_VERSION == 0
00030 #include <sigc++/signal_system.h>
00031 #else
00032 #include <sigc++/signal.h>
00033 #endif
00034 #include <sigc++/object_slot.h>
00035 
00036 #include "timer.h"
00037 
00038 namespace wftk {
00039 
00040 template<class C = SigC::Slot0<bool> >
00041 class EventQueue : public SigC::Object
00042 {
00043  public:
00045   EventQueue(unsigned iterations = 1);
00047   virtual ~EventQueue();
00048 
00050   void push(const C&);
00051 
00052  protected:
00054 
00058   virtual bool handle(C& c) {return c();}
00060   virtual void cleanup(C&) {}
00061 
00062  private:
00063   void act();
00064 
00065   wftk::Timer event_pump_; // zero duration timer, always called if running
00066   std::queue<C> queue_;
00067   unsigned iterations_;
00068 };
00069 
00070 } // namespace
00071 
00072 template<class C>
00073 wftk::EventQueue<C>::EventQueue(unsigned iterations) :
00074     event_pump_(0, false), iterations_(iterations)
00075 {
00076   assert(iterations);
00077   event_pump_.alarm.connect(SigC::slot(*this, &EventQueue::act));
00078 }
00079 
00080 template<class C>
00081 wftk::EventQueue<C>::~EventQueue()
00082 {
00083   while(!queue_.empty()) {
00084     cleanup(queue_.front());
00085     queue_.pop();
00086   }
00087 }
00088 
00089 template<class C>
00090 void
00091 wftk::EventQueue<C>::push(const C& c)
00092 {
00093   queue_.push(c);
00094   event_pump_.run();
00095 }
00096 
00097 template<class C>
00098 void
00099 wftk::EventQueue<C>::act()
00100 {
00101   unsigned i = 0;
00102 
00103   while(!queue_.empty() && i++ < iterations_)
00104     if(handle(queue_.front()))
00105       queue_.pop();
00106 
00107   if(queue_.empty())
00108     event_pump_.halt();
00109 }
00110 
00111 #endif // EVENT_QUEUE_H

Generated Tue Aug 9 18:40:26 2005.
Copyright © 1998-2003 by the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.