Maze Builder Docs 7.5.6
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
8namespace mazes
9{
10
16 template <typename Time = std::chrono::microseconds, typename Clock = std::chrono::high_resolution_clock>
18 {
19 mutable std::mutex mtx;
20 typename Clock::time_point start_time;
21 typename Clock::time_point end_time;
22
23 public:
24 explicit progress() : start_time(Clock::now()), end_time(start_time) {}
25
33 template <typename F, typename... Args, typename Duration = Time>
34 static Duration duration(F &&f, Args &&...args)
35 {
36 progress p;
37 p.start();
38
39 auto result = std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
40
41 if (!result)
42 {
43 return Duration::zero();
44 }
45
46 auto duration = p.elapsed<Duration>();
47 return duration;
48 }
49
51 void start() noexcept
52 {
53 std::lock_guard<std::mutex> lock(this->mtx);
54 start_time = end_time = Clock::now();
55 }
56
58 void reset() noexcept
59 {
60 this->start();
61 }
62
66 template <typename T = double>
67 T elapsed() noexcept
68 {
69 std::lock_guard<std::mutex> lock(this->mtx);
70 end_time = Clock::now();
71 return static_cast<T>(std::chrono::duration_cast<Time>(end_time - start_time).count());
72 }
73 }; // progress
74} // namespace mazes
75
76#endif // PROGRESS_H
Command-line argument handler with JSON support.
Definition args.h:20
Simple clock for elapsed events.
Definition progress.h:18
void start() noexcept
Start the progress.
Definition progress.h:51
static Duration duration(F &&f, Args &&...args)
Definition progress.h:34
T elapsed() noexcept
Capture the elapsed time.
Definition progress.h:67
void reset() noexcept
Reset the progress.
Definition progress.h:58
Namespace for the maze builder.
Definition algo_interface.h:6