Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #ifndef INCLUDED_SALHELPER_TIMER_HXX
22 : #define INCLUDED_SALHELPER_TIMER_HXX
23 :
24 : #include <salhelper/simplereferenceobject.hxx>
25 : #include <osl/time.h>
26 : #include <salhelper/salhelperdllapi.h>
27 :
28 : namespace salhelper
29 : {
30 :
31 : /** Helper class for easier manipulation with TimeValue.
32 : *
33 : * Times are seconds in UTC since 01.01.1970
34 : */
35 : struct TTimeValue : public TimeValue
36 : {
37 2667 : TTimeValue()
38 2667 : {
39 2667 : Seconds = 0;
40 2667 : Nanosec = 0;
41 2667 : }
42 :
43 198 : TTimeValue( sal_uInt32 Secs, sal_uInt32 Nano )
44 198 : {
45 198 : Seconds = Secs;
46 198 : Nanosec = Nano;
47 :
48 198 : normalize();
49 198 : }
50 :
51 0 : TTimeValue(sal_uInt32 MilliSecs)
52 0 : {
53 0 : Seconds = MilliSecs / 1000L;
54 0 : Nanosec = (MilliSecs % 1000) * 1000000L;
55 :
56 0 : normalize();
57 0 : }
58 :
59 0 : TTimeValue( const TTimeValue& rTimeValue )
60 0 : {
61 0 : Seconds = rTimeValue.Seconds;
62 0 : Nanosec = rTimeValue.Nanosec;
63 :
64 0 : normalize();
65 0 : }
66 :
67 : TTimeValue( const TimeValue& rTimeValue )
68 : {
69 : Seconds = rTimeValue.Seconds;
70 : Nanosec = rTimeValue.Nanosec;
71 :
72 : normalize();
73 : }
74 :
75 396 : void SAL_CALL normalize()
76 : {
77 396 : if ( Nanosec > 1000000000 )
78 : {
79 0 : Seconds += Nanosec / 1000000000;
80 0 : Nanosec %= 1000000000;
81 : }
82 396 : }
83 :
84 198 : void SAL_CALL addTime( const TTimeValue& Delta )
85 : {
86 198 : Seconds += Delta.Seconds;
87 198 : Nanosec += Delta.Nanosec;
88 :
89 198 : normalize();
90 198 : }
91 :
92 0 : bool SAL_CALL isEmpty() const
93 : {
94 0 : return ( ( Seconds == 0 ) && ( Nanosec == 0 ) );
95 : }
96 : };
97 :
98 0 : inline bool operator<( const TTimeValue& rTimeA, const TTimeValue& rTimeB )
99 : {
100 0 : if ( rTimeA.Seconds < rTimeB.Seconds )
101 0 : return true;
102 0 : else if ( rTimeA.Seconds > rTimeB.Seconds )
103 0 : return false;
104 : else
105 0 : return ( rTimeA.Nanosec < rTimeB.Nanosec );
106 : }
107 :
108 : inline bool operator>( const TTimeValue& rTimeA, const TTimeValue& rTimeB )
109 : {
110 : if ( rTimeA.Seconds > rTimeB.Seconds )
111 : return true;
112 : else if ( rTimeA.Seconds < rTimeB.Seconds )
113 : return false;
114 : else
115 : return ( rTimeA.Nanosec > rTimeB.Nanosec );
116 : }
117 :
118 : inline bool operator==( const TTimeValue& rTimeA, const TTimeValue& rTimeB )
119 : {
120 : return ( ( rTimeA.Seconds == rTimeB.Seconds ) &&
121 : ( rTimeA.Nanosec == rTimeB.Nanosec ) );
122 : }
123 :
124 : class TimerManager;
125 :
126 : /** Interface for the Timer and handling the event
127 : */
128 : class SALHELPER_DLLPUBLIC Timer : public salhelper::SimpleReferenceObject
129 : {
130 : public:
131 :
132 : /** Constructor.
133 : */
134 : Timer();
135 :
136 : /** Constructor.
137 : */
138 : Timer( const TTimeValue& Time );
139 :
140 : /** Constructor.
141 : */
142 : Timer( const TTimeValue& Time, const TTimeValue& RepeatTime );
143 :
144 : /** Start timer.
145 : */
146 : void SAL_CALL start();
147 :
148 : /** Abort timer prematurely.
149 : */
150 : void SAL_CALL stop();
151 :
152 : /** Returns sal_True if timer is running.
153 : */
154 : sal_Bool SAL_CALL isTicking() const;
155 :
156 : /** Is the timer expired?
157 : */
158 : sal_Bool SAL_CALL isExpired() const;
159 :
160 : /** Does pTimer expires before us?
161 : */
162 : sal_Bool SAL_CALL expiresBefore( const Timer* pTimer ) const;
163 :
164 : /** Set the absolute time when the timer should fire.
165 : */
166 : void SAL_CALL setAbsoluteTime( const TTimeValue& Time );
167 :
168 : /** Set the time to fire to 'now' + Remaining.
169 : */
170 : void SAL_CALL setRemainingTime( const TTimeValue& Remaining );
171 :
172 : /** Set the time to fire to 'now' + Remaining with repeat interveal
173 : * Repeat.
174 : */
175 : void SAL_CALL setRemainingTime( const TTimeValue& Remaining, const TTimeValue& Repeat );
176 :
177 : /** Adds Time to the 'fire time'.
178 : */
179 : void SAL_CALL addTime( const TTimeValue& Time );
180 :
181 : /** Returns the remaining time before timer expiration relative to now.
182 : */
183 : TTimeValue SAL_CALL getRemainingTime() const;
184 :
185 : protected:
186 :
187 : /** Destructor.
188 : */
189 : virtual ~Timer();
190 :
191 : /** What should be done when the 'timer fires'.
192 : */
193 : virtual void SAL_CALL onShot() = 0;
194 :
195 : protected:
196 :
197 : /** holds (initial) exparation time of this timer.
198 : */
199 : TTimeValue m_aTimeOut;
200 :
201 : /** holds the time of exparation of this timer.
202 : */
203 : TTimeValue m_aExpired;
204 :
205 : /** holds the time interveal of successive expirations.
206 : */
207 : TTimeValue m_aRepeatDelta;
208 :
209 : /** Pointer to the next timer (to fire).
210 : */
211 : Timer* m_pNext;
212 :
213 : private:
214 :
215 : /** Copy constructor deleted.
216 : */
217 : Timer( const Timer& rTimer ) SAL_DELETED_FUNCTION;
218 :
219 : /** Copy assignment operator deleted.
220 : */
221 : void SAL_CALL operator=( const Timer& rTimer ) SAL_DELETED_FUNCTION;
222 :
223 : friend class TimerManager;
224 : };
225 :
226 : }
227 :
228 : #endif // INCLUDED_SALHELPER_TIMER_HXX
229 :
230 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|