The Battle for Wesnoth  1.17.0-dev
optimer.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2020 - 2021
3  by Iris Morelle <shadowm2006@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include <chrono>
19 #include <functional>
20 #include <iosfwd>
21 
22 namespace util {
23 
24 /**
25  * Reports time elapsed at the end of an object scope.
26  *
27  * The constructor accepts a callable that takes the optimer as its only
28  * argument, which can then be used to write the elapsed time to a stream,
29  * variable, or anything else that's desirable for a particular use case.
30  *
31  * @tparam ResolutionType Clock resolution -- milliseconds, microseconds, etc.
32  * @tparam ClockType Clock type -- a monotonic clock by default.
33  */
34 template<typename ResolutionType, typename ClockType = std::chrono::steady_clock>
35 struct optimer
36 {
37  using clock = ClockType;
38  using resolution = ResolutionType;
39  using point = typename clock::time_point;
40  using interval = typename clock::duration;
41  using report_callback = std::function<void(const optimer&)>;
42 
43  /**
44  * Constructor, which starts the timer.
45  *
46  * The report callback may be an empty object, in which case the destructor
47  * will not do anything. This may be useful in order to obtain and keep
48  * track of elapsed time manually for other purposes than console printing.
49  */
51  : start_()
52  , repf_(f)
53  {
54  reset();
55  }
56 
57  /**
58  * Destructor, which invokes the report callback.
59  */
61  {
62  if(repf_) {
63  repf_(*this);
64  }
65  }
66 
67  /** Resets the timer back to zero. */
68  void reset()
69  {
70  start_ = clock::now();
71  }
72 
73  /** Returns the start time point. */
74  point start() const
75  {
76  return start_;
77  }
78 
79  /** Returns the elapsed time value. */
80  interval elapsed() const
81  {
82  return clock::now() - start_;
83  }
84 
85 private:
88 };
89 
90 /**
91  * Formats time elapsed for writing to a stream.
92  *
93  * @note The resulting output does <b>not</b> include a time unit suffix.
94  */
95 template<typename OpTimerType>
96 inline std::ostream& operator<<(std::ostream& o, const OpTimerType& tm)
97 {
98  o << std::chrono::duration_cast<typename OpTimerType::resolution>(tm.elapsed()).count();
99  return o;
100 }
101 
102 /**
103  * Time elapsed with millisecond resolution.
104  */
106 
107 } // end namespace util
point start() const
Returns the start time point.
Definition: optimer.hpp:74
typename clock::duration interval
Definition: optimer.hpp:40
Definition: optimer.hpp:22
ClockType clock
Definition: optimer.hpp:37
optimer(report_callback f=report_callback{})
Constructor, which starts the timer.
Definition: optimer.hpp:50
Reports time elapsed at the end of an object scope.
Definition: optimer.hpp:35
std::ostream & operator<<(std::ostream &o, const OpTimerType &tm)
Formats time elapsed for writing to a stream.
Definition: optimer.hpp:96
~optimer()
Destructor, which invokes the report callback.
Definition: optimer.hpp:60
interval elapsed() const
Returns the elapsed time value.
Definition: optimer.hpp:80
report_callback repf_
Definition: optimer.hpp:87
std::function< void(const optimer &)> report_callback
Definition: optimer.hpp:41
Holds a 2D point.
Definition: point.hpp:24
#define f
void reset()
Resets the timer back to zero.
Definition: optimer.hpp:68
point start_
Definition: optimer.hpp:86
ResolutionType resolution
Definition: optimer.hpp:38