0025748: Parallel version of progress indicator
[occt.git] / src / Message / Message_ProgressRange.hxx
1 // Copyright (c) 2020 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #ifndef _Message_ProgressRange_HeaderFile
15 #define _Message_ProgressRange_HeaderFile
16
17 #include <Standard_TypeDef.hxx>
18
19 class Message_ProgressScope;
20
21 //! Auxiliary class representing a part of the global progress scale allocated by
22 //! a step of the progress scope, see Message_ProgressScope::Next().
23 //!
24 //! A range object takes responsibility of advancing the progress by the size of
25 //! allocated step, which is then performed depending on how it is used:
26 //!
27 //! - If Message_ProgressScope object is created using this range as argument, then
28 //!   this respondibility is taken over by that scope.
29 //!
30 //! - Otherwise, a range advances progress directly upon destruction.
31 //!
32 //! A range object can be copied, the responsibility for progress advancement is 
33 //! then taken by the copy.
34 //! The same range object may be used (either copied or used to create scope) only once.
35 //! Any consequenct attempts to use range will give no result on the progress;
36 //! in debug mode, an assert message will be generated.
37 //!
38 //! @sa Message_ProgressScope for more details
39 class Message_ProgressRange
40 {
41 public:
42   //! Constructor of the range
43   Message_ProgressRange()
44     : myParentScope (0), myDelta (0), myWasUsed (false)
45   {}
46
47   //! Copy constructor disarms the source.
48   Message_ProgressRange (const Message_ProgressRange& theOther)
49     : myParentScope (theOther.myParentScope),
50       myDelta (theOther.myDelta),
51       myWasUsed (theOther.myWasUsed)
52   {
53     // discharge theOther
54     theOther.myWasUsed = true;
55   }
56
57   //! Copy assignment disarms the source.
58   Message_ProgressRange& operator=(const Message_ProgressRange& theOther)
59   {
60     myParentScope = theOther.myParentScope;
61     myDelta = theOther.myDelta;
62     myWasUsed = theOther.myWasUsed;
63     theOther.myWasUsed = true;
64     return *this;
65   }
66
67   //! Returns true if ProgressIndicator signals UserBreak
68   Standard_Boolean UserBreak() const;
69
70   //! Returns false if ProgressIndicator signals UserBreak
71   Standard_Boolean More() const
72   {
73     return !UserBreak();
74   }
75
76   //! Returns true if this progress range is attached to some indicator.
77   Standard_Boolean IsActive() const;
78
79   //! Closes the current range and advances indicator
80   void Close();
81
82   //! Destructor
83   ~Message_ProgressRange()
84   {
85     Close();
86   }
87
88 private:
89   //! Constructor is private
90   Message_ProgressRange (const Message_ProgressScope& theParent, Standard_Real theDelta)
91     : myParentScope (&theParent),
92       myDelta (theDelta),
93       myWasUsed (false)
94   {}
95
96 private:
97   const Message_ProgressScope* myParentScope;  //!< Pointer to parent scope
98   Standard_Real                myDelta;        //!< Step of incrementation
99   mutable Standard_Boolean     myWasUsed;      //!< Flag indicating that this range
100                                                //!  was used to create a new scope
101
102   friend class Message_ProgressScope;
103 };
104
105 #include <Message_ProgressIndicator.hxx>
106
107 //=======================================================================
108 //function : IsActive
109 //purpose  :
110 //=======================================================================
111 inline Standard_Boolean Message_ProgressRange::IsActive() const
112 {
113   return !myWasUsed && myParentScope && myParentScope->myProgress;
114 }
115
116 //=======================================================================
117 //function : UserBreak
118 //purpose  :
119 //=======================================================================
120 inline Standard_Boolean Message_ProgressRange::UserBreak() const
121 {
122   return myParentScope && myParentScope->myProgress && myParentScope->myProgress->UserBreak();
123 }
124
125 //=======================================================================
126 //function : Close
127 //purpose  :
128 //=======================================================================
129 inline void Message_ProgressRange::Close()
130 {
131   if (!IsActive())
132     return;
133
134   myParentScope->myProgress->Increment(myDelta, *myParentScope);
135   myParentScope = 0;
136   myWasUsed = true;
137 }
138
139 #endif // _Message_ProgressRange_HeaderFile