stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
progress.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023 Amebis
4*/
5
6#pragma once
7
8#include "compat.hpp"
9#include "interval.hpp"
10#include <chrono>
11
12namespace stdex
13{
17 template <class T>
19 {
20 public:
26 virtual void set_text(_In_z_ const char* msg)
27 {
28 _Unreferenced_(msg);
29 }
30
37 virtual void set_range(_In_ T start, _In_ T end)
38 {
39 start; end;
40 }
41
47 virtual void set(_In_ T value)
48 {
49 value;
50 }
51
57 virtual void show(_In_ bool show = true)
58 {
59 _Unreferenced_(show);
60 }
61
65 virtual bool cancel()
66 {
67 return false;
68 }
69 };
70
76 template <class T>
77 class lazy_progress : public progress<T>
78 {
79 public:
85 lazy_progress(_In_ const std::chrono::nanoseconds& timeout = std::chrono::nanoseconds(500000)) :
86 m_timeout(timeout),
87 m_start(0),
88 m_end(0),
89 m_value(-1)
90 {}
91
98 virtual void set_range(_In_ T start, _In_ T end)
99 {
100 m_start = start;
101 m_end = end;
102 }
103
109 virtual void set(_In_ T value)
110 {
111 if (value == m_start || value == m_end)
112 m_last = std::chrono::high_resolution_clock::now();
113 else if (value == m_value)
114 return;
115 else {
116 auto now = std::chrono::high_resolution_clock::now();
117 if (now - m_last < m_timeout)
118 return;
119 m_last = now;
120 }
121 m_value = value;
122 do_set();
123 }
124
125 protected:
129 virtual void do_set() {}
130
131 protected:
132 std::chrono::nanoseconds m_timeout;
133 std::chrono::system_clock::time_point m_last;
134 T m_start, m_end, m_value;
135 };
136
142 template <class T>
143 class global_progress : public progress<T>
144 {
145 public:
151 global_progress(_In_opt_ progress<T>* host = NULL) : m_host(host)
152 {}
153
159 void attach(_In_opt_ progress<T>* host)
160 {
161 m_host = host;
162 }
163
170 {
171 progress<T>* k = m_host;
172 m_host = NULL;
173 return k;
174 }
175
182 void set_global_range(_In_ T start, _In_ T end)
183 {
184 m_global.start = start;
185 m_global.end = end;
186 if (m_host)
187 m_host->set_range(m_global.start, m_global.end);
188 }
189
196 void set_section_range(_In_ T start, _In_ T end)
197 {
198 m_section.start = start;
199 m_section.end = end;
200 }
201
207 virtual void set_text(_In_ const char* msg)
208 {
209 if (m_host)
210 m_host->set_text(msg);
211 }
212
219 virtual void set_range(_In_ T start, _In_ T end)
220 {
221 m_local.start = start;
222 m_local.end = end;
223 }
224
230 virtual void set(_In_ T value)
231 {
232 if (m_host) {
233 T size = m_local.size();
234 if (size != 0) {
235 // TODO: Implement with muldiv.
236 m_host->set(((value - m_local.start) * m_section.size() / size) + m_section.start);
237 }
238 }
239 }
240
246 virtual void show(_In_ bool show = true)
247 {
248 if (m_host)
249 m_host->show(show);
250 }
251
255 virtual bool cancel()
256 {
257 return m_host && m_host->cancel();
258 }
259
260 protected:
261 progress<T>* m_host;
262 interval<T> m_local, m_global, m_section;
263 };
264
270
271 template <class T>
273 {
274 public:
276 global_progress<T>(host),
277 m_host_ref(host)
278 {
279 m_host_ref = this;
280 }
281
283 {
284 m_host_ref = this->detach();
285 }
286
287 protected:
288 progress<T>*& m_host_ref;
289 };
290}
Global progress indicator base class.
Definition progress.hpp:144
global_progress(progress< T > *host=NULL)
Constructs a progress indicator.
Definition progress.hpp:151
void set_section_range(T start, T end)
Set section extend of the progress indicator.
Definition progress.hpp:196
virtual void show(bool show=true)
Show or hide progress.
Definition progress.hpp:246
virtual bool cancel()
Query whether user requested abort.
Definition progress.hpp:255
virtual void set(T value)
Set local current progress.
Definition progress.hpp:230
virtual void set_text(const char *msg)
Set progress indicator text.
Definition progress.hpp:207
progress< T > * detach()
Detach host progress indicator.
Definition progress.hpp:169
void set_global_range(T start, T end)
Set global extend of the progress indicator.
Definition progress.hpp:182
void attach(progress< T > *host)
Attach to a host progress indicator.
Definition progress.hpp:159
virtual void set_range(T start, T end)
Set local extend of the progress indicator.
Definition progress.hpp:219
Lazy progress indicator base class.
Definition progress.hpp:78
virtual void set(T value)
Set current progress.
Definition progress.hpp:109
lazy_progress(const std::chrono::nanoseconds &timeout=std::chrono::nanoseconds(500000))
Constructs a lazy progress indicator.
Definition progress.hpp:85
virtual void do_set()
Called when progress reporting is due. Should override this method to implement actual progress refre...
Definition progress.hpp:129
virtual void set_range(T start, T end)
Set progress range extent.
Definition progress.hpp:98
Progress indicator switcher.
Definition progress.hpp:273
Progress indicator base class.
Definition progress.hpp:19
virtual bool cancel()
Query whether user requested abort.
Definition progress.hpp:65
virtual void set_text(const char *msg)
Set progress indicator text.
Definition progress.hpp:26
virtual void show(bool show=true)
Show or hide progress.
Definition progress.hpp:57
virtual void set(T value)
Set current progress.
Definition progress.hpp:47
virtual void set_range(T start, T end)
Set progress range extent.
Definition progress.hpp:37
Numerical interval.
Definition interval.hpp:18