xref: /aoo41x/main/vos/inc/vos/timer.hxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #ifndef _VOS_TIMER_HXX_
30*cdf0e10cSrcweir #define _VOS_TIMER_HXX_
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #	include <vos/refernce.hxx>
33*cdf0e10cSrcweir #	include <vos/mutex.hxx>
34*cdf0e10cSrcweir #	include <osl/time.h>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir namespace vos
38*cdf0e10cSrcweir {
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////////
41*cdf0e10cSrcweir //
42*cdf0e10cSrcweir // TTimeValue
43*cdf0e10cSrcweir //
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir /** <code> struct TTimeValue </code> : class for times. Times are seconds in UTC since 01.01.1970
46*cdf0e10cSrcweir  */
47*cdf0e10cSrcweir struct TTimeValue : public TimeValue
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir 	TTimeValue()
50*cdf0e10cSrcweir     { Seconds = 0; Nanosec = 0; }
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir     TTimeValue(sal_uInt32 Seconds, sal_uInt32 Nano);
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir     TTimeValue(sal_uInt32 MilliSecs)
55*cdf0e10cSrcweir     { Seconds = MilliSecs / 1000L; Nanosec = (MilliSecs % 1000) * 1000000L; }
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir     TTimeValue(const TTimeValue& rTimeValue)
58*cdf0e10cSrcweir     { Seconds = rTimeValue.Seconds; Nanosec = rTimeValue.Nanosec; }
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir     TTimeValue(const TimeValue& rTimeValue)
61*cdf0e10cSrcweir     { Seconds = rTimeValue.Seconds; Nanosec = rTimeValue.Nanosec; }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 	void		SAL_CALL normalize();
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir     void		SAL_CALL addTime(const TTimeValue& Delta);
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir 	sal_Bool	SAL_CALL isEmpty() const;
68*cdf0e10cSrcweir };
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir inline void TTimeValue::normalize()
71*cdf0e10cSrcweir {
72*cdf0e10cSrcweir 	if (Nanosec > 1000000000)
73*cdf0e10cSrcweir 	{
74*cdf0e10cSrcweir 		Seconds += Nanosec / 1000000000;
75*cdf0e10cSrcweir 		Nanosec %= 1000000000;
76*cdf0e10cSrcweir 	}
77*cdf0e10cSrcweir }
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir inline TTimeValue::TTimeValue(sal_uInt32 Secs, sal_uInt32 Nano)
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir 	Seconds = Secs;
82*cdf0e10cSrcweir 	Nanosec = Nano;
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     normalize();
85*cdf0e10cSrcweir }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir inline void TTimeValue::addTime(const TTimeValue& Time)
88*cdf0e10cSrcweir {
89*cdf0e10cSrcweir 	Seconds += Time.Seconds;
90*cdf0e10cSrcweir 	Nanosec += Time.Nanosec;
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 	normalize();
93*cdf0e10cSrcweir }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir inline sal_Bool TTimeValue::isEmpty() const
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir 	return ((Seconds == 0) && (Nanosec == 0));
98*cdf0e10cSrcweir }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir inline sal_Bool operator<(const TTimeValue& rTimeA, const TTimeValue& rTimeB)
101*cdf0e10cSrcweir {
102*cdf0e10cSrcweir 	if (rTimeA.Seconds < rTimeB.Seconds)
103*cdf0e10cSrcweir 		return sal_True;
104*cdf0e10cSrcweir 	else if (rTimeA.Seconds > rTimeB.Seconds)
105*cdf0e10cSrcweir 		return sal_False;
106*cdf0e10cSrcweir 	else
107*cdf0e10cSrcweir 		return (rTimeA.Nanosec < rTimeB.Nanosec);
108*cdf0e10cSrcweir }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir inline sal_Bool operator>(const TTimeValue& rTimeA, const TTimeValue& rTimeB)
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir 	if (rTimeA.Seconds > rTimeB.Seconds)
113*cdf0e10cSrcweir 		return sal_True;
114*cdf0e10cSrcweir 	else if (rTimeA.Seconds < rTimeB.Seconds)
115*cdf0e10cSrcweir 		return sal_False;
116*cdf0e10cSrcweir 	else
117*cdf0e10cSrcweir 		return (rTimeA.Nanosec > rTimeB.Nanosec);
118*cdf0e10cSrcweir }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir inline sal_Bool operator==(const TTimeValue& rTimeA, const TTimeValue& rTimeB)
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir 	return ((rTimeA.Seconds == rTimeB.Seconds) &&
123*cdf0e10cSrcweir 		    (rTimeA.Nanosec == rTimeB.Nanosec));
124*cdf0e10cSrcweir }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////////
128*cdf0e10cSrcweir //
129*cdf0e10cSrcweir //  Timer class
130*cdf0e10cSrcweir //
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir class OTimerManager;
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir /** <code> class OTimer </code> : Interface for the Timer and handling the event
135*cdf0e10cSrcweir */
136*cdf0e10cSrcweir class OTimer : virtual public OReference , virtual public OObject
137*cdf0e10cSrcweir {
138*cdf0e10cSrcweir 	VOS_DECLARE_CLASSINFO(VOS_NAMESPACE(OTimer, vos));
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir public:
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir     /// constructor
143*cdf0e10cSrcweir   	OTimer();
144*cdf0e10cSrcweir 	/// constructor
145*cdf0e10cSrcweir   	OTimer(const TTimeValue& Time);
146*cdf0e10cSrcweir 	/// constructor
147*cdf0e10cSrcweir   	OTimer(const TTimeValue& Time, const TTimeValue& RepeatTime);
148*cdf0e10cSrcweir   	/// start timer.
149*cdf0e10cSrcweir   	void SAL_CALL start();
150*cdf0e10cSrcweir   	/// abort timer prematurely.
151*cdf0e10cSrcweir   	void SAL_CALL stop();
152*cdf0e10cSrcweir   	/// returns <code> sal_True </code> if timer is running.
153*cdf0e10cSrcweir   	sal_Bool 	SAL_CALL isTicking() const;
154*cdf0e10cSrcweir 	/// is the timer expired?
155*cdf0e10cSrcweir   	sal_Bool 	SAL_CALL isExpired() const;
156*cdf0e10cSrcweir 	/// does <code> pTimer </code> expires before us?
157*cdf0e10cSrcweir   	sal_Bool    SAL_CALL expiresBefore(const OTimer* pTimer) const;
158*cdf0e10cSrcweir 	/// set the absolute time when the timer should fire
159*cdf0e10cSrcweir   	void		SAL_CALL setAbsoluteTime(const TTimeValue& Time);
160*cdf0e10cSrcweir 	/// set the time to fire to 'now' + <code> Remaining </code>
161*cdf0e10cSrcweir   	void		SAL_CALL setRemainingTime(const TTimeValue& Remaining);
162*cdf0e10cSrcweir 	/// set the time to fire to 'now' + <code> Remaining </code> with repeat interveal <code> Repeat </code>
163*cdf0e10cSrcweir   	void		SAL_CALL setRemainingTime(const TTimeValue& Remaining, const TTimeValue& Repeat);
164*cdf0e10cSrcweir 	/// adds <code> Time </code> to the 'fire time'
165*cdf0e10cSrcweir   	void		SAL_CALL addTime(const TTimeValue& Time);
166*cdf0e10cSrcweir 	/// returns the remaining time before timer expiration relative to now
167*cdf0e10cSrcweir   	TTimeValue	SAL_CALL getRemainingTime() const;
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir protected:
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir     /// destructor
172*cdf0e10cSrcweir     virtual ~OTimer();
173*cdf0e10cSrcweir     /// what should be done when the 'timer fires'
174*cdf0e10cSrcweir     virtual void SAL_CALL onShot() = 0;
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     /// holds (initial) exparation time of this timer
177*cdf0e10cSrcweir 	TTimeValue  m_TimeOut;
178*cdf0e10cSrcweir     /// holds the time of exparation of this timer
179*cdf0e10cSrcweir 	TTimeValue  m_Expired;
180*cdf0e10cSrcweir     /// holds the time interveal of successive exparations
181*cdf0e10cSrcweir 	TTimeValue  m_RepeatDelta;
182*cdf0e10cSrcweir     /// Pointer to the next timer (to fire)
183*cdf0e10cSrcweir   	OTimer*	 	m_pNext;
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir private:
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir     /// copy constructor disabled
188*cdf0e10cSrcweir     OTimer(const OTimer& rTimer);
189*cdf0e10cSrcweir     /// assignment operator disabled
190*cdf0e10cSrcweir     void SAL_CALL operator=(const OTimer& rTimer);
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 	friend class OTimerManager;
193*cdf0e10cSrcweir };
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir #endif  //_VOS_TIMER_HXX_
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir 
201