xref: /aoo4110/main/vos/inc/vos/timer.hxx (revision b1cdbd2c)
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 
32 
33 namespace vos
34 {
35 
36 /////////////////////////////////////////////////////////////////////////////
37 //
38 // TTimeValue
39 //
40 
41 /** <code> struct TTimeValue </code> : class for times. Times are seconds in UTC since 01.01.1970
42  */
43 struct TTimeValue : public TimeValue
44 {
TTimeValuevos::TTimeValue45 	TTimeValue()
46     { Seconds = 0; Nanosec = 0; }
47 
48     TTimeValue(sal_uInt32 Seconds, sal_uInt32 Nano);
49 
TTimeValuevos::TTimeValue50     TTimeValue(sal_uInt32 MilliSecs)
51     { Seconds = MilliSecs / 1000L; Nanosec = (MilliSecs % 1000) * 1000000L; }
52 
TTimeValuevos::TTimeValue53     TTimeValue(const TTimeValue& rTimeValue)
54     { Seconds = rTimeValue.Seconds; Nanosec = rTimeValue.Nanosec; }
55 
TTimeValuevos::TTimeValue56     TTimeValue(const TimeValue& rTimeValue)
57     { Seconds = rTimeValue.Seconds; Nanosec = rTimeValue.Nanosec; }
58 
59 	void		SAL_CALL normalize();
60 
61     void		SAL_CALL addTime(const TTimeValue& Delta);
62 
63 	sal_Bool	SAL_CALL isEmpty() const;
64 };
65 
normalize()66 inline void TTimeValue::normalize()
67 {
68 	if (Nanosec > 1000000000)
69 	{
70 		Seconds += Nanosec / 1000000000;
71 		Nanosec %= 1000000000;
72 	}
73 }
74 
TTimeValue(sal_uInt32 Secs,sal_uInt32 Nano)75 inline TTimeValue::TTimeValue(sal_uInt32 Secs, sal_uInt32 Nano)
76 {
77 	Seconds = Secs;
78 	Nanosec = Nano;
79 
80     normalize();
81 }
82 
addTime(const TTimeValue & Time)83 inline void TTimeValue::addTime(const TTimeValue& Time)
84 {
85 	Seconds += Time.Seconds;
86 	Nanosec += Time.Nanosec;
87 
88 	normalize();
89 }
90 
isEmpty() const91 inline sal_Bool TTimeValue::isEmpty() const
92 {
93 	return ((Seconds == 0) && (Nanosec == 0));
94 }
95 
operator <(const TTimeValue & rTimeA,const TTimeValue & rTimeB)96 inline sal_Bool operator<(const TTimeValue& rTimeA, const TTimeValue& rTimeB)
97 {
98 	if (rTimeA.Seconds < rTimeB.Seconds)
99 		return sal_True;
100 	else if (rTimeA.Seconds > rTimeB.Seconds)
101 		return sal_False;
102 	else
103 		return (rTimeA.Nanosec < rTimeB.Nanosec);
104 }
105 
operator >(const TTimeValue & rTimeA,const TTimeValue & rTimeB)106 inline sal_Bool operator>(const TTimeValue& rTimeA, const TTimeValue& rTimeB)
107 {
108 	if (rTimeA.Seconds > rTimeB.Seconds)
109 		return sal_True;
110 	else if (rTimeA.Seconds < rTimeB.Seconds)
111 		return sal_False;
112 	else
113 		return (rTimeA.Nanosec > rTimeB.Nanosec);
114 }
115 
operator ==(const TTimeValue & rTimeA,const TTimeValue & rTimeB)116 inline sal_Bool operator==(const TTimeValue& rTimeA, const TTimeValue& rTimeB)
117 {
118 	return ((rTimeA.Seconds == rTimeB.Seconds) &&
119 		    (rTimeA.Nanosec == rTimeB.Nanosec));
120 }
121 
122 
123 /////////////////////////////////////////////////////////////////////////////
124 //
125 //  Timer class
126 //
127 
128 class OTimerManager;
129 
130 /** <code> class OTimer </code> : Interface for the Timer and handling the event
131 */
132 class OTimer : virtual public OReference , virtual public OObject
133 {
134 	VOS_DECLARE_CLASSINFO(VOS_NAMESPACE(OTimer, vos));
135 
136 public:
137 
138     /// constructor
139   	OTimer();
140 	/// constructor
141   	OTimer(const TTimeValue& Time);
142 	/// constructor
143   	OTimer(const TTimeValue& Time, const TTimeValue& RepeatTime);
144   	/// start timer.
145   	void SAL_CALL start();
146   	/// abort timer prematurely.
147   	void SAL_CALL stop();
148   	/// returns <code> sal_True </code> if timer is running.
149   	sal_Bool 	SAL_CALL isTicking() const;
150 	/// is the timer expired?
151   	sal_Bool 	SAL_CALL isExpired() const;
152 	/// does <code> pTimer </code> expires before us?
153   	sal_Bool    SAL_CALL expiresBefore(const OTimer* pTimer) const;
154 	/// set the absolute time when the timer should fire
155   	void		SAL_CALL setAbsoluteTime(const TTimeValue& Time);
156 	/// set the time to fire to 'now' + <code> Remaining </code>
157   	void		SAL_CALL setRemainingTime(const TTimeValue& Remaining);
158 	/// set the time to fire to 'now' + <code> Remaining </code> with repeat interveal <code> Repeat </code>
159   	void		SAL_CALL setRemainingTime(const TTimeValue& Remaining, const TTimeValue& Repeat);
160 	/// adds <code> Time </code> to the 'fire time'
161   	void		SAL_CALL addTime(const TTimeValue& Time);
162 	/// returns the remaining time before timer expiration relative to now
163   	TTimeValue	SAL_CALL getRemainingTime() const;
164 
165 protected:
166 
167     /// destructor
168     virtual ~OTimer();
169     /// what should be done when the 'timer fires'
170     virtual void SAL_CALL onShot() = 0;
171 
172     /// holds (initial) exparation time of this timer
173 	TTimeValue  m_TimeOut;
174     /// holds the time of exparation of this timer
175 	TTimeValue  m_Expired;
176     /// holds the time interveal of successive exparations
177 	TTimeValue  m_RepeatDelta;
178     /// Pointer to the next timer (to fire)
179   	OTimer*	 	m_pNext;
180 
181 private:
182 
183     /// copy constructor disabled
184     OTimer(const OTimer& rTimer);
185     /// assignment operator disabled
186     void SAL_CALL operator=(const OTimer& rTimer);
187 
188 	friend class OTimerManager;
189 };
190 
191 }
192 
193 
194 #endif  //_VOS_TIMER_HXX_
195 
196 
197