Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / salhelper / timer.hxx
blobab02090d46daff7779bc6c03598f406809cfaa56
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
25 #include <osl/time.h>
26 #include <salhelper/salhelperdllapi.h>
28 namespace salhelper
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
37 TTimeValue()
39 Seconds = 0;
40 Nanosec = 0;
43 TTimeValue( sal_uInt32 Secs, sal_uInt32 Nano )
45 Seconds = Secs;
46 Nanosec = Nano;
48 normalize();
51 TTimeValue(sal_uInt32 MilliSecs)
53 Seconds = MilliSecs / 1000L;
54 Nanosec = (MilliSecs % 1000) * 1000000L;
56 normalize();
59 TTimeValue( const TTimeValue& rTimeValue ):
60 TimeValue(rTimeValue)
62 normalize();
65 TTimeValue( const TimeValue& rTimeValue )
67 Seconds = rTimeValue.Seconds;
68 Nanosec = rTimeValue.Nanosec;
70 normalize();
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;
87 normalize();
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 )
99 return true;
100 else if ( rTimeA.Seconds > rTimeB.Seconds )
101 return false;
102 else
103 return ( rTimeA.Nanosec < rTimeB.Nanosec );
106 inline bool operator>( const TTimeValue& rTimeA, const TTimeValue& rTimeB )
108 if ( rTimeA.Seconds > rTimeB.Seconds )
109 return true;
110 else if ( rTimeA.Seconds < rTimeB.Seconds )
111 return false;
112 else
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 ) );
122 class TimerManager;
124 /** Interface for the Timer and handling the event
126 class SALHELPER_DLLPUBLIC Timer : public salhelper::SimpleReferenceObject
128 public:
130 /** Constructor.
132 Timer();
134 /** Constructor.
136 Timer( const TTimeValue& Time );
138 /** Constructor.
140 Timer( const TTimeValue& Time, const TTimeValue& RepeatTime );
142 /** Start timer.
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
171 * Repeat.
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;
183 protected:
185 /** Destructor.
187 virtual ~Timer() SAL_OVERRIDE;
189 /** What should be done when the 'timer fires'.
191 virtual void SAL_CALL onShot() = 0;
193 protected:
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).
209 Timer* m_pNext;
211 private:
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: */