libzypp  17.16.0
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  //
130  class ProgressData : public base::ProvideNumericId<ProgressData,unsigned>
131  {
132  public:
133  typedef long long value_type;
139  typedef function<bool( const ProgressData & )> ReceiverFnc;
140 
141  private:
142  enum State { INIT, RUN, END };
143 
144  class Data
145  {
146  public:
147  Data( value_type min_r, value_type max_r, value_type val_r )
148  : _state( INIT ), _min( min_r ), _max( max_r ), _val( val_r )
149  , _last_val( 0 ), _last_send( 0 )
150  {}
151 
152  public:
154  std::string _name;
158 
162 
163  private:
165  friend Data * rwcowClone<Data>( const Data * rhs );
166  Data * clone() const { return new Data( *this ); }
167  };
168 
169  public:
172  : _d( new Data( 0, 0, 0 ) )
173  {}
174 
177  : _d( new Data( 0, max_r, 0 ) )
178  {}
179 
182  : _d( new Data( min_r, max_r, min_r ) )
183  {}
184 
187  : _d( new Data( min_r, max_r, val_r ) )
188  {}
189 
191  {
192  if ( _d->_state == RUN )
193  {
194  _d->_state = END;
195  report();
196  }
197  }
198 
199  public:
201  void min( value_type min_r )
202  { _d->_min = min_r; }
203 
205  void max( value_type max_r )
206  { _d->_max = max_r; }
207 
209  void noRange()
210  { range( 0, 0 ); }
211 
213  void range( value_type max_r )
214  { range( 0, max_r ); }
215 
217  void range( value_type min_r, value_type max_r )
218  { min( min_r ); max( max_r ); }
219 
220  public:
222  void name( const std::string & name_r )
223  { _d->_name = name_r; }
224 
226  void sendTo( const ReceiverFnc & fnc_r )
227  { _d->_receiver = fnc_r; }
228 
230  void noSend()
231  { _d->_receiver = ReceiverFnc(); }
232 
233  public:
244 
246  bool set( value_type val_r )
247  {
248  _d->_val = val_r;
249  return report();
250  }
251 
253  bool set( const ProgressData & rhs )
254  {
255  min( rhs.min() );
256  max( rhs.max() );
257  return set( rhs.val() );
258  }
259 
261  bool incr( value_type val_r = 1 )
262  { return set( val() + val_r ); }
263 
265  bool decr( value_type val_r = 1 )
266  { return set( val() - val_r ); }
267 
269  bool toMin()
270  { return set( min() ); }
271 
273  bool toMax()
274  { return hasRange() ? set( max() ) : tick(); }
275 
277  bool tick()
278  { return report(); }
279 
281 
282  public:
287  value_type min() const
288  { return _d->_min; }
289 
291  value_type max() const
292  { return _d->_max; }
293 
295  value_type val() const
296  { return _d->_val; }
297 
299  bool hasRange() const
300  { return min() != max(); }
301 
306  bool reportPercent() const
307  { return hasRange(); }
308 
313  bool reportAlive() const
314  { return ! hasRange(); }
315 
320  { return hasRange() ? val() * 100 / ( max() - min() ) : -1; }
321 
323  const std::string & name() const
324  { return _d->_name; }
325 
327  const ReceiverFnc & receiver() const
328  { return _d->_receiver; }
329 
333  bool finalReport() const
334  { return( _d->_state == END ); }
335 
337 
338  private:
340  bool report();
341 
344  };
346 
348  std::ostream & operator<<( std::ostream & str, const ProgressData & obj );
349 
351 
352  class InputStream;
354  ProgressData makeProgressData( const InputStream & input_r );
355 
357 
391  {
392  public:
408  ProgressData::value_type weight = 0 );
409 
414  bool operator()( const ProgressData &progress );
415 
416  private:
420  };
421 
423 } // namespace zypp
425 #endif // ZYPP_PROGRESSDATA_H
Data(value_type min_r, value_type max_r, value_type val_r)
Definition: ProgressData.h:147
ProgressData makeProgressData(const InputStream &input_r)
ProgressData()
Ctor no range [0,0](0).
Definition: ProgressData.h:171
void sendTo(const ReceiverFnc &fnc_r)
Set ReceiverFnc.
Definition: ProgressData.h:226
bool decr(value_type val_r=1)
Decrement counter value (default by 1).
Definition: ProgressData.h:265
ProgressData(value_type min_r, value_type max_r, value_type val_r)
Ctor [min,max](val).
Definition: ProgressData.h:186
String related utilities and Regular expression matching.
bool finalReport() const
Definition: ProgressData.h:333
bool toMax()
Set counter value to current max value (unless no range).
Definition: ProgressData.h:273
value_type max() const
Definition: ProgressData.h:291
Helper to create and pass std::istream.
Definition: InputStream.h:56
void min(value_type min_r)
Set new min value.
Definition: ProgressData.h:201
void max(value_type max_r)
Set new max value.
Definition: ProgressData.h:205
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition: ProgressData.h:139
bool report()
Send report if necessary.
Definition: ProgressData.cc:33
bool reportAlive() const
Definition: ProgressData.h:313
void range(value_type max_r)
Set new [0,max].
Definition: ProgressData.h:213
value_type min() const
Definition: ProgressData.h:287
Progress callback from another progress.
Definition: ProgressData.h:390
Base class for objects providing a numeric Id.
const ReceiverFnc & receiver() const
Definition: ProgressData.h:327
bool toMin()
Set counter value to current min value.
Definition: ProgressData.h:269
Store and operate on date (time_t).
Definition: Date.h:32
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:147
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:181
Maintain [min,max] and counter (value) for progress counting.
Definition: ProgressData.h:130
CombinedProgressData(ProgressData &pd, ProgressData::value_type weight=0)
Ctor.
void noRange()
Set no range [0,0].
Definition: ProgressData.h:209
bool incr(value_type val_r=1)
Increment counter value (default by 1).
Definition: ProgressData.h:261
void noSend()
Set no ReceiverFnc.
Definition: ProgressData.h:230
value_type val() const
Definition: ProgressData.h:295
const std::string & name() const
Definition: ProgressData.h:323
bool set(const ProgressData &rhs)
Set range and counter from an other ProgressData.
Definition: ProgressData.h:253
ProgressData::value_type _weight
Definition: ProgressData.h:417
ProgressData(value_type max_r)
Ctor [0,max](0).
Definition: ProgressData.h:176
value_type reportValue() const
Definition: ProgressData.h:319
bool tick()
Leave counter value unchanged (still alive).
Definition: ProgressData.h:277
bool set(value_type val_r)
Set new counter value.
Definition: ProgressData.h:246
long long value_type
Definition: ProgressData.h:133
void range(value_type min_r, value_type max_r)
Set new [min,max].
Definition: ProgressData.h:217
bool hasRange() const
Definition: ProgressData.h:299
void name(const std::string &name_r)
Set counter name.
Definition: ProgressData.h:222
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
ProgressData::value_type _last_value
Definition: ProgressData.h:418
bool reportPercent() const
Definition: ProgressData.h:306
RW_pointer supporting 'copy on write' functionality.
Definition: PtrTypes.h:458
RWCOW_pointer< Data > _d
Pointer to data.
Definition: ProgressData.h:343