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