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 SAL_WARN_UNUSED 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
):
65 TTimeValue( const TimeValue
& rTimeValue
)
67 Seconds
= rTimeValue
.Seconds
;
68 Nanosec
= rTimeValue
.Nanosec
;
73 void SAL_CALL
normalize()
75 if ( Nanosec
> 1000000000 )
77 Seconds
+= Nanosec
/ 1000000000;
78 Nanosec
%= 1000000000;
82 void SAL_CALL
addTime( const TTimeValue
& Delta
)
84 Seconds
+= Delta
.Seconds
;
85 Nanosec
+= Delta
.Nanosec
;
90 bool SAL_CALL
isEmpty() const
92 return ( ( Seconds
== 0 ) && ( Nanosec
== 0 ) );
96 inline bool operator<( const TTimeValue
& rTimeA
, const TTimeValue
& rTimeB
)
98 if ( rTimeA
.Seconds
< rTimeB
.Seconds
)
100 else if ( rTimeA
.Seconds
> rTimeB
.Seconds
)
103 return ( rTimeA
.Nanosec
< rTimeB
.Nanosec
);
106 inline bool operator>( const TTimeValue
& rTimeA
, const TTimeValue
& rTimeB
)
108 if ( rTimeA
.Seconds
> rTimeB
.Seconds
)
110 else if ( rTimeA
.Seconds
< rTimeB
.Seconds
)
113 return ( rTimeA
.Nanosec
> rTimeB
.Nanosec
);
116 inline bool operator==( const TTimeValue
& rTimeA
, const TTimeValue
& rTimeB
)
118 return ( ( rTimeA
.Seconds
== rTimeB
.Seconds
) &&
119 ( rTimeA
.Nanosec
== rTimeB
.Nanosec
) );
124 /** Interface for the Timer and handling the event
126 class SALHELPER_DLLPUBLIC Timer
: public salhelper::SimpleReferenceObject
136 Timer( const TTimeValue
& Time
);
140 Timer( const TTimeValue
& Time
, const TTimeValue
& RepeatTime
);
144 void SAL_CALL
start();
146 /** Abort timer prematurely.
148 void SAL_CALL
stop();
150 /** Returns sal_True if timer is running.
152 sal_Bool SAL_CALL
isTicking() const;
154 /** Is the timer expired?
156 sal_Bool SAL_CALL
isExpired() const;
158 /** Does pTimer expires before us?
160 sal_Bool SAL_CALL
expiresBefore( const Timer
* pTimer
) const;
162 /** Set the absolute time when the timer should fire.
164 void SAL_CALL
setAbsoluteTime( const TTimeValue
& Time
);
166 /** Set the time to fire to 'now' + Remaining.
168 void SAL_CALL
setRemainingTime( const TTimeValue
& Remaining
);
170 /** Set the time to fire to 'now' + Remaining with repeat interveal
173 void SAL_CALL
setRemainingTime( const TTimeValue
& Remaining
, const TTimeValue
& Repeat
);
175 /** Adds Time to the 'fire time'.
177 void SAL_CALL
addTime( const TTimeValue
& Time
);
179 /** Returns the remaining time before timer expiration relative to now.
181 TTimeValue SAL_CALL
getRemainingTime() const;
187 virtual ~Timer() SAL_OVERRIDE
;
189 /** What should be done when the 'timer fires'.
191 virtual void SAL_CALL
onShot() = 0;
195 /** holds (initial) exparation time of this timer.
197 TTimeValue m_aTimeOut
;
199 /** holds the time of exparation of this timer.
201 TTimeValue m_aExpired
;
203 /** holds the time interveal of successive expirations.
205 TTimeValue m_aRepeatDelta
;
207 /** Pointer to the next timer (to fire).
213 /** Copy constructor deleted.
215 Timer( const Timer
& rTimer
) SAL_DELETED_FUNCTION
;
217 /** Copy assignment operator deleted.
219 void SAL_CALL
operator=( const Timer
& rTimer
) SAL_DELETED_FUNCTION
;
221 friend class TimerManager
;
226 #endif // INCLUDED_SALHELPER_TIMER_HXX
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */