timer.h

Go to the documentation of this file.
00001 /*
00002  * timer.h
00003  *
00004  * Real time down counting time interval class.
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
00025  * All Rights Reserved.
00026  *
00027  * Contributor(s): ______________________________________.
00028  *
00029  * $Revision: 20385 $
00030  * $Author: rjongbloed $
00031  * $Date: 2008-06-04 10:40:38 +0000 (Wed, 04 Jun 2008) $
00032  */
00033 
00034 #ifndef _PTIMER
00035 #define _PTIMER
00036 
00037 #ifdef P_USE_PRAGMA
00038 #pragma interface
00039 #endif
00040 
00041 class PThread;
00042 
00043 #include <ptlib/notifier.h>
00044 
00073 class PTimer : public PTimeInterval
00074 {
00075   PCLASSINFO(PTimer, PTimeInterval);
00076 
00077   public:
00078     typedef unsigned IDType;
00079 
00087     PTimer(
00088       long milliseconds = 0,  
00089       int seconds = 0,        
00090       int minutes = 0,        
00091       int hours = 0,          
00092       int days = 0            
00093     );
00094     PTimer(
00095       const PTimeInterval & time    
00096     );
00097 
00104     PTimer & operator=(
00105       DWORD milliseconds            
00106     );
00107     PTimer & operator=(
00108       const PTimeInterval & time    
00109     );
00110 
00114     virtual ~PTimer();
00116 
00130     virtual void SetInterval(
00131       PInt64 milliseconds = 0,  
00132       long seconds = 0,         
00133       long minutes = 0,         
00134       long hours = 0,           
00135       int days = 0              
00136     );
00137 
00142     void RunContinuous(
00143       const PTimeInterval & time    // New time interval for timer.
00144     );
00145 
00158     void Stop(
00159       bool wait = true  
00160     );
00161 
00168     PBoolean IsRunning() const;
00169 
00174     void Pause();
00175 
00180     void Resume();
00181 
00187     PBoolean IsPaused() const;
00188 
00191     void Reset();
00192 
00195     const PTimeInterval & GetResetTime() const;
00197 
00212     virtual void OnTimeout();
00213 
00220     const PNotifier & GetNotifier() const;
00221 
00225     void SetNotifier(
00226       const PNotifier & func  // New notifier function for the timer.
00227     );
00229 
00244     static PTimeInterval Tick();
00245 
00254     static unsigned Resolution();
00256 
00257     IDType GetTimerId() const { return timerId; }
00258 
00259   private:
00260     void Construct();
00261 
00262     /* Start or restart the timer from the #resetTime# variable.
00263        This is an internal function.
00264      */
00265     void StartRunning(
00266       PBoolean once   // Flag for one shot or continuous.
00267     );
00268 
00269     /* Process the timer decrementing it by the delta amount and calling the
00270        #OnTimeout()# when zero. This is used internally by the
00271        #PTimerList::Process()# function.
00272      */
00273     void Process(
00274       const PTimeInterval & delta,    // Time interval since last call.
00275       PTimeInterval & minTimeLeft     // Minimum time left till next timeout.
00276     );
00277 
00278   // Member variables
00279 
00280     // Callback function for expired timers.
00281     PNotifier callback;
00282 
00283     // The time to reset a timer to when RunContinuous() is called.
00284     PTimeInterval resetTime;
00285 
00286     // Timer operates once then stops.
00287     PBoolean oneshot;
00288 
00289     // Timer state.
00290     enum { Stopped, Starting, Running, Paused } state;
00291 
00292     friend class PTimerList;              // needed for Process
00293     class PTimerList * timerList;  
00294 
00295     IDType timerId;
00296 
00297 // Include platform dependent part of class
00298 #ifdef _WIN32
00299 #include "msos/ptlib/timer.h"
00300 #else
00301 #include "unix/ptlib/timer.h"
00302 #endif
00303 };
00304 
00305 #endif
00306 
00307 
00308 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Feb 23 01:57:54 2009 for PTLib by  doxygen 1.5.1