LibreOffice Module canvas (master) 1
elapsedtime.cxx
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#include <sal/config.h>
21
23
24#include <tools/time.hxx>
25#include <utility>
26
27namespace canvas::tools {
28
30{
31 return ::tools::Time::GetMonotonicTicks() / 1.0E6;
32}
33
35 : m_pTimeBase(),
36 m_fLastQueriedTime( 0.0 ),
37 m_fStartTime( getSystemTime() ),
38 m_fFrozenTime( 0.0 ),
39 m_bInPauseMode( false ),
40 m_bInHoldMode( false )
41{
42}
43
45 std::shared_ptr<ElapsedTime> pTimeBase )
46 : m_pTimeBase(std::move( pTimeBase )),
47 m_fLastQueriedTime( 0.0 ),
48 m_fStartTime( getCurrentTime() ),
49 m_fFrozenTime( 0.0 ),
50 m_bInPauseMode( false ),
51 m_bInHoldMode( false )
52{
53}
54
56{
59 m_fFrozenTime = 0.0;
60 m_bInPauseMode = false;
61 m_bInHoldMode = false;
62}
63
64void ElapsedTime::adjustTimer( double fOffset )
65{
66 // to make getElapsedTime() become _larger_, have to reduce
67 // m_fStartTime.
68 m_fStartTime -= fOffset;
69
70 // also adjust frozen time, this method must _always_ affect the
71 // value returned by getElapsedTime()!
73 m_fFrozenTime += fOffset;
74}
75
77{
78 return m_pTimeBase == nullptr ? getSystemTime() : m_pTimeBase->getElapsedTimeImpl();
79}
80
82{
84 return m_fLastQueriedTime;
85}
86
88{
90 return m_fFrozenTime;
91
93}
94
96{
98 m_bInPauseMode = true;
99}
100
102{
103 m_bInPauseMode = false;
104
105 // stop pausing, time runs again. Note that
106 // getElapsedTimeImpl() honors hold mode, i.e. a
107 // continueTimer() in hold mode will preserve the latter
108 const double fPauseDuration( getElapsedTimeImpl() - m_fFrozenTime );
109
110 // adjust start time, such that subsequent getElapsedTime() calls
111 // will virtually start from m_fFrozenTime.
112 m_fStartTime += fPauseDuration;
113}
114
116{
117 // when called during hold mode (e.g. more than once per time
118 // object), the original hold time will be maintained.
120 m_bInHoldMode = true;
121}
122
124{
125 m_bInHoldMode = false;
126}
127
128} // namespace
129
130/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void releaseTimer()
Releases a held timer.
double m_fFrozenTime
Instant, when last pause or hold started, relative to m_fStartTime.
void pauseTimer()
Pauses the running timer.
Definition: elapsedtime.cxx:95
double getElapsedTimeImpl() const
Definition: elapsedtime.cxx:87
void holdTimer()
Holds the current time.
double m_fStartTime
Start time, from which the difference to the time base is returned.
double m_fLastQueriedTime
To validate adjustTimer() calls with bLimitToLastQueriedTime=true.
void adjustTimer(double fOffset)
Adjusts the timer, hold and pause times.
Definition: elapsedtime.cxx:64
static double getSystemTime()
Definition: elapsedtime.cxx:29
ElapsedTime()
Create a new ElapsedTime object.
Definition: elapsedtime.cxx:34
void continueTimer()
Continues the paused timer.
const std::shared_ptr< ElapsedTime > m_pTimeBase
bool m_bInPauseMode
True, when in pause mode.
double getCurrentTime() const
Definition: elapsedtime.cxx:76
bool m_bInHoldMode
True, when in hold mode.
void reset()
Reset the time.
Definition: elapsedtime.cxx:55
double getElapsedTime() const
Query the elapsed time.
Definition: elapsedtime.cxx:81