Libmagicxx v9.1.1
A C++ wrapper library over the Magic Number Recognition Library.
Loading...
Searching...
No Matches
progress_tracker.hpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Copyright (c) 2022-2025 Oğuz Toraman <oguz.toraman@tutanota.com> */
2/* SPDX-License-Identifier: LGPL-3.0-only */
3
4#ifndef PROGRESS_TRACKER_HPP
5#define PROGRESS_TRACKER_HPP
6
7#include <chrono>
8#include <condition_variable>
9#include <mutex>
10
11#include "percentage.hpp"
12
13namespace recognition {
14namespace utility {
21public:
27 explicit progress_tracker(std::uint64_t total_steps = 1u) noexcept
28 : m_total_steps{std::max<std::uint64_t>(total_steps, 1u)}
29 { }
30
36 void advance(std::uint64_t step_count = 1u) noexcept
37 {
38 std::lock_guard lock{m_mutex};
39 m_completed_steps = std::min(
40 m_completed_steps + step_count,
42 );
43 m_condition_variable.notify_all();
44 }
45
49 [[nodiscard]] std::uint64_t get_completed_steps() const noexcept
50 {
51 std::lock_guard lock{m_mutex};
52 return m_completed_steps;
53 }
54
58 [[nodiscard]] percentage get_completion_percentage() const noexcept
59 {
60 std::lock_guard lock{m_mutex};
62 }
63
67 [[nodiscard]] std::uint64_t get_incompleted_steps() const noexcept
68 {
69 std::lock_guard lock{m_mutex};
71 }
72
76 [[nodiscard]] std::uint64_t get_total_steps() const noexcept
77 {
78 std::lock_guard lock{m_mutex};
79 return m_total_steps;
80 }
81
85 [[nodiscard]] bool is_completed() const noexcept
86 {
87 std::lock_guard lock{m_mutex};
89 }
90
94 void mark_as_completed() noexcept
95 {
96 std::lock_guard lock{m_mutex};
98 m_condition_variable.notify_all();
99 }
100
106 void reset(std::uint64_t total_steps) noexcept
107 {
108 std::lock_guard lock{m_mutex};
109 m_total_steps = std::max<std::uint64_t>(total_steps, 1u);
111 m_condition_variable.notify_all();
112 }
113
124 template <typename RepresentationType, typename PeriodType>
125 [[nodiscard]] bool try_wait_for_completion(
126 std::chrono::duration<RepresentationType, PeriodType> timeout
127 ) const
128 {
129 std::unique_lock lock{m_mutex};
130 return m_condition_variable.wait_for(lock, timeout, [this] {
132 });
133 }
134
145 template <typename ClockType, typename DurationType>
147 std::chrono::time_point<ClockType, DurationType> deadline
148 ) const
149 {
150 std::unique_lock lock{m_mutex};
151 return m_condition_variable.wait_until(lock, deadline, [this] {
153 });
154 }
155
160 {
161 std::unique_lock lock{m_mutex};
162 m_condition_variable.wait(lock, [this] {
164 });
165 }
166
167private:
168 mutable std::mutex m_mutex{};
169 mutable std::condition_variable m_condition_variable{};
170 std::uint64_t m_total_steps;
171 std::uint64_t m_completed_steps{0u};
172};
173
179using shared_progress_tracker_t = std::shared_ptr<progress_tracker>;
180
189 std::uint64_t total_steps = 1u
190) noexcept
191{
192 return std::make_shared<progress_tracker>(total_steps);
193}
194
201public:
208 shared_progress_tracker_t shared_tracker
209 ) noexcept
210 : m_shared_tracker(shared_tracker)
211 { }
212
217 {
218 if (!m_shared_tracker) {
219 return;
220 }
221 m_shared_tracker->mark_as_completed();
222 }
223
224private:
226};
227
234public:
242 shared_progress_tracker_t shared_tracker,
243 std::uint64_t step_count = 1u
244 ) noexcept
245 : m_shared_tracker(shared_tracker)
246 , m_step_count(step_count)
247 { }
248
253 {
254 if (!m_shared_tracker) {
255 return;
256 }
258 }
259
260private:
262 std::uint64_t m_step_count;
263};
264} /* namespace utility */
265} /* namespace recognition */
266
267#endif /* PROGRESS_TRACKER_HPP */
~advance_tracker()
Advance the tracker by the step count if it exists.
Definition progress_tracker.hpp:252
advance_tracker(shared_progress_tracker_t shared_tracker, std::uint64_t step_count=1u) noexcept
Construct advance_tracker with a shared tracker and step count.
Definition progress_tracker.hpp:241
shared_progress_tracker_t m_shared_tracker
Definition progress_tracker.hpp:261
std::uint64_t m_step_count
Definition progress_tracker.hpp:262
shared_progress_tracker_t m_shared_tracker
Definition progress_tracker.hpp:225
~mark_tracker_as_completed()
Mark the tracker as completed if it exists.
Definition progress_tracker.hpp:216
mark_tracker_as_completed(shared_progress_tracker_t shared_tracker) noexcept
Construct mark_tracker_as_completed.
Definition progress_tracker.hpp:207
Represents a value in the range [0, 100].
Definition percentage.hpp:18
std::uint64_t m_completed_steps
Definition progress_tracker.hpp:171
void advance(std::uint64_t step_count=1u) noexcept
Increments the number of completed steps.
Definition progress_tracker.hpp:36
std::uint64_t m_total_steps
Definition progress_tracker.hpp:170
std::uint64_t get_total_steps() const noexcept
Definition progress_tracker.hpp:76
std::mutex m_mutex
Definition progress_tracker.hpp:168
bool is_completed() const noexcept
Definition progress_tracker.hpp:85
percentage get_completion_percentage() const noexcept
Definition progress_tracker.hpp:58
std::condition_variable m_condition_variable
Definition progress_tracker.hpp:169
std::uint64_t get_completed_steps() const noexcept
Definition progress_tracker.hpp:49
progress_tracker(std::uint64_t total_steps=1u) noexcept
Construct progress_tracker with a given total number of steps.
Definition progress_tracker.hpp:27
std::uint64_t get_incompleted_steps() const noexcept
Definition progress_tracker.hpp:67
void wait_for_completion() const
Wait until all steps are completed.
Definition progress_tracker.hpp:159
bool try_wait_for_completion_until(std::chrono::time_point< ClockType, DurationType > deadline) const
Wait until the job is complete or the specified deadline is reached.
Definition progress_tracker.hpp:146
void reset(std::uint64_t total_steps) noexcept
Reset the tracker with a new total step count.
Definition progress_tracker.hpp:106
void mark_as_completed() noexcept
Marks the job as fully completed.
Definition progress_tracker.hpp:94
bool try_wait_for_completion(std::chrono::duration< RepresentationType, PeriodType > timeout) const
Wait for the job to complete, or until the specified timeout duration elapses.
Definition progress_tracker.hpp:125
Definition percentage.hpp:12
shared_progress_tracker_t make_shared_progress_tracker(std::uint64_t total_steps=1u) noexcept
Create a shared pointer to a progress_tracker.
Definition progress_tracker.hpp:188
std::shared_ptr< progress_tracker > shared_progress_tracker_t
Alias for a shared pointer to progress_tracker.
Definition progress_tracker.hpp:179
Definition magic.hpp:19