Libmagicxx v10.0.3
A modern C++23 wrapper for libmagic — the library that powers the Unix file command.
Loading...
Searching...
No Matches
progress_tracker.hpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Copyright (c) 2022-2026 Oğuz Toraman <oguz.toraman@tutanota.com> */
2/* SPDX-License-Identifier: LGPL-3.0-only */
3
52
53#ifndef PROGRESS_TRACKER_HPP
54#define PROGRESS_TRACKER_HPP
55
56#include <chrono>
57#include <condition_variable>
58#include <mutex>
59
60#include "percentage.hpp"
61
62namespace Recognition {
63namespace Utility {
113public:
127 explicit ProgressTracker(std::uint64_t total_steps = 1u) noexcept
128 : m_total_steps{std::max<std::uint64_t>(total_steps, 1u)}
129 { }
130
149 void Advance(std::uint64_t step_count = 1u) noexcept
150 {
151 std::lock_guard lock{m_mutex};
152 m_completed_steps = std::min(
153 m_completed_steps + step_count,
155 );
156 m_condition_variable.notify_all();
157 }
158
168 [[nodiscard]] std::uint64_t GetCompletedSteps() const noexcept
169 {
170 std::lock_guard lock{m_mutex};
171 return m_completed_steps;
172 }
173
190 [[nodiscard]] Percentage GetCompletionPercentage() const noexcept
191 {
192 std::lock_guard lock{m_mutex};
194 }
195
205 [[nodiscard]] std::uint64_t GetIncompletedSteps() const noexcept
206 {
207 std::lock_guard lock{m_mutex};
209 }
210
220 [[nodiscard]] std::uint64_t GetTotalSteps() const noexcept
221 {
222 std::lock_guard lock{m_mutex};
223 return m_total_steps;
224 }
225
235 [[nodiscard]] bool IsCompleted() const noexcept
236 {
237 std::lock_guard lock{m_mutex};
239 }
240
257 void MarkAsCompleted() noexcept
258 {
259 std::lock_guard lock{m_mutex};
261 m_condition_variable.notify_all();
262 }
263
279 void Reset(std::uint64_t total_steps) noexcept
280 {
281 std::lock_guard lock{m_mutex};
282 m_total_steps = std::max<std::uint64_t>(total_steps, 1u);
284 m_condition_variable.notify_all();
285 }
286
311 template <typename RepresentationT, typename PeriodT>
312 [[nodiscard]] bool TryWaitForCompletion(
313 std::chrono::duration<RepresentationT, PeriodT> timeout
314 ) const
315 {
316 std::unique_lock lock{m_mutex};
317 return m_condition_variable.wait_for(lock, timeout, [this] {
319 });
320 }
321
345 template <typename ClockT, typename DurationT>
346 [[nodiscard]] bool TryWaitForCompletionUntil(
347 std::chrono::time_point<ClockT, DurationT> deadline
348 ) const
349 {
350 std::unique_lock lock{m_mutex};
351 return m_condition_variable.wait_until(lock, deadline, [this] {
353 });
354 }
355
375 void WaitForCompletion() const
376 {
377 std::unique_lock lock{m_mutex};
378 m_condition_variable.wait(lock, [this] {
380 });
381 }
382
383private:
384 mutable std::mutex m_mutex{};
385 mutable std::condition_variable
387 std::uint64_t m_total_steps;
388 std::uint64_t m_completed_steps{0u};
389};
390
405using SharedProgressTrackerT = std::shared_ptr<ProgressTracker>;
406
441 std::uint64_t total_steps = 1u
442) noexcept
443{
444 return std::make_shared<ProgressTracker>(total_steps);
445}
446
474public:
484 SharedProgressTrackerT shared_progress_tracker
485 ) noexcept
486 : m_shared_progress_tracker(shared_progress_tracker)
487 { }
488
497 {
499 return;
500 }
501 m_shared_progress_tracker->MarkAsCompleted();
502 }
503
504private:
507};
508
536public:
547 SharedProgressTrackerT shared_progress_tracker,
548 std::uint64_t step_count = 1u
549 ) noexcept
550 : m_shared_progress_tracker(shared_progress_tracker)
551 , m_step_count(step_count)
552 { }
553
562 {
564 return;
565 }
567 }
568
569private:
572 std::uint64_t m_step_count;
573};
574} /* namespace Utility */
575} /* namespace Recognition */
576
577#endif /* PROGRESS_TRACKER_HPP */
~AdvanceTracker()
Destructor that advances the tracker.
Definition progress_tracker.hpp:561
std::uint64_t m_step_count
Definition progress_tracker.hpp:572
SharedProgressTrackerT m_shared_progress_tracker
Definition progress_tracker.hpp:571
AdvanceTracker(SharedProgressTrackerT shared_progress_tracker, std::uint64_t step_count=1u) noexcept
Construct with a shared progress tracker and step count.
Definition progress_tracker.hpp:546
~MarkTrackerAsCompleted()
Destructor that marks the tracker as completed.
Definition progress_tracker.hpp:496
SharedProgressTrackerT m_shared_progress_tracker
Definition progress_tracker.hpp:506
MarkTrackerAsCompleted(SharedProgressTrackerT shared_progress_tracker) noexcept
Construct with a shared progress tracker.
Definition progress_tracker.hpp:483
A type-safe percentage value in the range [0, 100].
Definition percentage.hpp:81
void Reset(std::uint64_t total_steps) noexcept
Reset the tracker for a new job.
Definition progress_tracker.hpp:279
std::uint64_t GetCompletedSteps() const noexcept
Get the number of completed steps.
Definition progress_tracker.hpp:168
void Advance(std::uint64_t step_count=1u) noexcept
Advance the progress by a number of completed steps.
Definition progress_tracker.hpp:149
void WaitForCompletion() const
Wait indefinitely for the job to complete.
Definition progress_tracker.hpp:375
ProgressTracker(std::uint64_t total_steps=1u) noexcept
Construct a ProgressTracker with a given number of total steps.
Definition progress_tracker.hpp:127
std::condition_variable m_condition_variable
Definition progress_tracker.hpp:386
bool IsCompleted() const noexcept
Check if the job is complete.
Definition progress_tracker.hpp:235
std::uint64_t m_completed_steps
Definition progress_tracker.hpp:388
bool TryWaitForCompletion(std::chrono::duration< RepresentationT, PeriodT > timeout) const
Wait for completion with a timeout.
Definition progress_tracker.hpp:312
std::uint64_t GetTotalSteps() const noexcept
Get the total number of steps.
Definition progress_tracker.hpp:220
std::mutex m_mutex
Definition progress_tracker.hpp:384
void MarkAsCompleted() noexcept
Mark the job as fully completed.
Definition progress_tracker.hpp:257
std::uint64_t GetIncompletedSteps() const noexcept
Get the number of remaining (incomplete) steps.
Definition progress_tracker.hpp:205
Percentage GetCompletionPercentage() const noexcept
Get the completion percentage.
Definition progress_tracker.hpp:190
bool TryWaitForCompletionUntil(std::chrono::time_point< ClockT, DurationT > deadline) const
Wait for completion until a deadline.
Definition progress_tracker.hpp:346
std::uint64_t m_total_steps
Definition progress_tracker.hpp:387
SharedProgressTrackerT MakeSharedProgressTracker(std::uint64_t total_steps=1u) noexcept
Factory function to create a shared ProgressTracker.
Definition progress_tracker.hpp:440
std::shared_ptr< ProgressTracker > SharedProgressTrackerT
Shared pointer type for ProgressTracker.
Definition progress_tracker.hpp:405
Utility components for the libmagicxx library.
Definition percentage.hpp:39
Root namespace for the libmagicxx library.
Percentage value type for progress tracking.