-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbasic_timer.h
More file actions
123 lines (107 loc) · 3.47 KB
/
basic_timer.h
File metadata and controls
123 lines (107 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright (C) 2020-2026 MEmilio
*
* Authors: Rene Schmieding
*
* Contact: Martin J. Kuehn <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MIO_TIMER_BASIC_TIMER_H
#define MIO_TIMER_BASIC_TIMER_H
#include "memilio/io/io.h"
#include "memilio/timer/definitions.h"
#include <string_view>
#include <utility>
namespace mio
{
namespace timing
{
/// @brief A minimal timer class.
class BasicTimer
{
public:
/// @brief Start the timer. Must be followed by exactly one stop.
void start()
{
should_be_running(false, "start");
set_running(true);
m_start_time = get_time_now();
}
/// @brief Stop the timer and update the elapsed time. After calling stop, the timer may be started again.
void stop()
{
should_be_running(true, "stop");
set_running(false);
const TimeType stop_time = get_time_now();
m_elapsed_time += stop_time - m_start_time;
}
/// @brief Set the elapsed time to 0. Only call while the timer is stopped.
void reset()
{
should_be_running(false, "reset");
m_elapsed_time = mio::timing::DurationType{0};
}
/// @brief Get the total time spent between starts and stops. Only call while the timer is stopped.
DurationType get_elapsed_time() const
{
should_be_running(false, "get_elapsed_time");
return m_elapsed_time;
}
~BasicTimer()
{
should_be_running(false, "~BasicTimer");
}
/**
* serialize this.
* @see mio::serialize
*/
template <class IOContext>
void serialize(IOContext& io) const
{
auto obj = io.create_object("BasicTimer");
obj.add_element("elapsed_time", details::convert_to_ticks(m_elapsed_time));
}
/**
* deserialize an object of this class.
* @see mio::deserialize
*/
template <class IOContext>
static IOResult<BasicTimer> deserialize(IOContext& io)
{
using Tick = decltype(details::convert_to_ticks(std::declval<DurationType>()));
auto obj = io.expect_object("BasicTimer");
auto et = obj.expect_element("elapsed_time", Tag<Tick>{});
return apply(
io,
[](auto&& et_) {
BasicTimer b;
b.m_elapsed_time = DurationType{et_};
return b;
},
et);
}
private:
TimeType m_start_time; ///< The last time point at which start() was called
DurationType m_elapsed_time{0}; ///< The total time spent between starts and stops.
#ifndef NDEBUG
bool m_is_running = false; ///< In Debug builds, tracks whether the timer is running.
#endif
/// @brief In Debug builds, set whether the timer is running or not.
void set_running(bool new_state);
/// @brief In Debug builds, check that the state of m_is_running is as expected. Log an error if not.
void should_be_running(bool expected, const std::string_view function) const;
};
} // namespace timing
} // namespace mio
#endif // MIO_TIMER_BASIC_TIMER_H