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