1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #ifndef INCLUDED_SALHELPER_TIMER_HXX
22 #define INCLUDED_SALHELPER_TIMER_HXX
24 #include <salhelper/simplereferenceobject.hxx>
26 #include <salhelper/salhelperdllapi.h>
31 /** Helper class for easier manipulation with TimeValue.
33 * Times are seconds in UTC since 01.01.1970
35 struct TTimeValue
: public TimeValue
43 TTimeValue( sal_uInt32 Secs
, sal_uInt32 Nano
)
51 TTimeValue(sal_uInt32 MilliSecs
)
53 Seconds
= MilliSecs
/ 1000L;
54 Nanosec
= (MilliSecs
% 1000) * 1000000L;
59 TTimeValue( const TTimeValue
& rTimeValue
)
61 Seconds
= rTimeValue
.Seconds
;
62 Nanosec
= rTimeValue
.Nanosec
;
67 TTimeValue( const TimeValue
& rTimeValue
)
69 Seconds
= rTimeValue
.Seconds
;
70 Nanosec
= rTimeValue
.Nanosec
;
75 void SAL_CALL
normalize()
77 if ( Nanosec
> 1000000000 )
79 Seconds
+= Nanosec
/ 1000000000;
80 Nanosec
%= 1000000000;
84 void SAL_CALL
addTime( const TTimeValue
& Delta
)
86 Seconds
+= Delta
.Seconds
;
87 Nanosec
+= Delta
.Nanosec
;
92 bool SAL_CALL
isEmpty() const
94 return ( ( Seconds
== 0 ) && ( Nanosec
== 0 ) );
98 inline bool operator<( const TTimeValue
& rTimeA
, const TTimeValue
& rTimeB
)
100 if ( rTimeA
.Seconds
< rTimeB
.Seconds
)
102 else if ( rTimeA
.Seconds
> rTimeB
.Seconds
)
105 return ( rTimeA
.Nanosec
< rTimeB
.Nanosec
);
108 inline bool operator>( const TTimeValue
& rTimeA
, const TTimeValue
& rTimeB
)
110 if ( rTimeA
.Seconds
> rTimeB
.Seconds
)
112 else if ( rTimeA
.Seconds
< rTimeB
.Seconds
)
115 return ( rTimeA
.Nanosec
> rTimeB
.Nanosec
);
118 inline bool operator==( const TTimeValue
& rTimeA
, const TTimeValue
& rTimeB
)
120 return ( ( rTimeA
.Seconds
== rTimeB
.Seconds
) &&
121 ( rTimeA
.Nanosec
== rTimeB
.Nanosec
) );
126 /** Interface for the Timer and handling the event
128 class SALHELPER_DLLPUBLIC Timer
: public salhelper::SimpleReferenceObject
138 Timer( const TTimeValue
& Time
);
142 Timer( const TTimeValue
& Time
, const TTimeValue
& RepeatTime
);
146 void SAL_CALL
start();
148 /** Abort timer prematurely.
150 void SAL_CALL
stop();
152 /** Returns sal_True if timer is running.
154 sal_Bool SAL_CALL
isTicking() const;
156 /** Is the timer expired?
158 sal_Bool SAL_CALL
isExpired() const;
160 /** Does pTimer expires before us?
162 sal_Bool SAL_CALL
expiresBefore( const Timer
* pTimer
) const;
164 /** Set the absolute time when the timer should fire.
166 void SAL_CALL
setAbsoluteTime( const TTimeValue
& Time
);
168 /** Set the time to fire to 'now' + Remaining.
170 void SAL_CALL
setRemainingTime( const TTimeValue
& Remaining
);
172 /** Set the time to fire to 'now' + Remaining with repeat interveal
175 void SAL_CALL
setRemainingTime( const TTimeValue
& Remaining
, const TTimeValue
& Repeat
);
177 /** Adds Time to the 'fire time'.
179 void SAL_CALL
addTime( const TTimeValue
& Time
);
181 /** Returns the remaining time before timer expiration relative to now.
183 TTimeValue SAL_CALL
getRemainingTime() const;
191 /** What should be done when the 'timer fires'.
193 virtual void SAL_CALL
onShot() = 0;
197 /** holds (initial) exparation time of this timer.
199 TTimeValue m_aTimeOut
;
201 /** holds the time of exparation of this timer.
203 TTimeValue m_aExpired
;
205 /** holds the time interveal of successive expirations.
207 TTimeValue m_aRepeatDelta
;
209 /** Pointer to the next timer (to fire).
215 /** Copy constructor deleted.
217 Timer( const Timer
& rTimer
) SAL_DELETED_FUNCTION
;
219 /** Copy assignment operator deleted.
221 void SAL_CALL
operator=( const Timer
& rTimer
) SAL_DELETED_FUNCTION
;
223 friend class TimerManager
;
228 #endif // INCLUDED_SALHELPER_TIMER_HXX
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */