basic_timer.h Source File

CPP API: basic_timer.h Source File
basic_timer.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Rene Schmieding
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #ifndef MIO_TIMER_BASIC_TIMER_H
21 #define MIO_TIMER_BASIC_TIMER_H
22 
23 #include "memilio/io/io.h"
25 
26 #include <string_view>
27 #include <utility>
28 
29 namespace mio
30 {
31 namespace timing
32 {
33 
36 {
37 public:
39  void start()
40  {
41  should_be_running(false, "start");
42  set_running(true);
44  }
45 
47  void stop()
48  {
49  should_be_running(true, "stop");
50  set_running(false);
51  const TimeType stop_time = get_time_now();
52  m_elapsed_time += stop_time - m_start_time;
53  }
54 
56  void reset()
57  {
58  should_be_running(false, "reset");
60  }
61 
64  {
65  should_be_running(false, "get_elapsed_time");
66  return m_elapsed_time;
67  }
68 
70  {
71  should_be_running(false, "~BasicTimer");
72  }
73 
78  template <class IOContext>
79  void serialize(IOContext& io) const
80  {
81  auto obj = io.create_object("BasicTimer");
82  obj.add_element("elapsed_time", details::convert_to_ticks(m_elapsed_time));
83  }
84 
89  template <class IOContext>
90  static IOResult<BasicTimer> deserialize(IOContext& io)
91  {
92  using Tick = decltype(details::convert_to_ticks(std::declval<DurationType>()));
93  auto obj = io.expect_object("BasicTimer");
94  auto et = obj.expect_element("elapsed_time", Tag<Tick>{});
95  return apply(
96  io,
97  [](auto&& et_) {
98  BasicTimer b;
100  return b;
101  },
102  et);
103  }
104 
105 private:
108 
109 #ifndef NDEBUG
110  bool m_is_running = false;
111 #endif
112 
114  void set_running(bool new_state);
115 
117  void should_be_running(bool expected, const std::string_view function) const;
118 };
119 
120 } // namespace timing
121 } // namespace mio
122 
123 #endif // MIO_TIMER_BASIC_TIMER_H
A minimal timer class.
Definition: basic_timer.h:36
void reset()
Set the elapsed time to 0. Only call while the timer is stopped.
Definition: basic_timer.h:56
void stop()
Stop the timer and update the elapsed time. After calling stop, the timer may be started again.
Definition: basic_timer.h:47
void set_running(bool new_state)
In Debug builds, set whether the timer is running or not.
Definition: basic_timer.cpp:9
static IOResult< BasicTimer > deserialize(IOContext &io)
deserialize an object of this class.
Definition: basic_timer.h:90
bool m_is_running
In Debug builds, tracks whether the timer is running.
Definition: basic_timer.h:110
void should_be_running(bool expected, const std::string_view function) const
In Debug builds, check that the state of m_is_running is as expected. Log an error if not.
Definition: basic_timer.cpp:18
DurationType m_elapsed_time
The total time spent between starts and stops.
Definition: basic_timer.h:107
DurationType get_elapsed_time() const
Get the total time spent between starts and stops. Only call while the timer is stopped.
Definition: basic_timer.h:63
~BasicTimer()
Definition: basic_timer.h:69
void start()
Start the timer. Must be followed by exactly one stop.
Definition: basic_timer.h:39
TimeType m_start_time
The last time point at which start() was called.
Definition: basic_timer.h:106
void serialize(IOContext &io) const
serialize this.
Definition: basic_timer.h:79
decltype(auto) convert_to_ticks(DurationType duration)
Convert a duration to integer ticks. Useful for serialization.
Definition: definitions.h:49
std::chrono::steady_clock::time_point TimeType
Definition: definitions.h:40
TimeType get_time_now()
Get the current time.
Definition: definitions.cpp:46
std::chrono::steady_clock::duration DurationType
Definition: definitions.h:41
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
boost::outcome_v2::in_place_type_t< T > Tag
Type that is used for overload resolution.
Definition: io.h:407
details::ApplyResultT< F, T... > apply(IOContext &io, F f, const IOResult< T > &... rs)
Evaluate a function with zero or more unpacked IOResults as arguments.
Definition: io.h:481
boost::outcome_v2::unchecked< T, IOStatus > IOResult
Value-or-error type for operations that return a value but can fail.
Definition: io.h:353