Loading...
Searching...
No Matches
progress.hpp
/*
SPDX-License-Identifier: MIT
Copyright © 2023 Amebis
*/
#pragma once
#include "compat.hpp"
#include "interval.hpp"
#include <chrono>
namespace stdex
{
template <class T>
class progress
{
public:
virtual void set_text(_In_z_ const char* msg)
{
_Unreferenced_(msg);
}
virtual void set_range(_In_ T start, _In_ T end)
{
start; end;
}
virtual void set(_In_ T value)
{
value;
}
virtual void show(_In_ bool show = true)
{
_Unreferenced_(show);
}
virtual bool cancel()
{
return false;
}
};
template <class T>
class lazy_progress : public progress<T>
{
public:
lazy_progress(_In_ const std::chrono::nanoseconds& timeout = std::chrono::nanoseconds(500000)) :
m_timeout(timeout),
m_start(0),
m_end(0),
m_value(-1)
{}
virtual void set_range(_In_ T start, _In_ T end)
{
m_start = start;
m_end = end;
}
virtual void set(_In_ T value)
{
if (value == m_start || value == m_end)
m_last = std::chrono::high_resolution_clock::now();
else if (value == m_value)
return;
else {
auto now = std::chrono::high_resolution_clock::now();
if (now - m_last < m_timeout)
return;
m_last = now;
}
m_value = value;
do_set();
}
protected:
virtual void do_set() {}
protected:
std::chrono::nanoseconds m_timeout;
std::chrono::system_clock::time_point m_last;
T m_start, m_end, m_value;
};
template <class T>
class global_progress : public progress<T>
{
public:
global_progress(_In_opt_ progress<T>* host = NULL) : m_host(host)
{}
inline void attach(_In_opt_ progress<T>* host)
{
m_host = host;
}
inline progress<T>* detach()
{
progress<T>* k = m_host;
m_host = NULL;
return k;
}
inline void set_global_range(_In_ T start, _In_ T end)
{
m_global.start = start;
m_global.end = end;
if (m_host)
m_host->set_range(m_global.start, m_global.end);
}
inline void set_section_range(_In_ T start, _In_ T end)
{
m_section.start = start;
m_section.end = end;
}
virtual void set_text(_In_ const char* msg)
{
if (m_host)
m_host->set_text(msg);
}
virtual void set_range(_In_ T start, _In_ T end)
{
m_local.start = start;
m_local.end = end;
}
virtual void set(_In_ T value)
{
if (m_host) {
T size = m_local.size();
if (size != 0) {
// TODO: Implement with muldiv.
m_host->set(((value - m_local.start) * m_section.size() / size) + m_section.start);
}
}
}
virtual void show(_In_ bool show = true)
{
if (m_host)
m_host->show(show);
}
virtual bool cancel()
{
return m_host && m_host->cancel();
}
protected:
progress<T>* m_host;
interval<T> m_local, m_global, m_section;
};
template <class T>
class progress_switcher : public global_progress<T>
{
public:
progress_switcher(progress<T>*& host) :
global_progress<T>(host),
m_host_ref(host)
{
m_host_ref = this;
}
~progress_switcher()
{
m_host_ref = this->detach();
}
protected:
progress<T>*& m_host_ref;
};
}
stdex::global_progress
Global progress indicator base class.
Definition progress.hpp:144
stdex::global_progress::global_progress
global_progress(progress< T > *host=NULL)
Constructs a progress indicator.
Definition progress.hpp:151
stdex::global_progress::set_section_range
void set_section_range(T start, T end)
Set section extend of the progress indicator.
Definition progress.hpp:196
stdex::global_progress::show
virtual void show(bool show=true)
Show or hide progress.
Definition progress.hpp:246
stdex::global_progress::cancel
virtual bool cancel()
Query whether user requested abort.
Definition progress.hpp:255
stdex::global_progress::set
virtual void set(T value)
Set local current progress.
Definition progress.hpp:230
stdex::global_progress::set_text
virtual void set_text(const char *msg)
Set progress indicator text.
Definition progress.hpp:207
stdex::global_progress::detach
progress< T > * detach()
Detach host progress indicator.
Definition progress.hpp:169
stdex::global_progress::set_global_range
void set_global_range(T start, T end)
Set global extend of the progress indicator.
Definition progress.hpp:182
stdex::global_progress::attach
void attach(progress< T > *host)
Attach to a host progress indicator.
Definition progress.hpp:159
stdex::global_progress::set_range
virtual void set_range(T start, T end)
Set local extend of the progress indicator.
Definition progress.hpp:219
stdex::lazy_progress
Lazy progress indicator base class.
Definition progress.hpp:78
stdex::lazy_progress::set
virtual void set(T value)
Set current progress.
Definition progress.hpp:109
stdex::lazy_progress::lazy_progress
lazy_progress(const std::chrono::nanoseconds &timeout=std::chrono::nanoseconds(500000))
Constructs a lazy progress indicator.
Definition progress.hpp:85
stdex::lazy_progress::do_set
virtual void do_set()
Called when progress reporting is due. Should override this method to implement actual progress refre...
Definition progress.hpp:129
stdex::lazy_progress::set_range
virtual void set_range(T start, T end)
Set progress range extent.
Definition progress.hpp:98
stdex::progress_switcher
Progress indicator switcher.
Definition progress.hpp:273
stdex::progress
Progress indicator base class.
Definition progress.hpp:19
stdex::progress::cancel
virtual bool cancel()
Query whether user requested abort.
Definition progress.hpp:65
stdex::progress::set_text
virtual void set_text(const char *msg)
Set progress indicator text.
Definition progress.hpp:26
stdex::progress::show
virtual void show(bool show=true)
Show or hide progress.
Definition progress.hpp:57
stdex::progress::set
virtual void set(T value)
Set current progress.
Definition progress.hpp:47
stdex::progress::set_range
virtual void set_range(T start, T end)
Set progress range extent.
Definition progress.hpp:37
stdex::interval
Numerical interval.
Definition interval.hpp:18