0031763: Foundation Classes - reporting of progress within parallel algorithms
[occt.git] / src / Message / Message_ProgressRange.hxx
CommitLineData
7e785937 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
19class 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.
7a3e8aad 35//! Any consequent attempts to use range will give no result on the progress;
7e785937 36//! in debug mode, an assert message will be generated.
37//!
38//! @sa Message_ProgressScope for more details
39class Message_ProgressRange
40{
41public:
7a3e8aad 42 //! Constructor of the empty range
7e785937 43 Message_ProgressRange()
7a3e8aad 44 : myParentScope (0), myStart(0.), myDelta (0.), myWasUsed (false)
7e785937 45 {}
46
7a3e8aad 47 //! Copy constructor disarms the source
7e785937 48 Message_ProgressRange (const Message_ProgressRange& theOther)
49 : myParentScope (theOther.myParentScope),
7a3e8aad 50 myStart (theOther.myStart),
7e785937 51 myDelta (theOther.myDelta),
52 myWasUsed (theOther.myWasUsed)
53 {
54 // discharge theOther
55 theOther.myWasUsed = true;
56 }
57
7a3e8aad 58 //! Copy assignment disarms the source
7e785937 59 Message_ProgressRange& operator=(const Message_ProgressRange& theOther)
60 {
61 myParentScope = theOther.myParentScope;
7a3e8aad 62 myStart = theOther.myStart;
7e785937 63 myDelta = theOther.myDelta;
64 myWasUsed = theOther.myWasUsed;
65 theOther.myWasUsed = true;
66 return *this;
67 }
68
69 //! Returns true if ProgressIndicator signals UserBreak
70 Standard_Boolean UserBreak() const;
71
72 //! Returns false if ProgressIndicator signals UserBreak
73 Standard_Boolean More() const
74 {
75 return !UserBreak();
76 }
77
78 //! Returns true if this progress range is attached to some indicator.
79 Standard_Boolean IsActive() const;
80
81 //! Closes the current range and advances indicator
82 void Close();
83
84 //! Destructor
85 ~Message_ProgressRange()
86 {
87 Close();
88 }
89
90private:
91 //! Constructor is private
7a3e8aad 92 Message_ProgressRange (const Message_ProgressScope& theParent,
93 Standard_Real theStart, Standard_Real theDelta)
7e785937 94 : myParentScope (&theParent),
7a3e8aad 95 myStart (theStart),
7e785937 96 myDelta (theDelta),
97 myWasUsed (false)
98 {}
99
100private:
101 const Message_ProgressScope* myParentScope; //!< Pointer to parent scope
7a3e8aad 102 Standard_Real myStart; //!< Start point on the global scale
103 Standard_Real myDelta; //!< Step of incrementation on the global scale
104
7e785937 105 mutable Standard_Boolean myWasUsed; //!< Flag indicating that this range
106 //! was used to create a new scope
107
108 friend class Message_ProgressScope;
109};
110
111#include <Message_ProgressIndicator.hxx>
112
113//=======================================================================
114//function : IsActive
115//purpose :
116//=======================================================================
117inline Standard_Boolean Message_ProgressRange::IsActive() const
118{
119 return !myWasUsed && myParentScope && myParentScope->myProgress;
120}
121
122//=======================================================================
123//function : UserBreak
124//purpose :
125//=======================================================================
126inline Standard_Boolean Message_ProgressRange::UserBreak() const
127{
128 return myParentScope && myParentScope->myProgress && myParentScope->myProgress->UserBreak();
129}
130
131//=======================================================================
132//function : Close
133//purpose :
134//=======================================================================
135inline void Message_ProgressRange::Close()
136{
137 if (!IsActive())
138 return;
139
140 myParentScope->myProgress->Increment(myDelta, *myParentScope);
141 myParentScope = 0;
142 myWasUsed = true;
143}
144
145#endif // _Message_ProgressRange_HeaderFile