Maze Builder Docs 6.3.5
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
15template <typename Time = std::chrono::microseconds, typename Clock = std::chrono::high_resolution_clock>
16class progress {
17 mutable std::mutex mtx;
18 typename Clock::time_point start_time;
19 typename Clock::time_point end_time;
20public:
21 explicit progress() : start_time(Clock::now()), end_time(start_time) {}
22
30 template <typename F, typename... Args, typename Duration = Time>
31 static Duration duration(F&& f, Args&&... args) {
32 progress p;
33 p.start();
34
35 auto result = std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
36
37 if (!result) {
38 return Duration::zero();
39 }
40
41 auto duration = p.elapsed<Duration>();
42 return duration;
43 }
44
46 void start() noexcept {
47 std::lock_guard<std::mutex> lock(this->mtx);
48 start_time = end_time = Clock::now();
49 }
50
52 void reset() noexcept {
53 this->start();
54 }
55
59 template <typename T = double>
60 T elapsed() noexcept {
61 std::lock_guard<std::mutex> lock(this->mtx);
62 end_time = Clock::now();
63 return static_cast<T>(std::chrono::duration_cast<Time>(end_time - start_time).count());
64 }
65}; // progress
66} // namespace mazes
67
68#endif // PROGRESS_H
Simple clock for elapsed events.
Definition progress.h:16
T elapsed() noexcept
Capture the elapsed time.
Definition progress.h:60
static Duration duration(F &&f, Args &&... args)
Definition progress.h:31
Namespace for the maze builder.
Definition algo_interface.h:9
Simple argument handler.
Definition args.h:15