OpenWalnut  1.4.0
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 <set>
29 #include <string>
30 
31 #ifndef Q_MOC_RUN
32 #include <boost/thread.hpp>
33 #endif
34 #ifndef Q_MOC_RUN
35 #include <boost/shared_ptr.hpp>
36 #endif
37 
38 #include "WProgress.h"
39 
40 /**
41  * Base class for all kinds of progress combinations. You might want to derive from this class to implement some special progress
42  * combination.
43  */
45 {
46 friend class WProgressCombinerTest;
47 public:
48  /**
49  * Abbreviate shared_ptr for this class.
50  */
51  typedef boost::shared_ptr< WProgressCombiner > SPtr;
52 
53  /**
54  * Abbreviate shared_ptr for this class.
55  */
56  typedef boost::shared_ptr< const WProgressCombiner > ConstSPtr;
57 
58  /**
59  * Default constructor. It creates a empty combiner.
60  *
61  * \param name the (optional) name of this progress.
62  */
63  explicit WProgressCombiner( std::string name = "" );
64 
65  /**
66  * Destructor.
67  */
68  virtual ~WProgressCombiner();
69 
70  /**
71  * Stops the progress. Progress combiner propagate this request to their children. Please not that this operation is
72  * expansive. It locks the updateLock and removes all child progress.
73  */
74  virtual void finish();
75 
76  /**
77  * Simple increment operator to signal a forward stepping.
78  *
79  * \note this actually is for ++p. p++ is not useful since it returns a copy of WProgress with the old count.
80  *
81  * \return the incremented WProgress instance.
82  */
83  virtual WProgressCombiner& operator++();
84 
85  /**
86  * Returns the overall progress of this progress instance, including the child progress'.
87  *
88  * \return the progress.
89  */
90  virtual float getProgress();
91 
92  /**
93  * Adds a new progress to this combiner. It does not check whether the specified progress already is associated with another
94  * combiner, which allows some kind of "shared" progress. The progress stays in the progress list until finish() is called,
95  * which actually cleans up and resets a combiner.
96  *
97  * \param progress the progress to add as a child.
98  * \note it is possible to add ProgressCombiner instances as well.
99  */
100  virtual void addSubProgress( boost::shared_ptr< WProgress > progress );
101 
102  /**
103  * Removes the specified sub progress from this combiner.
104  *
105  * \param progress the progress to remove.
106  */
107  virtual void removeSubProgress( boost::shared_ptr< WProgress > progress );
108 
109  /**
110  * Function updating the internal state. This needs to be called before any get function to ensure the getter return the right
111  * values.
112  *
113  * \note this method is expansive. It uses a lock to avoid concurrent write and iterates over this combiners children.
114  */
115  virtual void update();
116 
117  /**
118  * Generates a string combined out of every child progress name.
119  *
120  * \param excludeFinished if true, the combined name list only contains unfinished progress'
121  *
122  * \return One describing string for all child progress names.
123  */
124  std::string getCombinedNames( bool excludeFinished = false ) const;
125 
126 protected:
127  /**
128  * The name of the combiner.
129  */
130  std::string m_name;
131 
132  /**
133  * The current conglomerated progress. Set by update().
134  */
135  float m_progress;
136 
137  /**
138  * Set of all child progress.
139  */
140  std::set< boost::shared_ptr< WProgress > > m_children;
141 
142  /**
143  * Lock for the above child set and the internal state update.
144  */
145  mutable boost::shared_mutex m_updateLock;
146 
147 private:
148 };
149 
150 #endif // WPROGRESSCOMBINER_H
151 
virtual float getProgress()
Returns the overall progress of this progress instance, including the child progress'.
std::string m_name
The name of the combiner.
Class managing progress inside of modules.
Definition: WProgress.h:43
float m_progress
The current conglomerated progress.
virtual ~WProgressCombiner()
Destructor.
virtual void addSubProgress(boost::shared_ptr< WProgress > progress)
Adds a new progress to this combiner.
boost::shared_mutex m_updateLock
Lock for the above child set and the internal state update.
Base class for all kinds of progress combinations.
std::string getCombinedNames(bool excludeFinished=false) const
Generates a string combined out of every child progress name.
std::set< boost::shared_ptr< WProgress > > m_children
Set of all child progress.
virtual void finish()
Stops the progress.
virtual void removeSubProgress(boost::shared_ptr< WProgress > progress)
Removes the specified sub progress from this combiner.
boost::shared_ptr< WProgressCombiner > SPtr
Abbreviate shared_ptr for this class.
virtual WProgressCombiner & operator++()
Simple increment operator to signal a forward stepping.
boost::shared_ptr< const WProgressCombiner > ConstSPtr
Abbreviate shared_ptr for this class.
WProgressCombiner(std::string name="")
Default constructor.
virtual void update()
Function updating the internal state.
Class testing the functionality of progress combiners.