xref: /aoo41x/main/vos/inc/vos/timer.hxx (revision 1be3ed10)
1*1be3ed10SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*1be3ed10SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*1be3ed10SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*1be3ed10SAndrew Rist  * distributed with this work for additional information
6*1be3ed10SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*1be3ed10SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*1be3ed10SAndrew Rist  * "License"); you may not use this file except in compliance
9*1be3ed10SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*1be3ed10SAndrew Rist  *
11*1be3ed10SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*1be3ed10SAndrew Rist  *
13*1be3ed10SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*1be3ed10SAndrew Rist  * software distributed under the License is distributed on an
15*1be3ed10SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*1be3ed10SAndrew Rist  * KIND, either express or implied.  See the License for the
17*1be3ed10SAndrew Rist  * specific language governing permissions and limitations
18*1be3ed10SAndrew Rist  * under the License.
19*1be3ed10SAndrew Rist  *
20*1be3ed10SAndrew Rist  *************************************************************/
21*1be3ed10SAndrew Rist 
22*1be3ed10SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #ifndef _VOS_TIMER_HXX_
26cdf0e10cSrcweir #define _VOS_TIMER_HXX_
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #	include <vos/refernce.hxx>
29cdf0e10cSrcweir #	include <vos/mutex.hxx>
30cdf0e10cSrcweir #	include <osl/time.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir 
33cdf0e10cSrcweir namespace vos
34cdf0e10cSrcweir {
35cdf0e10cSrcweir 
36cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////////
37cdf0e10cSrcweir //
38cdf0e10cSrcweir // TTimeValue
39cdf0e10cSrcweir //
40cdf0e10cSrcweir 
41cdf0e10cSrcweir /** <code> struct TTimeValue </code> : class for times. Times are seconds in UTC since 01.01.1970
42cdf0e10cSrcweir  */
43cdf0e10cSrcweir struct TTimeValue : public TimeValue
44cdf0e10cSrcweir {
TTimeValuevos::TTimeValue45cdf0e10cSrcweir 	TTimeValue()
46cdf0e10cSrcweir     { Seconds = 0; Nanosec = 0; }
47cdf0e10cSrcweir 
48cdf0e10cSrcweir     TTimeValue(sal_uInt32 Seconds, sal_uInt32 Nano);
49cdf0e10cSrcweir 
TTimeValuevos::TTimeValue50cdf0e10cSrcweir     TTimeValue(sal_uInt32 MilliSecs)
51cdf0e10cSrcweir     { Seconds = MilliSecs / 1000L; Nanosec = (MilliSecs % 1000) * 1000000L; }
52cdf0e10cSrcweir 
TTimeValuevos::TTimeValue53cdf0e10cSrcweir     TTimeValue(const TTimeValue& rTimeValue)
54cdf0e10cSrcweir     { Seconds = rTimeValue.Seconds; Nanosec = rTimeValue.Nanosec; }
55cdf0e10cSrcweir 
TTimeValuevos::TTimeValue56cdf0e10cSrcweir     TTimeValue(const TimeValue& rTimeValue)
57cdf0e10cSrcweir     { Seconds = rTimeValue.Seconds; Nanosec = rTimeValue.Nanosec; }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 	void		SAL_CALL normalize();
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     void		SAL_CALL addTime(const TTimeValue& Delta);
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 	sal_Bool	SAL_CALL isEmpty() const;
64cdf0e10cSrcweir };
65cdf0e10cSrcweir 
normalize()66cdf0e10cSrcweir inline void TTimeValue::normalize()
67cdf0e10cSrcweir {
68cdf0e10cSrcweir 	if (Nanosec > 1000000000)
69cdf0e10cSrcweir 	{
70cdf0e10cSrcweir 		Seconds += Nanosec / 1000000000;
71cdf0e10cSrcweir 		Nanosec %= 1000000000;
72cdf0e10cSrcweir 	}
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
TTimeValue(sal_uInt32 Secs,sal_uInt32 Nano)75cdf0e10cSrcweir inline TTimeValue::TTimeValue(sal_uInt32 Secs, sal_uInt32 Nano)
76cdf0e10cSrcweir {
77cdf0e10cSrcweir 	Seconds = Secs;
78cdf0e10cSrcweir 	Nanosec = Nano;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     normalize();
81cdf0e10cSrcweir }
82cdf0e10cSrcweir 
addTime(const TTimeValue & Time)83cdf0e10cSrcweir inline void TTimeValue::addTime(const TTimeValue& Time)
84cdf0e10cSrcweir {
85cdf0e10cSrcweir 	Seconds += Time.Seconds;
86cdf0e10cSrcweir 	Nanosec += Time.Nanosec;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 	normalize();
89cdf0e10cSrcweir }
90cdf0e10cSrcweir 
isEmpty() const91cdf0e10cSrcweir inline sal_Bool TTimeValue::isEmpty() const
92cdf0e10cSrcweir {
93cdf0e10cSrcweir 	return ((Seconds == 0) && (Nanosec == 0));
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
operator <(const TTimeValue & rTimeA,const TTimeValue & rTimeB)96cdf0e10cSrcweir inline sal_Bool operator<(const TTimeValue& rTimeA, const TTimeValue& rTimeB)
97cdf0e10cSrcweir {
98cdf0e10cSrcweir 	if (rTimeA.Seconds < rTimeB.Seconds)
99cdf0e10cSrcweir 		return sal_True;
100cdf0e10cSrcweir 	else if (rTimeA.Seconds > rTimeB.Seconds)
101cdf0e10cSrcweir 		return sal_False;
102cdf0e10cSrcweir 	else
103cdf0e10cSrcweir 		return (rTimeA.Nanosec < rTimeB.Nanosec);
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
operator >(const TTimeValue & rTimeA,const TTimeValue & rTimeB)106cdf0e10cSrcweir inline sal_Bool operator>(const TTimeValue& rTimeA, const TTimeValue& rTimeB)
107cdf0e10cSrcweir {
108cdf0e10cSrcweir 	if (rTimeA.Seconds > rTimeB.Seconds)
109cdf0e10cSrcweir 		return sal_True;
110cdf0e10cSrcweir 	else if (rTimeA.Seconds < rTimeB.Seconds)
111cdf0e10cSrcweir 		return sal_False;
112cdf0e10cSrcweir 	else
113cdf0e10cSrcweir 		return (rTimeA.Nanosec > rTimeB.Nanosec);
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
operator ==(const TTimeValue & rTimeA,const TTimeValue & rTimeB)116cdf0e10cSrcweir inline sal_Bool operator==(const TTimeValue& rTimeA, const TTimeValue& rTimeB)
117cdf0e10cSrcweir {
118cdf0e10cSrcweir 	return ((rTimeA.Seconds == rTimeB.Seconds) &&
119cdf0e10cSrcweir 		    (rTimeA.Nanosec == rTimeB.Nanosec));
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 
123cdf0e10cSrcweir /////////////////////////////////////////////////////////////////////////////
124cdf0e10cSrcweir //
125cdf0e10cSrcweir //  Timer class
126cdf0e10cSrcweir //
127cdf0e10cSrcweir 
128cdf0e10cSrcweir class OTimerManager;
129cdf0e10cSrcweir 
130cdf0e10cSrcweir /** <code> class OTimer </code> : Interface for the Timer and handling the event
131cdf0e10cSrcweir */
132cdf0e10cSrcweir class OTimer : virtual public OReference , virtual public OObject
133cdf0e10cSrcweir {
134cdf0e10cSrcweir 	VOS_DECLARE_CLASSINFO(VOS_NAMESPACE(OTimer, vos));
135cdf0e10cSrcweir 
136cdf0e10cSrcweir public:
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     /// constructor
139cdf0e10cSrcweir   	OTimer();
140cdf0e10cSrcweir 	/// constructor
141cdf0e10cSrcweir   	OTimer(const TTimeValue& Time);
142cdf0e10cSrcweir 	/// constructor
143cdf0e10cSrcweir   	OTimer(const TTimeValue& Time, const TTimeValue& RepeatTime);
144cdf0e10cSrcweir   	/// start timer.
145cdf0e10cSrcweir   	void SAL_CALL start();
146cdf0e10cSrcweir   	/// abort timer prematurely.
147cdf0e10cSrcweir   	void SAL_CALL stop();
148cdf0e10cSrcweir   	/// returns <code> sal_True </code> if timer is running.
149cdf0e10cSrcweir   	sal_Bool 	SAL_CALL isTicking() const;
150cdf0e10cSrcweir 	/// is the timer expired?
151cdf0e10cSrcweir   	sal_Bool 	SAL_CALL isExpired() const;
152cdf0e10cSrcweir 	/// does <code> pTimer </code> expires before us?
153cdf0e10cSrcweir   	sal_Bool    SAL_CALL expiresBefore(const OTimer* pTimer) const;
154cdf0e10cSrcweir 	/// set the absolute time when the timer should fire
155cdf0e10cSrcweir   	void		SAL_CALL setAbsoluteTime(const TTimeValue& Time);
156cdf0e10cSrcweir 	/// set the time to fire to 'now' + <code> Remaining </code>
157cdf0e10cSrcweir   	void		SAL_CALL setRemainingTime(const TTimeValue& Remaining);
158cdf0e10cSrcweir 	/// set the time to fire to 'now' + <code> Remaining </code> with repeat interveal <code> Repeat </code>
159cdf0e10cSrcweir   	void		SAL_CALL setRemainingTime(const TTimeValue& Remaining, const TTimeValue& Repeat);
160cdf0e10cSrcweir 	/// adds <code> Time </code> to the 'fire time'
161cdf0e10cSrcweir   	void		SAL_CALL addTime(const TTimeValue& Time);
162cdf0e10cSrcweir 	/// returns the remaining time before timer expiration relative to now
163cdf0e10cSrcweir   	TTimeValue	SAL_CALL getRemainingTime() const;
164cdf0e10cSrcweir 
165cdf0e10cSrcweir protected:
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     /// destructor
168cdf0e10cSrcweir     virtual ~OTimer();
169cdf0e10cSrcweir     /// what should be done when the 'timer fires'
170cdf0e10cSrcweir     virtual void SAL_CALL onShot() = 0;
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     /// holds (initial) exparation time of this timer
173cdf0e10cSrcweir 	TTimeValue  m_TimeOut;
174cdf0e10cSrcweir     /// holds the time of exparation of this timer
175cdf0e10cSrcweir 	TTimeValue  m_Expired;
176cdf0e10cSrcweir     /// holds the time interveal of successive exparations
177cdf0e10cSrcweir 	TTimeValue  m_RepeatDelta;
178cdf0e10cSrcweir     /// Pointer to the next timer (to fire)
179cdf0e10cSrcweir   	OTimer*	 	m_pNext;
180cdf0e10cSrcweir 
181cdf0e10cSrcweir private:
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     /// copy constructor disabled
184cdf0e10cSrcweir     OTimer(const OTimer& rTimer);
185cdf0e10cSrcweir     /// assignment operator disabled
186cdf0e10cSrcweir     void SAL_CALL operator=(const OTimer& rTimer);
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 	friend class OTimerManager;
189cdf0e10cSrcweir };
190cdf0e10cSrcweir 
191cdf0e10cSrcweir }
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 
194cdf0e10cSrcweir #endif  //_VOS_TIMER_HXX_
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 
197