LibreOffice Module vcl (master) 1
TaskStopwatch.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
10#ifndef INCLUDED_VCL_TASK_STOPWATCH_HXX
11#define INCLUDED_VCL_TASK_STOPWATCH_HXX
12
13#include <tools/time.hxx>
14#include <vcl/dllapi.h>
15#include <vcl/inputtypes.hxx>
16#include <vcl/svapp.hxx>
17
32{
33 static constexpr VclInputFlags eDefaultInputStop = VCL_INPUT_ANY & ~VclInputFlags::TIMER;
34 static constexpr unsigned int nDefaultTimeSlice = 50;
35 static unsigned int m_nTimeSlice;
36
37 sal_uInt64 m_nStartTicks;
41
42 bool nextIter()
43 {
44 sal_uInt64 nCurTicks = tools::Time::GetSystemTicks();
45 // handle system ticks wrap as exceeded time slice
46 if (nCurTicks < m_nStartTicks)
47 return false;
48
49 if (m_bConsiderLastIterTime)
50 {
51 // based on the last iter runtime, we don't expect to finish in time
52 // m_nTimeSlice < (nCurTicks - m_nStartTicks) + (nCurTicks - m_nIterStartTicks)
53 if (m_nTimeSlice < 2 * nCurTicks - m_nIterStartTicks - m_nStartTicks)
54 return false;
55 }
56 // time slice exceeded
57 else if (m_nTimeSlice < nCurTicks - m_nStartTicks)
58 return false;
59
60 m_nIterStartTicks = nCurTicks;
61
62 return !Application::AnyInput(m_eInputStop);
63 }
64
65public:
75 TaskStopwatch(bool bConciderLastIterTime = true)
76 : m_nStartTicks(tools::Time::GetSystemTicks())
77 , m_nIterStartTicks(m_nStartTicks)
78 , m_bConsiderLastIterTime(bConciderLastIterTime)
79 , m_eInputStop(eDefaultInputStop)
80 {
81 }
82
86 bool continueIter() { return nextIter(); }
87
91 void reset()
92 {
93 m_nStartTicks = tools::Time::GetSystemTicks();
94 m_nIterStartTicks = m_nStartTicks;
95 }
96
102 void setInputStop(VclInputFlags eInputStop = eDefaultInputStop) { m_eInputStop = eInputStop; }
103 VclInputFlags inputStop() const { return m_eInputStop; }
104
111 static unsigned int timeSlice() { return m_nTimeSlice; }
112 static void setTimeSlice(unsigned int nTimeSlice) { m_nTimeSlice = nTimeSlice; }
113};
114
115#endif // INCLUDED_VCL_TASK_STOPWATCH_HXX
116
117/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool AnyInput(VclInputFlags nType=VCL_INPUT_ANY)
Determine if there are any pending input events.
Definition: svapp.cxx:623
Helper class primary used to track time of long running iterating tasks.
VclInputFlags inputStop() const
bool continueIter()
Returns true, if another iteration will probably pass in the time slot.
void reset()
Reset the stopwatch.
bool m_bConsiderLastIterTime
static unsigned int m_nTimeSlice
VclInputFlags m_eInputStop
void setInputStop(VclInputFlags eInputStop=eDefaultInputStop)
Sets the input events, which should also "exceed" the stopwatch.
static unsigned int timeSlice()
Sets the time considered the acceptable maximum for a task to run.
sal_uInt64 m_nIterStartTicks
sal_uInt64 m_nStartTicks
TaskStopwatch(bool bConciderLastIterTime=true)
Per default the watch considers the last iter time when asking for an other iteration,...
static void setTimeSlice(unsigned int nTimeSlice)
static sal_uInt64 GetSystemTicks()
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
VclInputFlags
Definition: inputtypes.hxx:25
#define VCL_INPUT_ANY
Definition: inputtypes.hxx:39