libzypp  13.10.6
ProgressData.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_PROGRESSDATA_H
13 #define ZYPP_PROGRESSDATA_H
14 
15 #include <iosfwd>
16 #include <string>
17 
18 #include "zypp/base/PtrTypes.h"
19 #include "zypp/base/Function.h"
21 
22 #include "zypp/Date.h"
23 
25 namespace zypp
26 {
27 
29  //
30  // CLASS NAME : ProgressData
31  //
135  class ProgressData : public base::ProvideNumericId<ProgressData,unsigned>
136  {
137  public:
138  typedef long long value_type;
144  typedef function<bool( const ProgressData & )> ReceiverFnc;
145 
146  private:
147  enum State { INIT, RUN, END };
148 
149  class Data
150  {
151  public:
152  Data( value_type min_r, value_type max_r, value_type val_r )
153  : _state( INIT ), _min( min_r ), _max( max_r ), _val( val_r )
154  , _last_val( 0 ), _last_send( 0 )
155  {}
156 
157  public:
159  std::string _name;
163 
167 
168  private:
170  friend Data * rwcowClone<Data>( const Data * rhs );
171  Data * clone() const { return new Data( *this ); }
172  };
173 
174  public:
177  : _d( new Data( 0, 0, 0 ) )
178  {}
179 
182  : _d( new Data( 0, max_r, 0 ) )
183  {}
184 
187  : _d( new Data( min_r, max_r, min_r ) )
188  {}
189 
192  : _d( new Data( min_r, max_r, val_r ) )
193  {}
194 
196  {
197  if ( _d->_state == RUN )
198  {
199  _d->_state = END;
200  report();
201  }
202  }
203 
204  public:
206  void min( value_type min_r )
207  { _d->_min = min_r; }
208 
210  void max( value_type max_r )
211  { _d->_max = max_r; }
212 
214  void noRange()
215  { range( 0, 0 ); }
216 
218  void range( value_type max_r )
219  { range( 0, max_r ); }
220 
222  void range( value_type min_r, value_type max_r )
223  { min( min_r ); max( max_r ); }
224 
225  public:
227  void name( const std::string & name_r )
228  { _d->_name = name_r; }
229 
231  void sendTo( const ReceiverFnc & fnc_r )
232  { _d->_receiver = fnc_r; }
233 
235  void noSend()
236  { _d->_receiver = ReceiverFnc(); }
237 
238  public:
249 
251  bool set( value_type val_r )
252  {
253  _d->_val = val_r;
254  return report();
255  }
256 
258  bool incr( value_type val_r = 1 )
259  { return set( val() + val_r ); }
260 
262  bool decr( value_type val_r = 1 )
263  { return set( val() - val_r ); }
264 
266  bool toMin()
267  { return set( min() ); }
268 
270  bool toMax()
271  { return set( hasRange() ? max() : val() ); }
272 
274  bool tick()
275  { return set( val() ); }
276 
278 
279  public:
284  value_type min() const
285  { return _d->_min; }
286 
288  value_type max() const
289  { return _d->_max; }
290 
292  value_type val() const
293  { return _d->_val; }
294 
296  bool hasRange() const
297  { return min() != max(); }
298 
303  bool reportPercent() const
304  { return hasRange(); }
305 
310  bool reportAlive() const
311  { return ! hasRange(); }
312 
317  { return hasRange() ? val() * 100 / ( max() - min() ) : -1; }
318 
320  const std::string & name() const
321  { return _d->_name; }
322 
324  const ReceiverFnc & receiver() const
325  { return _d->_receiver; }
326 
330  bool finalReport() const
331  { return( _d->_state == END ); }
332 
334 
335  private:
337  bool report();
338 
341  };
343 
345  std::ostream & operator<<( std::ostream & str, const ProgressData & obj );
346 
348 
349  class InputStream;
351  ProgressData makeProgressData( const InputStream & input_r );
352 
354 
388  {
389  public:
405  ProgressData::value_type weight = 0 );
406 
411  bool operator()( const ProgressData &progress );
412 
413  private:
417  };
418 
420 } // namespace zypp
422 #endif // ZYPP_PROGRESSDATA_H
Data(value_type min_r, value_type max_r, value_type val_r)
Definition: ProgressData.h:152
const std::string & name() const
Definition: ProgressData.h:320
const ReceiverFnc & receiver() const
Definition: ProgressData.h:324
bool reportPercent() const
Definition: ProgressData.h:303
ProgressData makeProgressData(const InputStream &input_r)
bool reportAlive() const
Definition: ProgressData.h:310
ProgressData()
Ctor no range [0,0](0).
Definition: ProgressData.h:176
void sendTo(const ReceiverFnc &fnc_r)
Set ReceiverFnc.
Definition: ProgressData.h:231
bool decr(value_type val_r=1)
Decrement counter value (default by 1).
Definition: ProgressData.h:262
Data * clone() const
Definition: ProgressData.h:171
ProgressData(value_type min_r, value_type max_r, value_type val_r)
Ctor [min,max](val).
Definition: ProgressData.h:191
value_type max() const
Definition: ProgressData.h:288
bool finalReport() const
Definition: ProgressData.h:330
bool toMax()
Set counter value to current max value (unless no range).
Definition: ProgressData.h:270
Helper to create and pass std::istream.
Definition: InputStream.h:56
void min(value_type min_r)
Set new min value.
Definition: ProgressData.h:206
void max(value_type max_r)
Set new max value.
Definition: ProgressData.h:210
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition: ProgressData.h:144
bool hasRange() const
Definition: ProgressData.h:296
bool report()
Send report if necessary.
Definition: ProgressData.cc:33
void range(value_type max_r)
Set new [0,max].
Definition: ProgressData.h:218
Progress callback from another progress.
Definition: ProgressData.h:387
Base class for objects providing a numeric Id.
bool toMin()
Set counter value to current min value.
Definition: ProgressData.h:266
Store and operate on date (time_t).
Definition: Date.h:31
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:120
bool operator()(const ProgressData &progress)
Implements the ProgressData::ReceiverFnc callback interface.
ProgressData(value_type min_r, value_type max_r)
Ctor [min,max](min).
Definition: ProgressData.h:186
Maintain [min,max] and counter (value) for progress counting.
Definition: ProgressData.h:135
CombinedProgressData(ProgressData &pd, ProgressData::value_type weight=0)
Ctor.
void noRange()
Set no range [0,0].
Definition: ProgressData.h:214
bool incr(value_type val_r=1)
Increment counter value (default by 1).
Definition: ProgressData.h:258
void noSend()
Set no ReceiverFnc.
Definition: ProgressData.h:235
ProgressData::value_type _weight
Definition: ProgressData.h:414
value_type val() const
Definition: ProgressData.h:292
ProgressData(value_type max_r)
Ctor [0,max](0).
Definition: ProgressData.h:181
bool tick()
Leave counter value unchanged (still alive).
Definition: ProgressData.h:274
bool set(value_type val_r)
Set new counter value.
Definition: ProgressData.h:251
value_type min() const
Definition: ProgressData.h:284
long long value_type
Definition: ProgressData.h:138
void range(value_type min_r, value_type max_r)
Set new [min,max].
Definition: ProgressData.h:222
void name(const std::string &name_r)
Set counter name.
Definition: ProgressData.h:227
ProgressData::value_type _last_value
Definition: ProgressData.h:415
value_type reportValue() const
Definition: ProgressData.h:316
RW_pointer supporting &#39;copy on write&#39; functionality.
Definition: PtrTypes.h:438
RWCOW_pointer< Data > _d
Pointer to data.
Definition: ProgressData.h:340