Lightweight 0.20261031.0
Loading...
Searching...
No Matches
ProgressTask.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include <cstdint>
6#include <memory>
7#include <string_view>
8
9namespace Lightweight
10{
11
12/// @brief Progress of one task, which the library reports and a host (a CLI, a GUI) renders.
13///
14/// Tasks nest: a task opens sub-tasks for its parts, and destroying a task marks it finished. A
15/// total of 0 makes a task indeterminate: the host shows that it runs and for how long, not how
16/// far it got. The host implements this interface and the library only calls it, possibly from a
17/// worker thread, one task tree at a time. New virtual functions are only ever appended at the end.
19{
20 public:
21 /// Severity of a message reported against a task.
22 enum class LogLevel : std::uint8_t
23 {
24 Info,
25 Warning,
26 Error,
27 };
28
29 ProgressTask() = default;
30 ProgressTask(ProgressTask const&) = delete;
31 ProgressTask(ProgressTask&&) = delete;
32 ProgressTask& operator=(ProgressTask const&) = delete;
33 ProgressTask& operator=(ProgressTask&&) = delete;
34
35 /// Marks the task finished.
36 virtual ~ProgressTask() = default;
37
38 /// Opens a child task.
39 /// @param title What the child does, e.g. "Unit 2 of 5".
40 /// @param total Units of work of the child; 0 = indeterminate.
41 /// @return The child; destroying it finishes it.
42 [[nodiscard]] virtual std::unique_ptr<ProgressTask> BeginSubTask(std::string_view title, std::uint64_t total) = 0;
43
44 /// Replaces the task's total.
45 /// @param total Units of work; 0 = indeterminate.
46 virtual void SetTotal(std::uint64_t total) = 0;
47
48 /// Records more work done and names what runs now. `Advance(0, detail)` only changes the detail.
49 /// @param count Units of work done since the last call.
50 /// @param detail What the task does now; empty keeps the previous detail.
51 virtual void Advance(std::uint64_t count = 1, std::string_view detail = {}) = 0;
52
53 /// Reports a message about the task.
54 /// @param level The message's severity.
55 /// @param message The message, UTF-8.
56 virtual void Log(LogLevel level, std::string_view message) = 0;
57
58 /// @return True once the user asked to cancel; callers stop at their next safe point.
59 [[nodiscard]] virtual bool IsCancellationRequested() const = 0;
60
61 /// Marks the task failed; destroying it then ends it as failed, not as done.
62 /// @param reason What went wrong, UTF-8.
63 virtual void Fail(std::string_view reason) = 0;
64};
65
66/// @brief A ProgressTask that reports nothing and is never cancelled; used where a host passes none.
67class NullProgressTask final: public ProgressTask
68{
69 public:
70 /// @return Another NullProgressTask.
71 [[nodiscard]] std::unique_ptr<ProgressTask> BeginSubTask(std::string_view /*title*/, std::uint64_t /*total*/) override
72 {
73 return std::make_unique<NullProgressTask>();
74 }
75
76 /// Does nothing.
77 void SetTotal(std::uint64_t /*total*/) override {}
78
79 /// Does nothing.
80 void Advance(std::uint64_t /*count*/, std::string_view /*detail*/) override {}
81
82 /// Does nothing.
83 void Log(LogLevel /*level*/, std::string_view /*message*/) override {}
84
85 /// @return Always false.
86 [[nodiscard]] bool IsCancellationRequested() const override
87 {
88 return false;
89 }
90
91 /// Does nothing.
92 void Fail(std::string_view /*reason*/) override {}
93};
94
95} // namespace Lightweight
A ProgressTask that reports nothing and is never cancelled; used where a host passes none.
void SetTotal(std::uint64_t) override
Does nothing.
void Advance(std::uint64_t, std::string_view) override
Does nothing.
void Fail(std::string_view) override
Does nothing.
void Log(LogLevel, std::string_view) override
Does nothing.
std::unique_ptr< ProgressTask > BeginSubTask(std::string_view, std::uint64_t) override
bool IsCancellationRequested() const override
Progress of one task, which the library reports and a host (a CLI, a GUI) renders.
virtual void Advance(std::uint64_t count=1, std::string_view detail={})=0
virtual void Log(LogLevel level, std::string_view message)=0
virtual std::unique_ptr< ProgressTask > BeginSubTask(std::string_view title, std::uint64_t total)=0
virtual void SetTotal(std::uint64_t total)=0
LogLevel
Severity of a message reported against a task.
virtual bool IsCancellationRequested() const =0
virtual void Fail(std::string_view reason)=0
virtual ~ProgressTask()=default
Marks the task finished.