Branch libreoffice-5-0-4
[LibreOffice.git] / include / salhelper / timer.hxx
blobac2a523c4b6aceef351c55f84fc20ccc61bb9423
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 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 )
61 Seconds = rTimeValue.Seconds;
62 Nanosec = rTimeValue.Nanosec;
64 normalize();
67 TTimeValue( const TimeValue& rTimeValue )
69 Seconds = rTimeValue.Seconds;
70 Nanosec = rTimeValue.Nanosec;
72 normalize();
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;
89 normalize();
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 )
101 return true;
102 else if ( rTimeA.Seconds > rTimeB.Seconds )
103 return false;
104 else
105 return ( rTimeA.Nanosec < rTimeB.Nanosec );
108 inline bool operator>( const TTimeValue& rTimeA, const TTimeValue& rTimeB )
110 if ( rTimeA.Seconds > rTimeB.Seconds )
111 return true;
112 else if ( rTimeA.Seconds < rTimeB.Seconds )
113 return false;
114 else
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 ) );
124 class TimerManager;
126 /** Interface for the Timer and handling the event
128 class SALHELPER_DLLPUBLIC Timer : public salhelper::SimpleReferenceObject
130 public:
132 /** Constructor.
134 Timer();
136 /** Constructor.
138 Timer( const TTimeValue& Time );
140 /** Constructor.
142 Timer( const TTimeValue& Time, const TTimeValue& RepeatTime );
144 /** Start timer.
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
173 * Repeat.
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;
185 protected:
187 /** Destructor.
189 virtual ~Timer();
191 /** What should be done when the 'timer fires'.
193 virtual void SAL_CALL onShot() = 0;
195 protected:
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).
211 Timer* m_pNext;
213 private:
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: */