Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:16

0001 // Created on: 2002-02-20
0002 // Created by: Andrey BETENEV
0003 // Copyright (c) 2002-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _Message_ProgressIndicator_HeaderFile
0017 #define _Message_ProgressIndicator_HeaderFile
0018 
0019 #include <Standard_Mutex.hxx>
0020 #include <Standard_Handle.hxx>
0021 
0022 DEFINE_STANDARD_HANDLE(Message_ProgressIndicator, Standard_Transient)
0023 
0024 class Message_ProgressRange;
0025 class Message_ProgressScope;
0026 
0027 //! Defines abstract interface from program to the user.
0028 //! This includes progress indication and user break mechanisms.
0029 //!
0030 //! The progress indicator controls the progress scale with range from 0 to 1.
0031 //! 
0032 //! Method Start() should be called once, at the top level of the call stack,
0033 //! to reset progress indicator and get access to the root range:
0034 //!
0035 //! @code{.cpp}
0036 //! Handle(Message_ProgressIndicator) aProgress = ...;
0037 //! anAlgorithm.Perform (aProgress->Start());
0038 //! @endcode
0039 //!
0040 //! To advance the progress indicator in the algorithm,
0041 //! use the class Message_ProgressScope that provides iterator-like
0042 //! interface for incrementing progress; see documentation of that
0043 //! class for details.
0044 //! The object of class Message_ProgressRange will automatically advance 
0045 //! the indicator if it is not passed to any Message_ProgressScope.
0046 //!
0047 //! The progress indicator supports concurrent processing and 
0048 //! can be used in multithreaded applications.
0049 //!
0050 //! The derived class should be created to connect this interface to 
0051 //! actual implementation of progress indicator, to take care of visualization
0052 //! of the progress (e.g. show total position at the graphical bar,
0053 //! print scopes in text mode, or else), and for implementation
0054 //! of user break mechanism (if necessary).
0055 //!
0056 //! See details in documentation of methods Show() and UserBreak().
0057 
0058 class Message_ProgressIndicator : public Standard_Transient
0059 {
0060   DEFINE_STANDARD_RTTIEXT(Message_ProgressIndicator, Standard_Transient)
0061 public:
0062   //!@name Initialization of progress indication
0063 
0064   //! Resets the indicator to zero, calls Reset(), and returns the range.
0065   //! This range refers to the scope that has no name and is initialized
0066   //! with max value 1 and step 1.
0067   //! Use this method to get the top level range for progress indication.
0068   Standard_EXPORT Message_ProgressRange Start();
0069 
0070   //! If argument is non-null handle, returns theProgress->Start().
0071   //! Otherwise, returns dummy range that can be safely used in the algorithms
0072   //! but not bound to progress indicator.
0073   Standard_EXPORT static Message_ProgressRange Start
0074                       (const Handle(Message_ProgressIndicator)& theProgress);
0075 
0076 protected:
0077   //!@name Virtual methods to be defined by descendant.
0078 
0079   //! Should return True if user has sent a break signal.
0080   //!
0081   //! This method can be called concurrently, thus implementation should
0082   //! be thread-safe. It should not call Show() or Position() to
0083   //! avoid possible data races. The method should return as soon
0084   //! as possible to avoid delaying the calling algorithm.
0085   //!
0086   //! Default implementation returns False.
0087   virtual Standard_Boolean UserBreak()
0088   {
0089     return Standard_False;
0090   }
0091 
0092   //! Virtual method to be defined by descendant.
0093   //! Should update presentation of the progress indicator.
0094   //!
0095   //! It is called whenever progress position is changed.
0096   //! Calls to this method from progress indicator are protected by mutex so that
0097   //! it is never called concurrently for the same progress indicator instance.
0098   //! Show() should return as soon as possible to reduce thread contention
0099   //! in multithreaded algorithms.
0100   //!
0101   //! It is recommended to update (redraw, output etc.) only if progress is
0102   //! advanced by at least 1% from previous update.
0103   //!
0104   //! Flag isForce is intended for forcing update in case if it is required 
0105   //! at particular step of the algorithm; all calls to it from inside the core 
0106   //! mechanism (Message_Progress... classes) are done with this flag equal to False.
0107   //!
0108   //! The parameter theScope is the current scope being advanced;
0109   //! it can be used to show the names and ranges of the on-going scope and
0110   //! its parents, providing more visibility of the current stage of the process.
0111   virtual void Show (const Message_ProgressScope& theScope, 
0112                      const Standard_Boolean isForce) = 0;
0113 
0114   //! Call-back method called by Start(), can be redefined by descendants
0115   //! if some actions are needed when the indicator is restarted.
0116   virtual void Reset() {}
0117   
0118 public:
0119   //!@name Auxiliary methods
0120 
0121   //! Returns total progress position ranged from 0 to 1.
0122   //! Should not be called concurrently while the progress is advancing,
0123   //! except from implementation of method Show().
0124   Standard_Real GetPosition() const
0125   {
0126     return myPosition;
0127   }
0128 
0129   //! Destructor
0130   Standard_EXPORT ~Message_ProgressIndicator();
0131 
0132 protected:
0133   
0134   //! Constructor
0135   Standard_EXPORT Message_ProgressIndicator();
0136 
0137 private:
0138 
0139   //! Increment the progress value by the specified step, 
0140   //! then calls Show() to update presentation.
0141   //! The parameter theScope is reference to the caller object;
0142   //! it is passed to Show() where can be used to track context of the process.
0143   void Increment (const Standard_Real theStep, const Message_ProgressScope& theScope);
0144 
0145 private:
0146 
0147   Standard_Real myPosition;            //!< Total progress position ranged from 0 to 1
0148   Standard_Mutex myMutex;              //!< Protection of myPosition from concurrent increment
0149   Message_ProgressScope* myRootScope;  //!< The root progress scope
0150 
0151 private:
0152   friend class Message_ProgressScope;  //!< Friend: can call Increment()
0153   friend class Message_ProgressRange;  //!< Friend: can call Increment()
0154 };
0155 
0156 #include <Message_ProgressScope.hxx>
0157 
0158 //=======================================================================
0159 //function : Increment
0160 //purpose  :
0161 //=======================================================================
0162 inline void Message_ProgressIndicator::Increment(const Standard_Real theStep,
0163                                                  const Message_ProgressScope& theScope)
0164 {
0165   // protect incrementation by mutex to avoid problems in multithreaded scenarios
0166   Standard_Mutex::Sentry aSentry(myMutex);
0167 
0168   myPosition = Min(myPosition + theStep, 1.);
0169 
0170   // show progress indicator; note that this call is protected by
0171   // the same mutex to avoid concurrency and ensure that this call
0172   // to Show() will see the position exactly as it was just set above
0173   Show(theScope, Standard_False);
0174 }
0175 
0176 #endif // _Message_ProgressIndicator_HeaderFile