OpenWalnut  1.5.0dev
WProgressCombiner.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WPROGRESSCOMBINER_H
26 #define WPROGRESSCOMBINER_H
27 
28 #include <memory>
29 #include <set>
30 #include <shared_mutex>
31 #include <string>
32 
33 #include <boost/thread.hpp>
34 
35 #include "WProgress.h"
36 
37 /**
38  * Base class for all kinds of progress combinations. You might want to derive from this class to implement some special progress
39  * combination.
40  */
42 {
43 friend class WProgressCombinerTest; //!< Access for test class.
44 public:
45  /**
46  * Abbreviate shared_ptr for this class.
47  */
48  typedef std::shared_ptr< WProgressCombiner > SPtr;
49 
50  /**
51  * Abbreviate shared_ptr for this class.
52  */
53  typedef std::shared_ptr< const WProgressCombiner > ConstSPtr;
54 
55  /**
56  * Default constructor. It creates a empty combiner.
57  *
58  * \param name the (optional) name of this progress.
59  */
60  explicit WProgressCombiner( std::string name = "" );
61 
62  /**
63  * Destructor.
64  */
65  virtual ~WProgressCombiner();
66 
67  /**
68  * Stops the progress. Progress combiner propagate this request to their children. Please not that this operation is
69  * expansive. It locks the updateLock and removes all child progress.
70  */
71  virtual void finish();
72 
73  /**
74  * Simple increment operator to signal a forward stepping.
75  *
76  * \note this actually is for ++p. p++ is not useful since it returns a copy of WProgress with the old count.
77  *
78  * \return the incremented WProgress instance.
79  */
80  virtual WProgressCombiner& operator++();
81 
82  /**
83  * Returns the overall progress of this progress instance, including the child progress'.
84  *
85  * \return the progress.
86  */
87  virtual float getProgress();
88 
89  /**
90  * Adds a new progress to this combiner. It does not check whether the specified progress already is associated with another
91  * combiner, which allows some kind of "shared" progress. The progress stays in the progress list until finish() is called,
92  * which actually cleans up and resets a combiner.
93  *
94  * \param progress the progress to add as a child.
95  * \note it is possible to add ProgressCombiner instances as well.
96  */
97  virtual void addSubProgress( std::shared_ptr< WProgress > progress );
98 
99  /**
100  * Removes the specified sub progress from this combiner.
101  *
102  * \param progress the progress to remove.
103  */
104  virtual void removeSubProgress( std::shared_ptr< WProgress > progress );
105 
106  /**
107  * Function updating the internal state. This needs to be called before any get function to ensure the getter return the right
108  * values.
109  *
110  * \note this method is expansive. It uses a lock to avoid concurrent write and iterates over this combiners children.
111  */
112  virtual void update();
113 
114  /**
115  * Generates a string combined out of every child progress name.
116  *
117  * \param excludeFinished if true, the combined name list only contains unfinished progress'
118  *
119  * \return One describing string for all child progress names.
120  */
121  std::string getCombinedNames( bool excludeFinished = false ) const;
122 
123 protected:
124  /**
125  * The name of the combiner.
126  */
127  std::string m_name;
128 
129  /**
130  * The current conglomerated progress. Set by update().
131  */
132  float m_progress;
133 
134  /**
135  * Set of all child progress.
136  */
137  std::set< std::shared_ptr< WProgress > > m_children;
138 
139  /**
140  * Lock for the above child set and the internal state update.
141  */
142  mutable std::shared_mutex m_updateLock;
143 
144 private:
145 };
146 
147 #endif // WPROGRESSCOMBINER_H
148 
Class testing the functionality of progress combiners.
Base class for all kinds of progress combinations.
virtual void removeSubProgress(std::shared_ptr< WProgress > progress)
Removes the specified sub progress from this combiner.
std::shared_ptr< WProgressCombiner > SPtr
Abbreviate shared_ptr for this class.
virtual void addSubProgress(std::shared_ptr< WProgress > progress)
Adds a new progress to this combiner.
virtual void update()
Function updating the internal state.
WProgressCombiner(std::string name="")
Default constructor.
std::shared_mutex m_updateLock
Lock for the above child set and the internal state update.
std::set< std::shared_ptr< WProgress > > m_children
Set of all child progress.
virtual ~WProgressCombiner()
Destructor.
virtual float getProgress()
Returns the overall progress of this progress instance, including the child progress'.
std::shared_ptr< const WProgressCombiner > ConstSPtr
Abbreviate shared_ptr for this class.
virtual void finish()
Stops the progress.
virtual WProgressCombiner & operator++()
Simple increment operator to signal a forward stepping.
std::string getCombinedNames(bool excludeFinished=false) const
Generates a string combined out of every child progress name.
std::string m_name
The name of the combiner.
float m_progress
The current conglomerated progress.
Class managing progress inside of modules.
Definition: WProgress.h:42