libzypp 17.31.23
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
26namespace 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:
291 { return _d->_min; }
292
295 { return _d->_max; }
296
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
Progress callback from another progress.
Definition: progressdata.h:394
bool operator()(const ProgressData &progress)
Implements the ProgressData::ReceiverFnc callback interface.
ProgressData::value_type _weight
Definition: progressdata.h:420
ProgressData::value_type _last_value
Definition: progressdata.h:421
Helper to create and pass std::istream.
Definition: inputstream.h:57
std::chrono::steady_clock::time_point TimePoint
Definition: progressdata.h:148
Data(value_type min_r, value_type max_r, value_type val_r)
Definition: progressdata.h:150
Maintain [min,max] and counter (value) for progress counting.
Definition: progressdata.h:132
bool tick()
Leave counter value unchanged (still alive).
Definition: progressdata.h:280
bool decr(value_type val_r=1)
Decrement counter value (default by 1).
Definition: progressdata.h:268
bool reportPercent() const
Definition: progressdata.h:309
bool set(const ProgressData &rhs)
Set range and counter from an other ProgressData.
Definition: progressdata.h:256
value_type reportValue() const
Definition: progressdata.h:322
void noRange()
Set no range [0,0].
Definition: progressdata.h:212
long long value_type
Definition: progressdata.h:134
void sendTo(const ReceiverFnc &fnc_r)
Set ReceiverFnc.
Definition: progressdata.h:229
bool reportAlive() const
Definition: progressdata.h:316
ProgressData(value_type min_r, value_type max_r)
Ctor [min,max](min).
Definition: progressdata.h:184
const ReceiverFnc & receiver() const
Definition: progressdata.h:330
ProgressData(value_type min_r, value_type max_r, value_type val_r)
Ctor [min,max](val).
Definition: progressdata.h:189
bool toMax()
Set counter value to current max value (unless no range).
Definition: progressdata.h:276
void name(const std::string &name_r)
Set counter name.
Definition: progressdata.h:225
ProgressData(value_type max_r)
Ctor [0,max](0).
Definition: progressdata.h:179
void noSend()
Set no ReceiverFnc.
Definition: progressdata.h:233
bool report()
Send report if necessary.
Definition: progressdata.cc:33
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition: progressdata.h:140
void max(value_type max_r)
Set new max value.
Definition: progressdata.h:208
void range(value_type min_r, value_type max_r)
Set new [min,max].
Definition: progressdata.h:220
void min(value_type min_r)
Set new min value.
Definition: progressdata.h:204
bool incr(value_type val_r=1)
Increment counter value (default by 1).
Definition: progressdata.h:264
const std::string & name() const
Definition: progressdata.h:326
bool toMin()
Set counter value to current min value.
Definition: progressdata.h:272
ProgressData()
Ctor no range [0,0](0).
Definition: progressdata.h:174
value_type max() const
Definition: progressdata.h:294
bool set(value_type val_r)
Set new counter value.
Definition: progressdata.h:249
value_type min() const
Definition: progressdata.h:290
bool finalReport() const
Definition: progressdata.h:336
bool hasRange() const
Definition: progressdata.h:302
value_type val() const
Definition: progressdata.h:298
RWCOW_pointer< Data > _d
Pointer to data.
Definition: progressdata.h:346
void range(value_type max_r)
Set new [0,max].
Definition: progressdata.h:216
String related utilities and Regular expression matching.
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
ProgressData makeProgressData(const InputStream &input_r)
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition: SerialNumber.cc:52
RW_pointer supporting 'copy on write' functionality.
Definition: PtrTypes.h:459
Base class for objects providing a numeric Id.