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