NDEVR
API Documentation
ProgressInfo.hpp
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: InfoPipe
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/BaseValues.h>
35#include <NDEVR/TimeSpan.h>
36#include <NDEVR/Buffer.h>
37namespace NDEVR
38{
39 class StringView;
40 class String;
41 class LogStream;
42 class LogMessage;
43 class TranslatedString;
48 class InfoPipe
49 {
50 public:
51 friend class LogPtr;
52 friend class ProgressInfo;
53 friend class CancelInfo;
54 friend class LogManager;
55 InfoPipe() noexcept {};
56 virtual ~InfoPipe(){};
57 virtual void addMessage(uint04 id, const LogMessage& message) = 0;
58 virtual void addMessage(uint04 id, const StringView& message, uint01 log_level = 10) = 0;
59 virtual void addMessage(uint04 id, const TranslatedString& message, uint01 log_level = 10) = 0;
60 virtual void addMessages(uint04 id, const AlocatingAlignedBuffer<LogMessage, 32>& messages) = 0;
61 virtual void addStream(LogStream*, bool) {}
62 virtual void removeStream(LogStream*) {}
63 virtual bool cancelRequested() const = 0;
64 virtual void requestCancel() = 0;
65 virtual void requestCancel(uint04) = 0;
66 virtual fltp04 progressIdx(uint04) const { return 1.0f; };
67 virtual void addInput(const StringView&, uint04) = 0;
68 virtual void addInput(const StringView&) = 0;
69 protected:
73 virtual uint04 addProgressSource(const TranslatedString& name, fltp04 progress) = 0;
74 virtual void addProgressSource(const TranslatedString& name, uint04 idx, fltp04 progress) = 0;
75 virtual void removeProgressSource(uint04 idx) = 0;
76 virtual void setProgressIdx(uint04 idx, fltp04 percent) = 0;
81 virtual void addCancelGuard(const TranslatedString& name, uint04 idx) = 0;
86 virtual void removeCancelGuard(uint04 idx) = 0;
87 virtual bool cancelRequestedIdx(uint04 idx) const = 0;
88
89 virtual uint04 addInputGuard(const TranslatedString& name) = 0;
90 virtual void addInputGuard(const TranslatedString& name, uint04 idx) = 0;
91 virtual void removeInputGuard(uint04 idx) = 0;
92 virtual void getInputIdx(String&, uint04, bool) {};
93 virtual void clearInputIdx(uint04) = 0;
94 protected:
95 static volatile uint04 s_idx_incrementor;//used to generate new idx. Use thread-safe increment
96 };
97
101 class LogPtr : public InfoPipe
102 {
103 public:
104 LogPtr() noexcept
105 : m_log(nullptr)
106 , m_idx(Constant<uint04>::Invalid)
107 {}
108 LogPtr(InfoPipe* log)
109 : m_log(log)
110 , m_idx(Constant<uint04>::Invalid)
111 {}
112 LogPtr(const LogPtr& log)
113 : m_log(log.m_log)
114 , m_idx(log.m_idx)
115 {}
116 explicit LogPtr(InfoPipe& log)
117 : m_log(&log)
118 , m_idx(Constant<uint04>::Invalid)
119 {}
120 bool isValid() const
121 {
122 return m_log != nullptr;
123 }
124 InfoPipe* pipe() const
125 {
126 return m_log;
127 }
128 LogPtr& operator=(const LogPtr& log)
129 {
130 m_log = log.m_log;
131 return *this;
132 }
133 bool operator==(const LogPtr& log) const
134 {
135 if (m_log == log.m_log && m_idx == log.m_idx)
136 return true;
137 return false;
138 }
139 bool operator!=(const LogPtr& log) const
140 {
141 if (m_log != log.m_log || m_idx != log.m_idx)
142 return true;
143 return false;
144 }
145 void addMessage(const LogMessage& message) const
146 {
147 if (m_log)
148 return m_log->addMessage(m_idx, message);
149 }
150 void addMessage(const StringView& message, uint01 log_level = 10) const
151 {
152 if (m_log)
153 return m_log->addMessage(m_idx, message, log_level);
154 }
155 void addMessage(const TranslatedString& message, uint01 log_level = 10) const
156 {
157 if (m_log)
158 m_log->addMessage(m_idx, message, log_level);
159 }
160
161 void addMessages(const AlocatingAlignedBuffer<LogMessage, 32>& messages) const
162 {
163 if (m_log)
164 m_log->addMessages(m_idx, messages);
165 }
166 void addStream(LogStream* stream, bool add_all) final override
167 {
168 if (m_log) m_log->addStream(stream, add_all);
169 }
170 void removeStream(LogStream* stream) final override
171 {
172 if (m_log) m_log->removeStream(stream);
173 }
174 void requestCancel() final override
175 {
176 if (m_log) m_log->requestCancel();
177 }
178 void requestCancel(uint04 idx) final override
179 {
180 if (m_log) m_log->requestCancel(idx);
181 }
182 bool cancelRequested() const override
183 {
184 if (m_log)
185 return m_log->cancelRequested();
186 return false;
187 }
188 fltp04 progressIdx(uint04 idx) const final override
189 {
190 if (m_log)
191 return m_log->progressIdx(idx);
192 return 1.0f;
193 }
194 void addInput(const StringView& s, uint04 idx) final override
195 {
196 if (m_log)
197 m_log->addInput(s, idx);
198 }
199 void addInput(const StringView& s) final override
200 {
201 if (m_log)
202 m_log->addInput(s);
203 }
204 protected:
205 virtual void addMessage(uint04 id, const LogMessage& message) final override
206 {
207 if (m_log)
208 m_log->addMessage(id, message);
209 }
210 virtual void addMessage(uint04 id, const StringView& message, uint01 log_level) final override
211 {
212 if (m_log)
213 m_log->addMessage(id, message, log_level);
214 }
215 virtual void addMessage(uint04 id, const TranslatedString& message, uint01 log_level) final override
216 {
217 if (m_log)
218 m_log->addMessage(id, message, log_level);
219 }
220 virtual void addMessages(uint04 id, const AlocatingAlignedBuffer<LogMessage, 32>& messages) final override
221 {
222 if (m_log)
223 m_log->addMessages(id, messages);
224 }
225 void setProgressIdx(uint04 idx, fltp04 percent) final override
226 {
227 if (m_log)
228 m_log->setProgressIdx(idx, percent);
229 }
230 virtual uint04 addProgressSource(const TranslatedString& name, fltp04 progress) final override
231 {
232 if (m_log)
233 return m_log->addProgressSource(name, progress);
234 return Constant<uint04>::Invalid;
235 }
236 virtual void addProgressSource(const TranslatedString& name, uint04 idx, fltp04 progress) final override
237 {
238 if (m_log)
239 return m_log->addProgressSource(name, idx, progress);
240 }
241 void removeProgressSource(uint04 idx) final override
242 {
243 if (m_log)
244 m_log->removeProgressSource(idx);
245 }
247 {
248 if (m_log)
249 return m_log->addCancelGuard(name);
250 return Constant<uint04>::Invalid;
251 }
252 virtual void addCancelGuard(const TranslatedString& name, uint04 idx) final override
253 {
254 if (m_log)
255 return m_log->addCancelGuard(name, idx);
256 }
257 void removeCancelGuard(uint04 idx) final override
258 {
259 if (m_log)
260 m_log->removeCancelGuard(idx);
261 }
262 bool cancelRequestedIdx(uint04 idx) const final override
263 {
264 if (m_log)
265 return m_log->cancelRequestedIdx(idx);
266 else
267 return false;
268 }
269 uint04 addInputGuard(const TranslatedString& name) final override
270 {
271 if (m_log)
272 return m_log->addInputGuard(name);
273 return Constant<uint04>::Invalid;
274 }
275 void addInputGuard(const TranslatedString& name, uint04 idx) final override
276 {
277 if (m_log)
278 m_log->addInputGuard(name, idx);
279 }
280 void removeInputGuard(uint04 idx) final override
281 {
282 if (m_log)
283 m_log->removeInputGuard(idx);
284 }
285 virtual void getInputIdx(String& s, uint04 idx, bool clear) final override
286 {
287 if (m_log)
288 m_log->getInputIdx(s, idx, clear);
289 }
290 virtual void clearInputIdx(uint04 idx) final override
291 {
292 if (m_log)
293 m_log->clearInputIdx(idx);
294 }
295 protected:
296 InfoPipe* m_log;
297 uint04 m_idx;
298 };
299
302 class ProgressInfo : public LogPtr
303 {
304 public:
305 ProgressInfo()
306 : LogPtr()
307 {}
308 ProgressInfo(const TranslatedString& name, InfoPipe* pipe, fltp04 percent = Constant<fltp04>::Invalid)
309 : LogPtr(pipe)
310 {
311 if (pipe)
312 pipe->addProgressSource(name, percent);
313 }
314 ProgressInfo(const TranslatedString& name, const LogPtr& pipe, fltp04 percent = Constant<fltp04>::Invalid)
315 : LogPtr(pipe)
316 {
317 m_idx = LogPtr::addProgressSource(name, percent);
318 }
319 ProgressInfo(const TranslatedString& name, InfoPipe& pipe, fltp04 percent = Constant<fltp04>::Invalid)
320 : LogPtr(pipe)
321 {
322 m_idx = pipe.addProgressSource(name, percent);
323 }
324 ProgressInfo(const ProgressInfo& info) = delete;
325 ~ProgressInfo()
326 {
327 LogPtr::setProgressIdx(m_idx, 1.0f);
328 LogPtr::removeProgressSource(m_idx);
329 }
330 void setPipe(InfoPipe* pipe)
331 {
332 finish();
333 m_log = pipe;
334 }
335 void finish()
336 {
337 LogPtr::setProgressIdx(m_idx, 1.0f);
338 LogPtr::removeProgressSource(m_idx);
339 m_idx = Constant<uint04>::Invalid;
340 }
341 void restart(const TranslatedString& name, fltp04 percent = Constant<fltp04>::Invalid)
342 {
343 LogPtr::setProgressIdx(m_idx, 1.0f);
344 LogPtr::removeProgressSource(m_idx);
345 m_idx = LogPtr::addProgressSource(name, percent);
346 }
347 void restart(const TranslatedString& name, InfoPipe* log, fltp04 percent = Constant<fltp04>::Invalid)
348 {
349 if (m_log)
350 finish();
351 m_log = log;
352 m_idx = LogPtr::addProgressSource(name, percent);
353 }
354 void setProgress(fltp04 progress)
355 {
356 LogPtr::setProgressIdx(m_idx, progress);
357 }
358 template<class t_type_a>
359 void setProgress(const t_type_a& count, const t_type_a& total)
360 {
361 if(LogPtr::isValid())
362 LogPtr::setProgressIdx(m_idx, cast<fltp04>(count) / cast<fltp04>(total));
363 }
364 template<class t_type_a>
365 void setProgressIfNeeded(const t_type_a& count, const t_type_a& total, const t_type_a& epsilon)
366 {
367 uint08 ucount = cast<uint08>(count);
368 if(LogPtr::isValid() && (m_last_progress_set > ucount || m_last_progress_set + epsilon > ucount))
369 LogPtr::setProgressIdx(m_idx, cast<fltp04>(count) / cast<fltp04>(total));
370 }
371 //calculates an epsilon to be used with setProgressIfNeeded
372 template<class t_type_a>
373 static t_type_a CalcAbsEpsilon(const t_type_a& total, fltp08 desired_epsilon = 0.01)
374 {
375 return cast<t_type_a>(cast<fltp08>(total) * desired_epsilon);
376 }
377 void setProgress(const TimeSpan& count, const TimeSpan& total)
378 {
379 if (LogPtr::isValid())
380 LogPtr::setProgressIdx(m_idx, cast<fltp04>(count / total));
381 }
382 void setToIndeterminateProgress()
383 {
384 LogPtr::setProgressIdx(m_idx, Constant<fltp04>::Invalid);
385 }
386 fltp04 progress() const
387 {
388 return LogPtr::progressIdx(m_idx);
389 }
390 protected:
391 uint08 m_last_progress_set = Constant<uint08>::Invalid;
392 };
393
396 class CancelInfo : public LogPtr
397 {
398 public:
399 CancelInfo()
400 : LogPtr()
401 {}
402 CancelInfo(const TranslatedString& name, InfoPipe* pipe)
403 : LogPtr(pipe)
404 {
405 if (pipe)
406 m_idx = pipe->addCancelGuard(name);
407 }
408 CancelInfo(const TranslatedString& name, const LogPtr& pipe)
409 : LogPtr(pipe)
410 {
412 }
413 CancelInfo(const TranslatedString& name, InfoPipe& pipe)
414 : LogPtr(pipe)
415 {
416 m_idx = pipe.addCancelGuard(name);
417 }
418 CancelInfo(const CancelInfo& info) = delete;
419 ~CancelInfo()
420 {
422 }
423 void finish()
424 {
426 }
427 void setPipe(InfoPipe* pipe)
428 {
429 finish();
430 m_log = pipe;
431 }
432 void restart(const TranslatedString& name)
433 {
436 }
437 void restart(const TranslatedString& name, InfoPipe* log)
438 {
439 if (m_log)
440 finish();
441 m_log = log;
443 }
444 bool cancelRequested() const final override
445 {
446 if (m_log)
447 return m_log->cancelRequestedIdx(m_idx);
448 return false;
449 }
450 };
451};
A light-weight base class for Log that allows processes to update, without the need for additional in...
virtual uint04 addCancelGuard(const TranslatedString &name)=0
If added, signals to any LogStream that cancel is enabled.
virtual void removeCancelGuard(uint04 idx)=0
If removed, signals to any LogStream that cancel is disabled, assuming there are no more cancel gaurd...
virtual uint04 addProgressSource(const TranslatedString &name, fltp04 progress)=0
Adds a progress gaurd, which.
A class that allows for specific log information to be conveyed to a InfoPipe object including inform...
Definition LogMessage.h:48
uint04 addCancelGuard(const TranslatedString &name) final override
If added, signals to any LogStream that cancel is enabled.
virtual uint04 addProgressSource(const TranslatedString &name, fltp04 progress) final override
Adds a progress gaurd, which.
void removeCancelGuard(uint04 idx) final override
If removed, signals to any LogStream that cancel is disabled, assuming there are no more cancel gaurd...
A listener that receives and processes log messages and progress updates from an InfoPipe.
Definition LogStream.h:49
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
Stores a time span, or difference between two times, with an optional start time.
Definition TimeSpan.h:46
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
The primary namespace for the NDEVR SDK.
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
double fltp08
Defines an alias representing an 8 byte floating-point number.
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
@ name
The display name of the object.
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
Definition Angle.h:408