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