libzypp  17.31.31
progressdata.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_CORE_UI_PROGRESSDATA_H
13 #define ZYPP_CORE_UI_PROGRESSDATA_H
14 
15 #include <iosfwd>
16 #include <string>
17 #include <chrono>
18 
19 #include <zypp/base/PtrTypes.h>
20 #include <zypp/base/Function.h>
21 #include <zypp-core/base/ProvideNumericId>
22 
23 #include <zypp/Date.h>
24 
26 namespace zypp
27 {
28 
30  //
31  // CLASS NAME : ProgressData
32  //
131  class ProgressData : public base::ProvideNumericId<ProgressData,unsigned>
132  {
133  public:
134  typedef long long value_type;
140  typedef function<bool( const ProgressData & )> ReceiverFnc;
141 
142  private:
143  enum State { INIT, RUN, END };
144 
145  class Data
146  {
147  public:
148  using TimePoint = std::chrono::steady_clock::time_point;
149 
150  Data( value_type min_r, value_type max_r, value_type val_r )
151  : _state( INIT ), _min( min_r ), _max( max_r ), _val( val_r )
152  , _last_val( 0 )
153  {}
154 
155  public:
157  std::string _name;
161 
165 
166  private:
168  friend Data * rwcowClone<Data>( const Data * rhs );
169  Data * clone() const { return new Data( *this ); }
170  };
171 
172  public:
175  : _d( new Data( 0, 0, 0 ) )
176  {}
177 
180  : _d( new Data( 0, max_r, 0 ) )
181  {}
182 
185  : _d( new Data( min_r, max_r, min_r ) )
186  {}
187 
190  : _d( new Data( min_r, max_r, val_r ) )
191  {}
192 
194  {
195  if ( _d->_state == RUN )
196  {
197  _d->_state = END;
198  report();
199  }
200  }
201 
202  public:
204  void min( value_type min_r )
205  { _d->_min = min_r; }
206 
208  void max( value_type max_r )
209  { _d->_max = max_r; }
210 
212  void noRange()
213  { range( 0, 0 ); }
214 
216  void range( value_type max_r )
217  { range( 0, max_r ); }
218 
220  void range( value_type min_r, value_type max_r )
221  { min( min_r ); max( max_r ); }
222 
223  public:
225  void name( const std::string & name_r )
226  { _d->_name = name_r; }
227 
229  void sendTo( const ReceiverFnc & fnc_r )
230  { _d->_receiver = fnc_r; }
231 
233  void noSend()
234  { _d->_receiver = ReceiverFnc(); }
235 
236  public:
247 
249  bool set( value_type val_r )
250  {
251  _d->_val = val_r;
252  return report();
253  }
254 
256  bool set( const ProgressData & rhs )
257  {
258  min( rhs.min() );
259  max( rhs.max() );
260  return set( rhs.val() );
261  }
262 
264  bool incr( value_type val_r = 1 )
265  { return set( val() + val_r ); }
266 
268  bool decr( value_type val_r = 1 )
269  { return set( val() - val_r ); }
270 
272  bool toMin()
273  { return set( min() ); }
274 
276  bool toMax()
277  { return hasRange() ? set( max() ) : tick(); }
278 
280  bool tick()
281  { return report(); }
282 
284 
285  public:
290  value_type min() const
291  { return _d->_min; }
292 
294  value_type max() const
295  { return _d->_max; }
296 
298  value_type val() const
299  { return _d->_val; }
300 
302  bool hasRange() const
303  { return min() != max(); }
304 
309  bool reportPercent() const
310  { return hasRange(); }
311 
316  bool reportAlive() const
317  { return ! hasRange(); }
318 
323  { return hasRange() ? val() * 100 / ( max() - min() ) : -1; }
324 
326  const std::string & name() const
327  { return _d->_name; }
328 
330  const ReceiverFnc & receiver() const
331  { return _d->_receiver; }
332 
336  bool finalReport() const
337  { return( _d->_state == END ); }
338 
340 
341  private:
343  bool report();
344 
347  };
349 
351  std::ostream & operator<<( std::ostream & str, const ProgressData & obj );
352 
354 
355  class InputStream;
357  ProgressData makeProgressData( const InputStream & input_r );
358 
360 
394  {
395  public:
411  ProgressData::value_type weight = 0 );
412 
417  bool operator()( const ProgressData &progress );
418 
419  private:
423  };
424 
426 } // namespace zypp
428 #endif // ZYPP_CORE_UI_PROGRESSDATA_H
Data(value_type min_r, value_type max_r, value_type val_r)
Definition: progressdata.h:150
ProgressData makeProgressData(const InputStream &input_r)
ProgressData()
Ctor no range [0,0](0).
Definition: progressdata.h:174
void sendTo(const ReceiverFnc &fnc_r)
Set ReceiverFnc.
Definition: progressdata.h:229
bool decr(value_type val_r=1)
Decrement counter value (default by 1).
Definition: progressdata.h:268
ProgressData(value_type min_r, value_type max_r, value_type val_r)
Ctor [min,max](val).
Definition: progressdata.h:189
String related utilities and Regular expression matching.
bool finalReport() const
Definition: progressdata.h:336
bool toMax()
Set counter value to current max value (unless no range).
Definition: progressdata.h:276
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition: SerialNumber.cc:52
value_type max() const
Definition: progressdata.h:294
Helper to create and pass std::istream.
Definition: inputstream.h:56
void min(value_type min_r)
Set new min value.
Definition: progressdata.h:204
void max(value_type max_r)
Set new max value.
Definition: progressdata.h:208
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition: progressdata.h:140
bool report()
Send report if necessary.
Definition: progressdata.cc:33
bool reportAlive() const
Definition: progressdata.h:316
void range(value_type max_r)
Set new [0,max].
Definition: progressdata.h:216
value_type min() const
Definition: progressdata.h:290
Progress callback from another progress.
Definition: progressdata.h:393
Base class for objects providing a numeric Id.
const ReceiverFnc & receiver() const
Definition: progressdata.h:330
bool toMin()
Set counter value to current min value.
Definition: progressdata.h:272
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:184
Maintain [min,max] and counter (value) for progress counting.
Definition: progressdata.h:131
CombinedProgressData(ProgressData &pd, ProgressData::value_type weight=0)
Ctor.
void noRange()
Set no range [0,0].
Definition: progressdata.h:212
bool incr(value_type val_r=1)
Increment counter value (default by 1).
Definition: progressdata.h:264
void noSend()
Set no ReceiverFnc.
Definition: progressdata.h:233
value_type val() const
Definition: progressdata.h:298
const std::string & name() const
Definition: progressdata.h:326
ProgressData::value_type _weight
Definition: progressdata.h:420
ProgressData(value_type max_r)
Ctor [0,max](0).
Definition: progressdata.h:179
value_type reportValue() const
Definition: progressdata.h:322
bool tick()
Leave counter value unchanged (still alive).
Definition: progressdata.h:280
std::chrono::steady_clock::time_point TimePoint
Definition: progressdata.h:148
long long value_type
Definition: progressdata.h:134
void range(value_type min_r, value_type max_r)
Set new [min,max].
Definition: progressdata.h:220
bool hasRange() const
Definition: progressdata.h:302
void name(const std::string &name_r)
Set counter name.
Definition: progressdata.h:225
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
ProgressData::value_type _last_value
Definition: progressdata.h:421
bool reportPercent() const
Definition: progressdata.h:309
RW_pointer supporting &#39;copy on write&#39; functionality.
Definition: PtrTypes.h:458
RWCOW_pointer< Data > _d
Pointer to data.
Definition: progressdata.h:346