17#ifndef AMINO_CORE_TASK_OBSERVER_H
18#define AMINO_CORE_TASK_OBSERVER_H
28#include <Amino/Core/internal/PImpl.h>
33#define AMINO_API AMINO_CORE_SHARED_DECL
76 operator Status()
const {
return m_status; }
79 explicit operator bool()
const {
return m_status ==
eSuccess; }
120 virtual
void onProgress(
121 StringView const& title,
unsigned num,
unsigned denum) noexcept;
124 virtual
void onMessage(
Message const& message) noexcept;
154 template <
typename T,
typename... Args>
183 inline explicit operator
bool()
const {
return isValid(); }
245 static_assert(std::is_base_of_v<ITaskObserver, T>);
247 template <
typename D>
248 using enable_if_derived = std::enable_if_t<std::is_convertible_v<D*, T*>>;
260 typename = std::enable_if_t<std::is_constructible_v<T, Args...>>>
266 template <
typename D,
typename = enable_if_derived<D>>
272 template <
typename D,
typename = enable_if_derived<D>>
281 T
const&
operator*()
const {
return *get_dereferenceable(); }
284 T
const*
operator->()
const {
return get_dereferenceable(); }
287 T
const*
get()
const {
return static_cast<T const*
>(getInterface()); }
288 T*
get() {
return static_cast<T *
>(getInterface()); }
302 T
const* get_dereferenceable()
const {
303 auto const* ptr = get();
307 T* get_dereferenceable() {
353 inline explicit operator
bool()
const {
return isValid(); }
363 StringView const& title,
unsigned num,
unsigned denum)
const {
364 assert(num <= denum);
365 internal_notifyProgress(title, num, denum);
374 template <
typename... Args>
380 Args
const&... args)
const {
381 internal_notifyMessage(
382 std::move(source), kind, cls, text, Internal::FormatArgs{args...});
384 template <
typename... Args>
389 Args
const&... args)
const {
390 internal_notifyMessage(
391 Source{}, kind, cls, text, Internal::FormatArgs{args...});
393 template <
typename... Args>
396 internal_notifyMessage(
398 Internal::FormatArgs{args...});
400 template <
typename... Args>
405 Args
const&... args)
const {
406 internal_notifyMessage(
408 Internal::FormatArgs{args...});
413 template <
typename... Args>
414 AMINO_INTERNAL_FORCEINLINE
void notifyDebug(Args&&... args)
const {
419 template <
typename... Args>
420 AMINO_INTERNAL_FORCEINLINE
void notifyInfo(Args&&... args)
const {
425 template <
typename... Args>
431 template <
typename... Args>
432 AMINO_INTERNAL_FORCEINLINE
void notifyError(Args&&... args)
const {
460 StringView const& title,
unsigned num,
unsigned denum)
const;
469 Internal::FormatArgs<0>
const&)
const;
481 Internal::PImpl<TaskNotifier, 2> m_impl;
498 template <
typename TaskNotifier>
501 : m_notifier{notifier},
504 m_denominator{denum} {
505 m_notifier.notifyProgress(m_title, 0, m_denominator);
511 m_notifier.notifyProgress(m_title, m_numerator, m_denominator);
522 unsigned m_numerator;
525 const unsigned m_denominator;
535template <
typename T,
typename... Args>
537 static_assert(std::is_base_of_v<ITaskObserver, T>);
538 static_assert(!std::is_abstract_v<T>);
539 static_assert(std::is_constructible_v<T, Args...>);
548 auto notifier = internal_getNotifier(success);
549 assert(success &&
"TaskObserver already has a notifier in use.");
569 : m_notifier(
std::move(notifier)) {}
581 unsigned denum) noexcept override;
584 void onMessage(
Message const& message) noexcept override;
Definition of macros for symbol visibility.
String view class (similar to std::string_view)
Forward declarations for task observer classes.
MessageKind
The kind of message (error, warning, info, debug).
The "class" of the message.
The text content from which to construct a message (with optional formatting specifiers).
A Message object that of a specific MessageKind, optional MessageClass, and a MessageText that relate...
Generic opaque handle to a source (provenance).
Same as Span but the constructor from a range is implicit, making it more convenient and safe to use ...
String view class (similar to std::string_view).
The completion status of a task.
TaskStatus()=default
Default constructor (eSuccess status).
TaskStatus(Status status) noexcept
Constructor.
@ eSuccess
The task completed successfully.
@ eError
The task failed due to an error.
@ eDropped
The task was dropped (because it was destroyed or cancelled before it started).
@ eCancelled
The task was cancelled explicitly by the user.
Abstract interface for observing the progress of a task.
virtual ~ITaskObserver()
Destructor.
ITaskObserver()=default
Constructor.
virtual void onStart() noexcept
Called when the task starts (defaults to noop).
Class managing (with unique ownership) a concrete ITaskObserver.
TaskNotifier getNotifier() const
Create and return a notifier associated with this observer.
AMINO_CORE_SHARED_DECL TaskObserver & operator=(TaskObserver &&o) noexcept
Move assignment operator.
AMINO_CORE_SHARED_DECL void reset() noexcept
Reset the observer to a noop observer.
static TaskObserverT< T > make(Args &&... args)
Create a new concrete task observer of type T.
AMINO_CORE_SHARED_DECL bool isInUse() const
Whether this TaskObserver has a TaskNotifier in use.
AMINO_CORE_SHARED_DECL TaskObserver() noexcept
Default constructor (constructs a noop observer).
Typed-version of TaskObserver.
TaskObserverT(Args &&... args)
Constructs a TaskObserverT managing a concrete Amino::ITaskObserver of derived type T.
T & operator*()
Get the concrete derived Amino::ITaskObserver uniquely managed by this TaskObserverT.
T const * operator->() const
Get the concrete derived Amino::ITaskObserver uniquely managed by this TaskObserverT.
TaskObserverT(TaskObserverT< D > &&other) noexcept
Upcast conversion constructor.
TaskObserverT & operator=(TaskObserverT< D > &&other) noexcept
Upcast conversion assignment.
T const & operator*() const
Get the concrete derived Amino::ITaskObserver uniquely managed by this TaskObserverT.
T * operator->()
Get the concrete derived Amino::ITaskObserver uniquely managed by this TaskObserverT.
T * get()
Get the concrete derived Amino::ITaskObserver uniquely managed by this TaskObserverT.
T const * get() const
Get the concrete derived Amino::ITaskObserver uniquely managed by this TaskObserverT.
Class referencing (non-owning) a concrete ITaskObserver.
AMINO_CORE_SHARED_DECL TaskNotifier() noexcept
Default Constructor (constructs a noop notifier).
void notifySuccess() const
Notify the observer that the task has completed successfully.
AMINO_INTERNAL_FORCEINLINE void notifyMessage(MessageKind kind, MessageText text, Args const &... args) const
Notify the observer that a message was reported.
void notifyProgress(StringView const &title, unsigned num, unsigned denum) const
Notify the observer of the progress of a named task.
AMINO_INTERNAL_FORCEINLINE void notifyWarning(Args &&... args) const
Short-hand for notifying a warning message.
AMINO_CORE_SHARED_DECL void notifyStart() const
Notify the observer that the task has started.
AMINO_INTERNAL_FORCEINLINE void notifyMessage(MessageKind kind, Source source, MessageClass cls, MessageText text, Args const &... args) const
Notify the observer that a message was reported.
void notifyCancelled() const
Notify the observer that the task has completed due to cancellation.
AMINO_INTERNAL_FORCEINLINE void notifyMessage(MessageKind kind, MessageClass cls, MessageText text, Args const &... args) const
Notify the observer that a message was reported.
AMINO_INTERNAL_FORCEINLINE void notifyError(Args &&... args) const
Short-hand for notifying an error message.
void notifyError() const
Notify the observer that the task has completed with errors.
AMINO_INTERNAL_FORCEINLINE void notifyMessage(MessageKind kind, Source source, MessageText text, Args const &... args) const
Notify the observer that a message was reported.
AMINO_INTERNAL_FORCEINLINE void notifyDebug(Args &&... args) const
Short-hand for notifying a debug message.
AMINO_CORE_SHARED_DECL void notifyMessage(Message const &message) const
Notify the observer that a message was reported.
void notifyDropped() const
Notify the observer that the task has been dropped.
AMINO_CORE_SHARED_DECL void notifyDone(TaskStatus status) const
Notify the observer that the task has completed.
AMINO_INTERNAL_FORCEINLINE void notifyInfo(Args &&... args) const
Short-hand for notifying an info message.
RAII-style progress notifier helper.
void notify(unsigned n=1)
Notify the observer of the progress of the task.
TaskProgressNotifier(TaskNotifier ¬ifier, StringLiteral title, unsigned denum)
Constructor.
Base class for a ITaskObserver that forwards notifications to a given TaskNotifier.
void onStart() noexcept override
Forwards the onStart notification to its notifier.
~ForwardTaskObserver() override
Destructor.
ForwardTaskObserver(TaskNotifier notifier)
Constructor.