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 * This file is part of LibreOffice published API.
24 #ifndef INCLUDED_SALHELPER_TIMER_HXX
25 #define INCLUDED_SALHELPER_TIMER_HXX
27 #include "salhelper/simplereferenceobject.hxx"
29 #include "salhelper/salhelperdllapi.h"
34 /** Helper class for easier manipulation with TimeValue.
36 * Times are seconds in UTC since 01.01.1970
38 struct SAL_WARN_UNUSED TTimeValue
: public TimeValue
46 TTimeValue( sal_uInt32 Secs
, sal_uInt32 Nano
)
54 TTimeValue(sal_uInt32 MilliSecs
)
56 Seconds
= MilliSecs
/ 1000;
57 Nanosec
= (MilliSecs
% 1000) * 1000000L;
62 TTimeValue( const TimeValue
& rTimeValue
)
64 Seconds
= rTimeValue
.Seconds
;
65 Nanosec
= rTimeValue
.Nanosec
;
70 void SAL_CALL
normalize()
72 if ( Nanosec
> 1000000000 )
74 Seconds
+= Nanosec
/ 1000000000;
75 Nanosec
%= 1000000000;
79 void SAL_CALL
addTime( const TTimeValue
& Delta
)
81 Seconds
+= Delta
.Seconds
;
82 Nanosec
+= Delta
.Nanosec
;
87 bool SAL_CALL
isEmpty() const
89 return ( ( Seconds
== 0 ) && ( Nanosec
== 0 ) );
93 inline bool operator<( const TTimeValue
& rTimeA
, const TTimeValue
& rTimeB
)
95 if ( rTimeA
.Seconds
< rTimeB
.Seconds
)
97 else if ( rTimeA
.Seconds
> rTimeB
.Seconds
)
100 return ( rTimeA
.Nanosec
< rTimeB
.Nanosec
);
103 inline bool operator>( const TTimeValue
& rTimeA
, const TTimeValue
& rTimeB
)
105 if ( rTimeA
.Seconds
> rTimeB
.Seconds
)
107 else if ( rTimeA
.Seconds
< rTimeB
.Seconds
)
110 return ( rTimeA
.Nanosec
> rTimeB
.Nanosec
);
113 inline bool operator==( const TTimeValue
& rTimeA
, const TTimeValue
& rTimeB
)
115 return ( ( rTimeA
.Seconds
== rTimeB
.Seconds
) &&
116 ( rTimeA
.Nanosec
== rTimeB
.Nanosec
) );
121 /** Interface for the Timer and handling the event
123 class SALHELPER_DLLPUBLIC Timer
: public salhelper::SimpleReferenceObject
133 Timer( const TTimeValue
& Time
);
137 Timer( const TTimeValue
& Time
, const TTimeValue
& RepeatTime
);
141 void SAL_CALL
start();
143 /** Abort timer prematurely.
145 void SAL_CALL
stop();
147 /** Returns sal_True if timer is running.
149 sal_Bool SAL_CALL
isTicking() const;
151 /** Is the timer expired?
153 sal_Bool SAL_CALL
isExpired() const;
155 /** Does pTimer expires before us?
157 sal_Bool SAL_CALL
expiresBefore( const Timer
* pTimer
) const;
159 /** Set the absolute time when the timer should fire.
161 void SAL_CALL
setAbsoluteTime( const TTimeValue
& Time
);
163 /** Set the time to fire to 'now' + Remaining.
165 void SAL_CALL
setRemainingTime( const TTimeValue
& Remaining
);
167 /** Set the time to fire to 'now' + Remaining with repeat interveal
170 void SAL_CALL
setRemainingTime( const TTimeValue
& Remaining
, const TTimeValue
& Repeat
);
172 /** Adds Time to the 'fire time'.
174 void SAL_CALL
addTime( const TTimeValue
& Time
);
176 /** Returns the remaining time before timer expiration relative to now.
178 TTimeValue SAL_CALL
getRemainingTime() const;
184 virtual ~Timer() SAL_OVERRIDE
;
186 /** What should be done when the 'timer fires'.
188 virtual void SAL_CALL
onShot() = 0;
192 /** holds (initial) expiration time of this timer.
194 TTimeValue m_aTimeOut
;
196 /** holds the time of expiration of this timer.
198 TTimeValue m_aExpired
;
200 /** holds the time interveal of successive expirations.
202 TTimeValue m_aRepeatDelta
;
204 /** Pointer to the next timer (to fire).
210 /** Copy constructor deleted.
212 Timer( const Timer
& rTimer
) SAL_DELETED_FUNCTION
;
214 /** Copy assignment operator deleted.
216 void SAL_CALL
operator=( const Timer
& rTimer
) SAL_DELETED_FUNCTION
;
218 friend class TimerManager
;
223 #endif // INCLUDED_SALHELPER_TIMER_HXX
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */