PCL
StatusMonitor.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/StatusMonitor.h - Released 2024-06-18T15:48:54Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_StatusMonitor_h
53 #define __PCL_StatusMonitor_h
54 
56 
57 #include <pcl/Defs.h>
58 
59 #include <pcl/Exception.h>
60 #include <pcl/String.h>
61 #include <pcl/Utility.h>
62 
63 namespace pcl
64 {
65 
66 class PCL_CLASS StatusMonitor;
67 
68 // ----------------------------------------------------------------------------
69 
97 class PCL_CLASS StatusCallback
98 {
99 public:
100 
104  StatusCallback() = default;
105 
109  StatusCallback( const StatusCallback& ) = default;
110 
115 
119  virtual ~StatusCallback()
120  {
121  }
122 
126  StatusCallback& operator =( const StatusCallback& ) = default;
127 
131  StatusCallback& operator =( StatusCallback&& ) = default;
132 
145  virtual int Initialized( const StatusMonitor& monitor ) const = 0;
146 
162  virtual int Updated( const StatusMonitor& monitor ) const = 0;
163 
181  virtual int Completed( const StatusMonitor& monitor ) const = 0;
182 
190  virtual void InfoUpdated( const StatusMonitor& monitor ) const = 0;
191 };
192 
193 // ----------------------------------------------------------------------------
194 
222 class PCL_CLASS StatusMonitor
223 {
224 public:
225 
229  StatusMonitor() = default;
230 
235  {
236  Assign( x );
237  }
238 
242  virtual ~StatusMonitor()
243  {
244  Clear();
245  }
246 
250  StatusMonitor& operator =( const StatusMonitor& x )
251  {
252  if ( this != &x )
253  Assign( x );
254  return *this;
255  }
256 
276  void Initialize( const String& info, size_type count = 0 );
277 
338  {
339  ++m_initDisableCount;
340  }
341 
352  {
353  --m_initDisableCount;
354  }
355 
364  {
365  return m_initDisableCount <= 0;
366  }
367 
374  bool IsInitialized() const
375  {
376  return m_initialized;
377  }
378 
385  bool IsCompleted() const
386  {
387  return m_completed;
388  }
389 
402  bool IsAborted() const
403  {
404  return m_aborted;
405  }
406 
413  void SetInfo( const String& s )
414  {
415  m_info = s;
416  if ( m_callback != nullptr )
417  m_callback->InfoUpdated( *this );
418  }
419 
434  void operator ++()
435  {
436  if ( ++m_count == m_total || m_needsUpdate )
437  Update();
438  }
439 
446  void operator +=( size_type n )
447  {
448  m_count += n;
449  if ( m_total != 0 ) // if not an unbounded monitor
450  if ( m_count > m_total )
451  m_count = m_total;
452  if ( m_needsUpdate || m_count == m_total )
453  Update();
454  }
455 
463  void Complete()
464  {
465  m_count = m_total;
466  Update();
467  }
468 
474  const StatusCallback* Callback() const
475  {
476  return m_callback;
477  }
478 
485  {
486  return m_callback;
487  }
488 
499  void SetCallback( StatusCallback* callback )
500  {
501  Reset();
502  m_callback = callback;
503  }
504 
516  int ReturnValue() const
517  {
518  return m_retVal;
519  }
520 
525  String Info() const
526  {
527  return m_info;
528  }
529 
536  size_type Count() const
537  {
538  return m_count;
539  }
540 
547  size_type Total() const
548  {
549  return m_total;
550  }
551 
556  void Clear()
557  {
558  Reset();
559  }
560 
569  static unsigned RefreshRate()
570  {
571  return s_msRefreshRate;
572  }
573 
589  static void SetRefreshRate( unsigned ms );
590 
591 private:
592 
593  StatusCallback* m_callback = nullptr;
594  void* m_thread = nullptr; // current thread when Initialize() was called
595  bool m_initialized = false;
596  bool m_completed = false;
597  bool m_aborted = false;
598  bool m_needsUpdate = false; // thread-safe flag set by the dispatcher
599  int m_initDisableCount = 0;
600  int m_retVal = 0;
601  String m_info;
602  size_type m_total = 0;
603  size_type m_count = 0;
604 
605  static unsigned s_msRefreshRate;
606 
607  void Reset();
608  void Assign( const StatusMonitor& );
609  void Update();
610 
611  friend class PCL_CLASS AutoStatusCallbackRestorer;
612  friend class MonitorDispatcherThread;
613 };
614 
615 // ----------------------------------------------------------------------------
616 
617 } // pcl
618 
619 #endif // __PCL_StatusMonitor_h
620 
621 // ----------------------------------------------------------------------------
622 // EOF pcl/StatusMonitor.h - Released 2024-06-18T15:48:54Z
Automatic recovery of status monitoring callbacks.
Provides status monitoring callback functions.
Definition: StatusMonitor.h:98
virtual int Updated(const StatusMonitor &monitor) const =0
StatusCallback(const StatusCallback &)=default
virtual int Initialized(const StatusMonitor &monitor) const =0
virtual ~StatusCallback()
StatusCallback()=default
virtual void InfoUpdated(const StatusMonitor &monitor) const =0
virtual int Completed(const StatusMonitor &monitor) const =0
StatusCallback(StatusCallback &&)=default
An asynchronous status monitoring system.
bool IsCompleted() const
static void SetRefreshRate(unsigned ms)
void SetCallback(StatusCallback *callback)
bool IsAborted() const
StatusMonitor(const StatusMonitor &x)
StatusMonitor()=default
const StatusCallback * Callback() const
static unsigned RefreshRate()
void Initialize(const String &info, size_type count=0)
virtual ~StatusMonitor()
String Info() const
StatusCallback * Callback()
size_type Count() const
int ReturnValue() const
bool IsInitialized() const
size_type Total() const
void DisableInitialization()
bool IsInitializationEnabled() const
void SetInfo(const String &s)
Unicode (UTF-16) string.
Definition: String.h:8113
size_t size_type
Definition: Defs.h:609
PCL root namespace.
Definition: AbstractImage.h:77