Libmagicxx v9.0.2
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 {
14
15namespace utility {
16
23public:
29 explicit progress_tracker(std::uint64_t total_steps = 1u) noexcept
30 : m_total_steps{std::max<std::uint64_t>(total_steps, 1u)}
31 { }
32
38 void advance(std::uint64_t step_count = 1u) noexcept
39 {
40 std::lock_guard lock{m_mutex};
41 m_completed_steps = std::min(
42 m_completed_steps + step_count,
44 );
45 m_condition_variable.notify_all();
46 }
47
51 [[nodiscard]] std::uint64_t get_completed_steps() const noexcept
52 {
53 std::lock_guard lock{m_mutex};
54 return m_completed_steps;
55 }
56
60 [[nodiscard]] percentage get_completion_percentage() const noexcept
61 {
62 std::lock_guard lock{m_mutex};
64 }
65
69 [[nodiscard]] std::uint64_t get_incompleted_steps() const noexcept
70 {
71 std::lock_guard lock{m_mutex};
73 }
74
78 [[nodiscard]] std::uint64_t get_total_steps() const noexcept
79 {
80 std::lock_guard lock{m_mutex};
81 return m_total_steps;
82 }
83
87 [[nodiscard]] bool is_completed() const noexcept
88 {
89 std::lock_guard lock{m_mutex};
91 }
92
96 void mark_as_completed() noexcept
97 {
98 std::lock_guard lock{m_mutex};
100 m_condition_variable.notify_all();
101 }
102
108 void reset(std::uint64_t total_steps) noexcept
109 {
110 std::lock_guard lock{m_mutex};
111 m_total_steps = std::max<std::uint64_t>(total_steps, 1u);
113 m_condition_variable.notify_all();
114 }
115
126 template <typename RepresentationType, typename PeriodType>
127 [[nodiscard]] bool try_wait_for_completion(
128 std::chrono::duration<RepresentationType, PeriodType> timeout
129 ) const
130 {
131 std::unique_lock lock{m_mutex};
132 return m_condition_variable.wait_for(lock, timeout, [this] {
134 });
135 }
136
147 template <typename ClockType, typename DurationType>
149 std::chrono::time_point<ClockType, DurationType> deadline
150 ) const
151 {
152 std::unique_lock lock{m_mutex};
153 return m_condition_variable.wait_until(lock, deadline, [this] {
155 });
156 }
157
162 {
163 std::unique_lock lock{m_mutex};
164 m_condition_variable.wait(lock, [this] {
166 });
167 }
168
169private:
170 mutable std::mutex m_mutex{};
171 mutable std::condition_variable m_condition_variable{};
172 std::uint64_t m_total_steps;
173 std::uint64_t m_completed_steps{0u};
174};
175
181using shared_progress_tracker_t = std::shared_ptr<progress_tracker>;
182
191 std::uint64_t total_steps = 1u
192) noexcept
193{
194 return std::make_shared<progress_tracker>(total_steps);
195}
196
203public:
210 ) noexcept
211 : m_shared_tracker(shared_tracker)
212 { }
213
218 {
219 if (!m_shared_tracker) {
220 return;
221 }
222 m_shared_tracker->mark_as_completed();
223 }
224
225private:
227};
228
235public:
243 shared_progress_tracker_t shared_tracker,
244 std::uint64_t step_count = 1u
245 ) noexcept
246 : m_shared_tracker(shared_tracker)
247 , m_step_count(step_count)
248 { }
249
254 {
255 if (!m_shared_tracker) {
256 return;
257 }
259 }
260
261private:
263 std::uint64_t m_step_count;
264};
265
266} /* namespace utility */
267
268} /* namespace recognition */
269
270#endif /* PROGRESS_TRACKER_HPP */
RAII helper that advances a shared progress_tracker by a given step count upon destruction.
Definition progress_tracker.hpp:234
~advance_tracker()
Advance the tracker by the step count if it exists.
Definition progress_tracker.hpp:253
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:242
shared_progress_tracker_t m_shared_tracker
Definition progress_tracker.hpp:262
std::uint64_t m_step_count
Definition progress_tracker.hpp:263
RAII helper that marks a shared progress_tracker as completed upon destruction.
Definition progress_tracker.hpp:202
shared_progress_tracker_t m_shared_tracker
Definition progress_tracker.hpp:226
~mark_tracker_as_completed()
Mark the tracker as completed if it exists.
Definition progress_tracker.hpp:217
mark_tracker_as_completed(shared_progress_tracker_t shared_tracker) noexcept
Construct mark_tracker_as_completed.
Definition progress_tracker.hpp:209
Represents a value in the range [0, 100].
Definition percentage.hpp:20
Thread-safe tracker for monitoring the progress of a job composed of multiple steps.
Definition progress_tracker.hpp:22
std::uint64_t m_completed_steps
Definition progress_tracker.hpp:173
void advance(std::uint64_t step_count=1u) noexcept
Increments the number of completed steps.
Definition progress_tracker.hpp:38
std::uint64_t m_total_steps
Definition progress_tracker.hpp:172
std::uint64_t get_total_steps() const noexcept
Definition progress_tracker.hpp:78
std::mutex m_mutex
Definition progress_tracker.hpp:170
bool is_completed() const noexcept
Definition progress_tracker.hpp:87
percentage get_completion_percentage() const noexcept
Definition progress_tracker.hpp:60
std::condition_variable m_condition_variable
Definition progress_tracker.hpp:171
std::uint64_t get_completed_steps() const noexcept
Definition progress_tracker.hpp:51
progress_tracker(std::uint64_t total_steps=1u) noexcept
Construct progress_tracker with a given total number of steps.
Definition progress_tracker.hpp:29
std::uint64_t get_incompleted_steps() const noexcept
Definition progress_tracker.hpp:69
void wait_for_completion() const
Wait until all steps are completed.
Definition progress_tracker.hpp:161
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:148
void reset(std::uint64_t total_steps) noexcept
Reset the tracker with a new total step count.
Definition progress_tracker.hpp:108
void mark_as_completed() noexcept
Marks the job as fully completed.
Definition progress_tracker.hpp:96
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:127
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:190
std::shared_ptr< progress_tracker > shared_progress_tracker_t
Alias for a shared pointer to progress_tracker.
Definition progress_tracker.hpp:181
Definition magic.hpp:19