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

timer.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 _TIMER_H_
00023 #define _TIMER_H_
00024 
00025 #include <set>
00026 
00027 #include <sigc++/object.h>
00028 #if SIGC_MAJOR_VERSION == 1 && SIGC_MINOR_VERSION == 0
00029 #include <sigc++/signal_system.h>
00030 #else
00031 #include <sigc++/signal.h>
00032 #endif
00033 
00034 #include <SDL/SDL_types.h>
00035 
00036 #include "application.h"
00037 
00038 namespace wftk {
00039 
00041 class Time
00042 {
00043  public:
00044   Time() : epoch_(0), ticks_(0) {}
00045 
00046   // default copy constructor, destructor, operator= are fine
00047 
00049   static Time now();
00050 
00052   Uint32 ticks() const {return ticks_;}
00053 
00055   struct Info
00056   {
00058     Sint32 days;
00060     Uint8 hours;
00062     Uint8 minutes;
00064     Uint8 seconds;
00066     Uint16 msecs;
00067   };
00068 
00070   Info info() const;
00071 
00073   bool operator==(const Time& t) const
00074     {return epoch_ == t.epoch_ && ticks_ == t.ticks_;}
00076   bool operator!=(const Time& t) const {return !(*this == t);}
00078   bool operator<(const Time& t) const
00079     {return (epoch_ == t.epoch_) ? (ticks_ < t.ticks_) : (epoch_ < t.epoch_);}
00081   bool operator>(const Time& t) const {return t < *this;}
00083   bool operator<=(const Time& t) const {return !(t < *this);}
00085   bool operator>=(const Time& t) const {return !(*this < t);}
00086 
00088   int operator-(const Time& t) const;
00089 
00091   Time& operator+=(Uint32);
00093   Time& operator-=(Uint32);
00095   Time& operator+=(int val) {return (val >= 0)
00096     ? operator+=((Uint32) val) : operator-=(negative_rectify(val));}
00098   Time& operator-=(int val) {return (val >= 0)
00099     ? operator-=((Uint32) val) : operator+=(negative_rectify(val));}
00101   Time operator+(Uint32 val) const {Time out(*this); out += val; return out;}
00103   Time operator-(Uint32 val) const {Time out(*this); out -= val; return out;}
00105   Time operator+(int val) const {Time out(*this); out += val; return out;}
00107   Time operator-(int val) const {Time out(*this); out -= val; return out;}
00108 
00109  private:
00110   Time(Sint32 epoch, Uint32 ticks) : epoch_(epoch), ticks_(ticks) {}
00111 
00112   // returns -val, dealing with type limitations
00113   static unsigned negative_rectify(int val);
00114 
00115   // epoch is 2^32 milliseconds, equal to 49 days, 17 hours, 2 minutes,
00116   // 47 seconds, 296 milliseconds
00117   Sint32 epoch_; // epoch_ < 0 is time before program start
00118   Uint32 ticks_;
00119 };
00120 
00122 inline Time operator+(Uint32 val, const Time& t) {return t + val;}
00124 inline Time operator+(int val, const Time& t) {return t + val;}
00125 
00127 class Timer : virtual public SigC::Object
00128 {
00129  public:
00131   Timer(Uint32 mseconds, bool runnning = true);
00133   ~Timer();
00134 
00135   class Alarm : public SigC::Signal1<void,unsigned int>
00136   {
00137    public:
00138     SigC::Connection connect(const SigC::Slot0<void>&);
00139     SigC::Connection connect(const SigC::Slot1<void,unsigned int>&);
00140   };
00141 
00143   Alarm alarm;
00144 
00146   void setInterval(unsigned int delta);
00148   Uint32 interval() const {return deltaT_;}
00149 
00151   void run();
00153   void halt();
00154 
00156   static void processAllTimers();
00157 
00163   static Uint32 limitWait(Uint32 max);
00164   
00165  private:
00166   // unimplemented
00167   Timer(const Timer&);
00168   Timer& operator=(const Timer&);
00169 
00171   void update(const Time&);
00173   bool running_;
00175   Uint32 deltaT_;
00177   Time nextEvent_;
00178 
00180   static Time needUpdateBy_;
00182   void setNeedUpdateBy(bool force = false);
00184   static std::set<Timer*> runningTimers_;
00185 
00186   typedef Application::FloatingEvent<Timer> BaseEvent;
00187   class Event : public BaseEvent
00188   {
00189    public:
00190     Event(Timer& timer, Uint32 time_passed) :
00191         BaseEvent(timer), timePassed_(time_passed) {}
00192 
00193     virtual void operator()();
00194 
00195    private:
00196     Uint32 timePassed_;
00197   };
00198 
00199 public:
00200   // so we can stop our alarms from being emitted if
00201   // we're destroyed
00202   BaseEvent* currentEvent_;
00203 };
00204 
00205 }
00206 
00207 #endif

Generated Tue Apr 12 22:48:52 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.