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 TimeValue
& rTimeValue
)
61 Seconds
= rTimeValue
.Seconds
;
62 Nanosec
= rTimeValue
.Nanosec
;
67 void SAL_CALL
normalize()
69 if ( Nanosec
> 1000000000 )
71 Seconds
+= Nanosec
/ 1000000000;
72 Nanosec
%= 1000000000;
76 void SAL_CALL
addTime( const TTimeValue
& Delta
)
78 Seconds
+= Delta
.Seconds
;
79 Nanosec
+= Delta
.Nanosec
;
84 bool SAL_CALL
isEmpty() const
86 return ( ( Seconds
== 0 ) && ( Nanosec
== 0 ) );
90 inline bool operator<( const TTimeValue
& rTimeA
, const TTimeValue
& rTimeB
)
92 if ( rTimeA
.Seconds
< rTimeB
.Seconds
)
94 else if ( rTimeA
.Seconds
> rTimeB
.Seconds
)
97 return ( rTimeA
.Nanosec
< rTimeB
.Nanosec
);
100 inline bool operator>( const TTimeValue
& rTimeA
, const TTimeValue
& rTimeB
)
102 if ( rTimeA
.Seconds
> rTimeB
.Seconds
)
104 else if ( rTimeA
.Seconds
< rTimeB
.Seconds
)
107 return ( rTimeA
.Nanosec
> rTimeB
.Nanosec
);
110 inline bool operator==( const TTimeValue
& rTimeA
, const TTimeValue
& rTimeB
)
112 return ( ( rTimeA
.Seconds
== rTimeB
.Seconds
) &&
113 ( rTimeA
.Nanosec
== rTimeB
.Nanosec
) );
118 /** Interface for the Timer and handling the event
120 class SALHELPER_DLLPUBLIC Timer
: public salhelper::SimpleReferenceObject
130 Timer( const TTimeValue
& Time
);
134 Timer( const TTimeValue
& Time
, const TTimeValue
& RepeatTime
);
138 void SAL_CALL
start();
140 /** Abort timer prematurely.
142 void SAL_CALL
stop();
144 /** Returns sal_True if timer is running.
146 sal_Bool SAL_CALL
isTicking() const;
148 /** Is the timer expired?
150 sal_Bool SAL_CALL
isExpired() const;
152 /** Does pTimer expires before us?
154 sal_Bool SAL_CALL
expiresBefore( const Timer
* pTimer
) const;
156 /** Set the absolute time when the timer should fire.
158 void SAL_CALL
setAbsoluteTime( const TTimeValue
& Time
);
160 /** Set the time to fire to 'now' + Remaining.
162 void SAL_CALL
setRemainingTime( const TTimeValue
& Remaining
);
164 /** Set the time to fire to 'now' + Remaining with repeat interveal
167 void SAL_CALL
setRemainingTime( const TTimeValue
& Remaining
, const TTimeValue
& Repeat
);
169 /** Adds Time to the 'fire time'.
171 void SAL_CALL
addTime( const TTimeValue
& Time
);
173 /** Returns the remaining time before timer expiration relative to now.
175 TTimeValue SAL_CALL
getRemainingTime() const;
181 virtual ~Timer() SAL_OVERRIDE
;
183 /** What should be done when the 'timer fires'.
185 virtual void SAL_CALL
onShot() = 0;
189 /** holds (initial) expiration time of this timer.
191 TTimeValue m_aTimeOut
;
193 /** holds the time of expiration of this timer.
195 TTimeValue m_aExpired
;
197 /** holds the time interveal of successive expirations.
199 TTimeValue m_aRepeatDelta
;
201 /** Pointer to the next timer (to fire).
207 /** Copy constructor deleted.
209 Timer( const Timer
& rTimer
) SAL_DELETED_FUNCTION
;
211 /** Copy assignment operator deleted.
213 void SAL_CALL
operator=( const Timer
& rTimer
) SAL_DELETED_FUNCTION
;
215 friend class TimerManager
;
220 #endif // INCLUDED_SALHELPER_TIMER_HXX
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */