timer.h
Go to the documentation of this file.
/*
* timer.h
*
* Real time down counting time interval class.
*
* Portable Windows Library
*
* Copyright (c) 1993-1998 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Portions are Copyright (C) 1993 Free Software Foundation, Inc.
* All Rights Reserved.
*
* Contributor(s): ______________________________________.
*
* $Revision: 26594 $
* $Author: rjongbloed $
* $Date: 2011-10-13 06:51:30 -0500 (Thu, 13 Oct 2011) $
*/
#ifndef PTLIB_TIMER_H
#define PTLIB_TIMER_H
#ifdef P_USE_PRAGMA
#pragma interface
#endif
class PThread;
#include <ptlib/notifier.h>
class PSimpleTimer : public PTimeInterval
{
PCLASSINFO(PSimpleTimer, PTimeInterval);
public:
PSimpleTimer(
long milliseconds = 0,
int seconds = 0,
int minutes = 0,
int hours = 0,
int days = 0
);
PSimpleTimer(
const PTimeInterval & time
);
PSimpleTimer(
const PSimpleTimer & timer
);
PSimpleTimer & operator=(
DWORD milliseconds
);
PSimpleTimer & operator=(
const PTimeInterval & time
);
PSimpleTimer & operator=(
const PSimpleTimer & timer
);
virtual void SetInterval(
PInt64 milliseconds = 0,
long seconds = 0,
long minutes = 0,
long hours = 0,
int days = 0
);
void Stop();
PTimeInterval GetElapsed() const;
PTimeInterval GetRemaining() const;
bool IsRunning() const;
bool HasExpired() const;
operator bool() const;
protected:
PTimeInterval m_startTick;
};
class PTimer : public PTimeInterval
{
PCLASSINFO(PTimer, PTimeInterval);
public:
typedef unsigned IDType;
PTimer(
long milliseconds = 0,
int seconds = 0,
int minutes = 0,
int hours = 0,
int days = 0
);
PTimer(
const PTimeInterval & time
);
PTimer(
const PTimer & timer
);
PTimer & operator=(
DWORD milliseconds
);
PTimer & operator=(
const PTimeInterval & time
);
PTimer & operator=(
const PTimer & timer
);
virtual ~PTimer();
virtual void SetInterval(
PInt64 milliseconds = 0,
long seconds = 0,
long minutes = 0,
long hours = 0,
int days = 0
);
void RunContinuous(
const PTimeInterval & time // New time interval for timer.
);
void Stop(
bool wait = true
);
PBoolean IsRunning() const;
void Pause();
void Resume();
PBoolean IsPaused() const;
void Reset();
const PTimeInterval & GetResetTime() const;
virtual void OnTimeout();
const PNotifier & GetNotifier() const;
void SetNotifier(
const PNotifier & func // New notifier function for the timer.
);
static PTimeInterval Tick();
static unsigned Resolution();
PInt64 GetMilliSeconds() const;
PInt64 GetAbsoluteTime() const { return m_absoluteTime; }
// Internal functions.
IDType GetTimerId() const { return m_timerId; }
PAtomicInteger::IntegerType GetNextSerialNumber() { return ++m_serialNumber; }
private:
void Construct();
/* Start or restart the timer from the <code>resetTime</code> variable.
This is an internal function.
*/
void StartRunning(
PBoolean once // Flag for one shot or continuous.
);
/* Process the timer decrementing it by the delta amount and calling the
<code>OnTimeout()</code> when zero. This is used internally by the
<code>PTimerList::Process()</code> function.
*/
void Process(
PInt64 now // time consider as "now"
);
// Member variables
// Callback function for expired timers.
PNotifier m_callback;
// The time to reset a timer to when RunContinuous() is called.
PTimeInterval m_resetTime;
// Timer operates once then stops.
PBoolean m_oneshot;
// Timer state.
enum { Stopped, Running, Paused } m_state;
friend class PTimerList; // needed for Process
class PTimerList * m_timerList;
IDType m_timerId;
PAtomicInteger m_serialNumber;
PInt64 m_absoluteTime;
// Include platform dependent part of class
#ifdef _WIN32
#include "msos/ptlib/timer.h"
#else
#include "unix/ptlib/timer.h"
#endif
};
#endif // PTLIB_TIMER_H
// End Of File ///////////////////////////////////////////////////////////////
PTimer::GetNextSerialNumber
PAtomicInteger::IntegerType GetNextSerialNumber()
Definition: timer.h:384
PTimer::operator=
PTimer & operator=(DWORD milliseconds)
Restart the timer in one shot mode using the specified time value.
PSimpleTimer::HasExpired
bool HasExpired() const
Indicate timer has expired.
PTimeInterval
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:55
PNotifier
Class specialisation for PNotifierTemplate<INT>
PSimpleTimer::SetInterval
virtual void SetInterval(PInt64 milliseconds=0, long seconds=0, long minutes=0, long hours=0, int days=0)
Set the value of the time interval.
PTimer::~PTimer
virtual ~PTimer()
Destroy the timer object, removing it from the applications timer list if it was running.
PTimer::Pause
void Pause()
Pause a running timer.
PTimer::OnTimeout
virtual void OnTimeout()
This function is called on time out.
PTimer::SetInterval
virtual void SetInterval(PInt64 milliseconds=0, long seconds=0, long minutes=0, long hours=0, int days=0)
Set the value of the time interval.
PSimpleTimer::Stop
void Stop()
Stop the timer.
PTimer::Stop
void Stop(bool wait=true)
Stop a running timer.
PSimpleTimer::IsRunning
bool IsRunning() const
Indicate timer has not expired.
PTimer::IsRunning
PBoolean IsRunning() const
Determine if the timer is currently running.
PSimpleTimer::m_startTick
PTimeInterval m_startTick
Definition: timer.h:149
PTimer
A class representing a system timer.
Definition: timer.h:181
PBoolean
BOOL PBoolean
Definition: object.h:102
PSimpleTimer::GetElapsed
PTimeInterval GetElapsed() const
Return the real time elapsed since instantiation.
PTimerList
Definition: pprocess.h:106
PTimer::RunContinuous
void RunContinuous(const PTimeInterval &time)
Start a timer in continous cycle mode.
PAtomicInteger
This class implements an integer that can be atomically incremented and decremented in a thread-safe ...
Definition: critsec.h:171
PSimpleTimer::operator=
PSimpleTimer & operator=(DWORD milliseconds)
Restart the timer using the specified time value.
PTimer::PTimer
PTimer(long milliseconds=0, int seconds=0, int minutes=0, int hours=0, int days=0)
Create a new timer object and start it in one shot mode for the specified amount of time...
PTimer::Tick
static PTimeInterval Tick()
Get the number of milliseconds since some arbtrary point in time.
PTimer::IsPaused
PBoolean IsPaused() const
Determine if the timer is currently paused.
PTimer::Resolution
static unsigned Resolution()
Get the smallest number of milliseconds that the timer can be set to.
PTimer::GetTimerId
IDType GetTimerId() const
Definition: timer.h:383
PTimer::GetAbsoluteTime
PInt64 GetAbsoluteTime() const
Return absolute time timer will expire.
Definition: timer.h:379
PSimpleTimer::GetRemaining
PTimeInterval GetRemaining() const
Return the real time remaining before expiry.
PThread
This class defines a thread of execution in the system.
Definition: thread.h:66
PTimer::GetMilliSeconds
PInt64 GetMilliSeconds() const
Return number of milliseconds left in timer.
PSimpleTimer
A class represeting a simple timer.
Definition: timer.h:58
PSimpleTimer::PSimpleTimer
PSimpleTimer(long milliseconds=0, int seconds=0, int minutes=0, int hours=0, int days=0)
Create a new timer object which will be expired the specified time interval after "now" in real time...
PAtomicInteger::IntegerType
PAtomicBase::IntegerType IntegerType
Definition: critsec.h:174
PTimer::GetNotifier
const PNotifier & GetNotifier() const
Get the current call back function that is called whenever the timer expires.
PTimer::Resume
void Resume()
Restart a paused timer continuing at the time it was paused.
PTimer::SetNotifier
void SetNotifier(const PNotifier &func)
Set the call back function that is called whenever the timer expires.
PTimer::Reset
void Reset()
Restart a timer continuing from the time it was initially.
PTimer::GetResetTime
const PTimeInterval & GetResetTime() const
Get the time this timer was set to initially.
notifier.h
PTimer::IDType
unsigned IDType
Definition: timer.h:186