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