LibreOffice: salhelper/timer.hxx Source File

LibreOffice LibreOffice 25.8 SDK C/C++ API Reference

Loading...
Searching...
No Matches
timer.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20/*
21 * This file is part of LibreOffice published API.
22 */
23
24#ifndef INCLUDED_SALHELPER_TIMER_HXX
25#define INCLUDED_SALHELPER_TIMER_HXX
26
27#include "salhelper/simplereferenceobject.hxx"
28#include "osl/time.h"
29#include "salhelper/salhelperdllapi.h"
30
31namespace salhelper
32{
33
38 struct SAL_WARN_UNUSED TTimeValue : public TimeValue
39{
40 TTimeValue()
41 {
42 Seconds = 0;
43 Nanosec = 0;
44 }
45
46 TTimeValue( sal_uInt32 Secs, sal_uInt32 Nano )
47 {
48 Seconds = Secs;
49 Nanosec = Nano;
50
51 normalize();
52 }
53
54 TTimeValue(sal_uInt32 MilliSecs)
55 {
56 Seconds = MilliSecs / 1000;
57 Nanosec = (MilliSecs % 1000) * 1000000L;
58
59 normalize();
60 }
61
62 TTimeValue( const TimeValue& rTimeValue )
63 {
64 Seconds = rTimeValue.Seconds;
65 Nanosec = rTimeValue.Nanosec;
66
67 normalize();
68 }
69
70 void SAL_CALL normalize()
71 {
72 if ( Nanosec > 1000000000 )
73 {
74 Seconds += Nanosec / 1000000000;
75 Nanosec %= 1000000000;
76 }
77 }
78
79 void SAL_CALL addTime( const TTimeValue& Delta )
80 {
81 Seconds += Delta.Seconds;
82 Nanosec += Delta.Nanosec;
83
84 normalize();
85 }
86
87 bool SAL_CALL isEmpty() const
88 {
89 return ( ( Seconds == 0 ) && ( Nanosec == 0 ) );
90 }
91};
92
93 inline bool operator<( const TTimeValue& rTimeA, const TTimeValue& rTimeB )
94{
95 if ( rTimeA.Seconds < rTimeB.Seconds )
96 return true;
97 else if ( rTimeA.Seconds > rTimeB.Seconds )
98 return false;
99 else
100 return ( rTimeA.Nanosec < rTimeB.Nanosec );
101}
102
103 inline bool operator>( const TTimeValue& rTimeA, const TTimeValue& rTimeB )
104{
105 if ( rTimeA.Seconds > rTimeB.Seconds )
106 return true;
107 else if ( rTimeA.Seconds < rTimeB.Seconds )
108 return false;
109 else
110 return ( rTimeA.Nanosec > rTimeB.Nanosec );
111}
112
113 inline bool operator==( const TTimeValue& rTimeA, const TTimeValue& rTimeB )
114{
115 return ( ( rTimeA.Seconds == rTimeB.Seconds ) &&
116 ( rTimeA.Nanosec == rTimeB.Nanosec ) );
117}
118
119class TimerManager;
120
123 class SALHELPER_DLLPUBLIC Timer : public salhelper::SimpleReferenceObject
124{
125public:
126
129 Timer();
130
133 Timer( const TTimeValue& Time );
134
137 Timer( const TTimeValue& Time, const TTimeValue& RepeatTime );
138
141 void SAL_CALL start();
142
145 void SAL_CALL stop();
146
149 sal_Bool SAL_CALL isTicking() const;
150
153 sal_Bool SAL_CALL isExpired() const;
154
157 sal_Bool SAL_CALL expiresBefore( const Timer* pTimer ) const;
158
161 void SAL_CALL setAbsoluteTime( const TTimeValue& Time );
162
165 void SAL_CALL setRemainingTime( const TTimeValue& Remaining );
166
170 void SAL_CALL setRemainingTime( const TTimeValue& Remaining, const TTimeValue& Repeat );
171
174 void SAL_CALL addTime( const TTimeValue& Time );
175
178 TTimeValue SAL_CALL getRemainingTime() const;
179
182 static void SAL_CALL joinThread();
183
186 static void SAL_CALL startThread();
187
188protected:
189
192 virtual ~Timer() SAL_OVERRIDE;
193
196 virtual void SAL_CALL onShot() = 0;
197
198protected:
199
202 TTimeValue m_aTimeOut;
203
206 TTimeValue m_aExpired;
207
210 TTimeValue m_aRepeatDelta;
211
214 Timer* m_pNext;
215
216private:
217
220 Timer( const Timer& rTimer ) SAL_DELETED_FUNCTION;
221
224 void SAL_CALL operator=( const Timer& rTimer ) SAL_DELETED_FUNCTION;
225
226 friend class TimerManager;
227};
228
229}
230
231#endif // INCLUDED_SALHELPER_TIMER_HXX
232
233/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DELETED_FUNCTION
#define SAL_DELETED_FUNCTION
short-circuit extra-verbose API namespaces
Definition types.h:396
sal_Bool
unsigned char sal_Bool
Definition types.h:38
SAL_OVERRIDE
#define SAL_OVERRIDE
C++11 "override" feature.
Definition types.h:409
SAL_WARN_UNUSED
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition types.h:611
simplereferenceobject.hxx
salhelperdllapi.h
SALHELPER_DLLPUBLIC
#define SALHELPER_DLLPUBLIC
Definition salhelperdllapi.h:32
time.h
salhelper
Definition condition.hxx:34
salhelper::operator<
bool operator<(const TTimeValue &rTimeA, const TTimeValue &rTimeB)
Definition timer.hxx:93
salhelper::operator>
bool operator>(const TTimeValue &rTimeA, const TTimeValue &rTimeB)
Definition timer.hxx:103
salhelper::operator==
bool operator==(const TTimeValue &rTimeA, const TTimeValue &rTimeB)
Definition timer.hxx:113
salhelper::SimpleReferenceObject
A simple base implementation for reference-counted objects.
Definition simplereferenceobject.hxx:62
salhelper::TTimeValue
Helper class for easier manipulation with TimeValue.
Definition timer.hxx:39
salhelper::TTimeValue::addTime
void addTime(const TTimeValue &Delta)
Definition timer.hxx:79
salhelper::TTimeValue::TTimeValue
TTimeValue(const TimeValue &rTimeValue)
Definition timer.hxx:62
salhelper::TTimeValue::TTimeValue
TTimeValue(sal_uInt32 MilliSecs)
Definition timer.hxx:54
salhelper::TTimeValue::TTimeValue
TTimeValue(sal_uInt32 Secs, sal_uInt32 Nano)
Definition timer.hxx:46
salhelper::TTimeValue::isEmpty
bool isEmpty() const
Definition timer.hxx:87
salhelper::TTimeValue::normalize
void normalize()
Definition timer.hxx:70
salhelper::TTimeValue::TTimeValue
TTimeValue()
Definition timer.hxx:40
salhelper::Timer::start
void start()
Start timer.
salhelper::Timer::getRemainingTime
TTimeValue getRemainingTime() const
Returns the remaining time before timer expiration relative to now.
salhelper::Timer::Timer
Timer(const TTimeValue &Time)
Constructor.
salhelper::Timer::onShot
virtual void onShot()=0
What should be done when the 'timer fires'.
salhelper::Timer::setRemainingTime
void setRemainingTime(const TTimeValue &Remaining, const TTimeValue &Repeat)
Set the time to fire to 'now' + Remaining with repeat interveal Repeat.
salhelper::Timer::stop
void stop()
Abort timer prematurely.
salhelper::Timer::~Timer
virtual ~Timer() SAL_OVERRIDE
Destructor.
salhelper::Timer::joinThread
static void joinThread()
Internal method to shutdown the timer thread.
salhelper::Timer::Timer
Timer(const TTimeValue &Time, const TTimeValue &RepeatTime)
Constructor.
salhelper::Timer::m_aTimeOut
TTimeValue m_aTimeOut
holds (initial) expiration time of this timer.
Definition timer.hxx:202
salhelper::Timer::isExpired
sal_Bool isExpired() const
Is the timer expired?
salhelper::Timer::m_pNext
Timer * m_pNext
Pointer to the next timer (to fire).
Definition timer.hxx:214
salhelper::Timer::m_aExpired
TTimeValue m_aExpired
holds the time of expiration of this timer.
Definition timer.hxx:206
salhelper::Timer::TimerManager
friend class TimerManager
Definition timer.hxx:226
salhelper::Timer::addTime
void addTime(const TTimeValue &Time)
Adds Time to the 'fire time'.
salhelper::Timer::startThread
static void startThread()
Internal method to re-start the timer thread if necessary.
salhelper::Timer::expiresBefore
sal_Bool expiresBefore(const Timer *pTimer) const
Does pTimer expires before us?
salhelper::Timer::setAbsoluteTime
void setAbsoluteTime(const TTimeValue &Time)
Set the absolute time when the timer should fire.
salhelper::Timer::m_aRepeatDelta
TTimeValue m_aRepeatDelta
holds the time interveal of successive expirations.
Definition timer.hxx:210
salhelper::Timer::Timer
Timer()
Constructor.
salhelper::Timer::isTicking
sal_Bool isTicking() const
Returns sal_True if timer is running.
salhelper::Timer::setRemainingTime
void setRemainingTime(const TTimeValue &Remaining)
Set the time to fire to 'now' + Remaining.
TimeValue
Time since Jan-01-1970.
Definition time.h:76
TimeValue::Seconds
sal_uInt32 Seconds
Definition time.h:77
TimeValue::Nanosec
sal_uInt32 Nanosec
Definition time.h:78