ucommon/timers.h

Go to the documentation of this file.
00001 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
00002 //
00003 // This file is part of GNU uCommon C++.
00004 //
00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published
00007 // by the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // GNU uCommon C++ is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 #ifndef _UCOMMON_TIMERS_H_
00028 #define _UCOMMON_TIMERS_H_
00029 
00030 #ifndef _UCOMMON_LINKED_H_
00031 #include <ucommon/linked.h>
00032 #endif
00033 
00034 #ifndef _MSWINDOWS_
00035 #include <unistd.h>
00036 #include <sys/time.h>
00037 #endif
00038 
00039 #include <time.h>
00040 
00041 NAMESPACE_UCOMMON
00042 
00049 class __EXPORT Timer
00050 {
00051 private:
00052     friend class Conditional;
00053     friend class Semaphore;
00054     friend class Event;
00055 
00056 #if _POSIX_TIMERS > 0 && defined(POSIX_TIMERS)
00057     timespec timer;
00058 #else
00059 #undef  POSIX_TIMERS    // make sure not used if no support
00060     timeval timer;
00061 #endif
00062     bool updated;
00063 
00064 protected:
00069     bool update(void);
00070 
00075     bool is_active(void);
00076 
00077 public:
00078 #if _MSC_VER > 1400        // windows broken dll linkage issue...
00079     static const timeout_t inf = ((timeout_t)(-1));
00080     static const time_t reset = ((time_t)(0));
00081 #else
00082     static const timeout_t inf; 
00083     static const time_t reset;  
00084 #endif
00085 
00086 #ifdef  _MSWINDOWS_
00087     typedef unsigned __int64 tick_t;
00088 #else
00089     typedef uint64_t tick_t;
00090 #endif
00091 
00095     Timer();
00096 
00101     Timer(timeout_t offset);
00102 
00107     Timer(time_t offset);
00108 
00113     Timer(const Timer& copy);
00114 
00119     void set(timeout_t expire);
00120 
00125     void set(time_t expire);
00126 
00130     void set(void);
00131 
00135     void clear(void);
00136 
00141     timeout_t get(void) const;
00142 
00147     inline timeout_t operator*() const
00148         {return get();};
00149 
00154     bool operator!() const;
00155 
00160     operator bool() const;
00161 
00166     Timer& operator=(time_t expire);
00167 
00172     Timer& operator=(timeout_t expire);
00173 
00178     Timer& operator+=(time_t expire);
00179 
00184     Timer& operator+=(timeout_t expire);
00185 
00190     Timer& operator-=(time_t expire);
00191 
00196     Timer& operator-=(timeout_t expire);
00197 
00203     timeout_t operator-(const Timer& timer);
00204 
00210     bool operator==(const Timer& timer) const;
00211 
00217     bool operator!=(const Timer& timer) const;
00218 
00224     bool operator<(const Timer& timer) const;
00225 
00231     bool operator<=(const Timer& timer) const;
00232 
00238     bool operator>(const Timer& timer) const;
00239 
00245     bool operator>=(const Timer& timer) const;
00246 
00251     static void sync(Timer &timer);
00252 
00257     static tick_t ticks(void);
00258 };
00259 
00270 class __EXPORT TimerQueue : public OrderedIndex
00271 {
00272 public:
00281     class __EXPORT event : protected Timer, public LinkedList
00282     {
00283     protected:
00284         friend class TimerQueue;
00285 
00290         event(timeout_t expire);
00291 
00297         event(TimerQueue *queue, timeout_t expire);
00298 
00302         virtual void expired(void) = 0;
00303 
00309         virtual timeout_t timeout(void);
00310 
00311     public:
00315         virtual ~event();
00316 
00322         void attach(TimerQueue *queue);
00323 
00327         void detach(void);
00328 
00333         void arm(timeout_t timeout);
00334 
00338         void disarm(void);
00339 
00344         inline timeout_t get(void) const
00345             {return Timer::get();};
00346 
00350         void update(void);
00351 
00356         inline TimerQueue *list(void)
00357             {return static_cast<TimerQueue*>(Root);};
00358     };
00359 
00360 protected:
00361     friend class event;
00362 
00367     virtual void modify(void) = 0;
00368 
00374     virtual void update(void) = 0;
00375 
00376 public:
00380     TimerQueue();
00381 
00385     virtual ~TimerQueue();
00386 
00391     void operator+=(event &timer);
00392 
00397     void operator-=(event &timer);
00398 
00406     timeout_t expire();
00407 };
00408 
00412 typedef TimerQueue::event TQEvent;
00413 
00417 typedef Timer timer_t;
00418 
00419 END_NAMESPACE
00420 
00421 extern "C" {
00422 #if defined(WIN32)
00423     __EXPORT int gettimeofday(struct timeval *tv, void *tz);
00424 #endif
00425 }
00426 
00427 #endif

Generated on 14 Aug 2013 for UCommon by  doxygen 1.4.7