Maze Builder Docs 8.2.1
Loading...
Searching...
No Matches
progress.h
Go to the documentation of this file.
1#ifndef PROGRESS_H
2#define PROGRESS_H
3
4#include <chrono>
5#include <mutex>
6#include <utility>
7
10namespace mazes
11{
16 template <typename Time = std::chrono::microseconds, typename Clock = std::chrono::high_resolution_clock>
18 {
19 mutable std::mutex mtx;
20 Clock::time_point start_time;
21 Clock::time_point end_time;
22
23 public:
24 explicit progress() : start_time(Clock::now()), end_time(start_time)
25 {
26 }
27
36 template <typename F, typename... Args, typename Duration = Time>
37 static Duration duration(F&& f, Args&&... args)
38 {
39 progress p;
40 p.start();
41
42 if constexpr (std::is_void_v<std::invoke_result_t<F, Args...>>)
43 {
44 std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
45 }
46 else
47 {
48 auto result = std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
49 if (!result)
50 {
51 return Duration::zero();
52 }
53 }
54
55 auto duration = p.elapsed<Duration>();
56 return duration;
57 }
58
60 void start() noexcept
61 {
62 std::lock_guard<std::mutex> lock(this->mtx);
63 start_time = end_time = Clock::now();
64 }
65
67 void reset() noexcept
68 {
69 this->start();
70 }
71
75 template <typename T = double>
76 T elapsed() noexcept
77 {
78 std::lock_guard<std::mutex> lock(this->mtx);
79 end_time = Clock::now();
80 return static_cast<T>(std::chrono::duration_cast<Time>(end_time - start_time).count());
81 }
82 }; // progress
83} // namespace mazes
84
85#endif // PROGRESS_H
Command-line argument handler with JSON support.
Definition args.h:19
Simple clock for elapsed events.
Definition progress.h:18
void start() noexcept
Start the progress.
Definition progress.h:60
T elapsed() noexcept
Capture the elapsed time.
Definition progress.h:76
static Duration duration(F &&f, Args &&... args)
Measures the duration of a callable object.
Definition progress.h:37
void reset() noexcept
Reset the progress.
Definition progress.h:67