HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
progress.hpp
1// Copyright Take Vos 2024.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "notifier.hpp"
8#include "../utility/utility.hpp"
9#include "../macros.hpp"
10#include <memory>
11#include <mutex>
12#include <chrono>
13
14hi_export_module(hikogui.dispatch.progress);
15
16hi_export namespace hi {
17inline namespace v1 {
18
19class progress_sink;
20
24public:
25 constexpr progress_token() noexcept = default;
26
29 void set_value(float value);
30
34 {
35 set_value(value);
36 return *this;
37 }
38
39private:
40 progress_sink* _sink = nullptr;
41
42 constexpr progress_token(progress_sink* sink) noexcept : _sink(sink) {}
43
44 friend class progress_sink;
45};
46
50public:
51 using callback_type = notifier<>::callback_type;
52
55 constexpr progress_sink() noexcept = default;
56
59 [[nodiscard]] progress_token get_token() const noexcept
60 {
61 return progress_token{const_cast<progress_sink*>(this)};
62 }
63
66 void reset()
67 {
68 set_value(0.0f);
69 }
70
73 void set_value(float value)
74 {
75 _value = value;
76 _notifier();
77 }
78
81 [[nodiscard]] constexpr float value() const noexcept
82 {
83 return _value;
84 }
85
88 [[nodiscard]] float operator*() const noexcept
89 {
90 return _value;
91 }
92
95 template<typename Callback>
96 [[nodiscard]] notifier<>::callback_type subscribe(Callback&& callback, callback_flags flags = callback_flags::synchronous)
97 {
98 return _notifier.subscribe(std::forward<Callback>(callback), flags);
99 }
100
101private:
102 notifier<> _notifier = {};
103 float _value = 0.0f;
104};
105
106inline void progress_token::set_value(float value)
107{
108 hi_axiom_bounds(value, 0.0f, 1.0f);
109 if (_sink != nullptr) {
110 _sink->set_value(value);
111 }
112}
113
114} // namespace v1
115}
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition callback.hpp:77
Token to pass to a function to report its progress.
Definition progress.hpp:23
void set_value(float value)
Set the current progress.
Definition progress.hpp:106
progress_token & operator=(float value)
Set the current progress.
Definition progress.hpp:33
A sink to read the current progress of a function.
Definition progress.hpp:49
float operator*() const noexcept
Get the current progress.
Definition progress.hpp:88
progress_token get_token() const noexcept
Get a token to pass to a function.
Definition progress.hpp:59
constexpr float value() const noexcept
Get the current progress.
Definition progress.hpp:81
constexpr progress_sink() noexcept=default
Create a fresh progress.
void reset()
Reset progress.
Definition progress.hpp:66
void set_value(float value)
Set progress.
Definition progress.hpp:73
notifier ::callback_type subscribe(Callback &&callback, callback_flags flags=callback_flags::synchronous)
Subscribe a callback function to be called when progress is modified.
Definition progress.hpp:96