libzypp: zypp/ProgressData.h Source File

libzypp 17.25.10

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>
20 #include <zypp/base/ProvideNumericId.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:
153 State _state;
154 std::string _name;
155 value_type _min;
156 value_type _max;
157 value_type _val;
158
159 ReceiverFnc _receiver;
160 value_type _last_val;
161 Date _last_send;
162
163 private:
165 friend Data * rwcowClone<Data>( const Data * rhs );
166 Data * clone() const { return new Data( *this ); }
167 };
168
169 public:
171 ProgressData()
172 : _d( new Data( 0, 0, 0 ) )
173 {}
174
176 ProgressData( value_type max_r )
177 : _d( new Data( 0, max_r, 0 ) )
178 {}
179
181 ProgressData( value_type min_r, value_type max_r )
182 : _d( new Data( min_r, max_r, min_r ) )
183 {}
184
186 ProgressData( value_type min_r, value_type max_r, value_type val_r )
187 : _d( new Data( min_r, max_r, val_r ) )
188 {}
189
190 ~ProgressData()
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
319 value_type reportValue() const
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
343 RWCOW_pointer<Data> _d;
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
390 class CombinedProgressData
391 {
392 public:
407 CombinedProgressData( ProgressData &pd,
408 ProgressData::value_type weight = 0 );
409
414 bool operator()( const ProgressData &progress );
415
416 private:
417 ProgressData::value_type _weight;
418 ProgressData::value_type _last_value;
419 ProgressData &_pd;
420 };
421
423 } // namespace zypp
425 #endif // ZYPP_PROGRESSDATA_H
Date.h
Function.h
ProvideNumericId.h
PtrTypes.h
zypp::CombinedProgressData
Progress callback from another progress.
Definition: ProgressData.h:391
zypp::CombinedProgressData::_pd
ProgressData & _pd
Definition: ProgressData.h:419
zypp::CombinedProgressData::operator()
bool operator()(const ProgressData &progress)
Implements the ProgressData::ReceiverFnc callback interface.
Definition: ProgressData.cc:135
zypp::CombinedProgressData::_weight
ProgressData::value_type _weight
Definition: ProgressData.h:417
zypp::CombinedProgressData::CombinedProgressData
CombinedProgressData(ProgressData &pd, ProgressData::value_type weight=0)
Ctor.
Definition: ProgressData.cc:126
zypp::CombinedProgressData::_last_value
ProgressData::value_type _last_value
Definition: ProgressData.h:418
zypp::Date
Store and operate on date (time_t).
Definition: Date.h:33
zypp::InputStream
Helper to create and pass std::istream.
Definition: InputStream.h:57
zypp::ProgressData::Data
Definition: ProgressData.h:145
zypp::ProgressData::Data::_last_val
value_type _last_val
Definition: ProgressData.h:160
zypp::ProgressData::Data::_min
value_type _min
Definition: ProgressData.h:155
zypp::ProgressData::Data::_max
value_type _max
Definition: ProgressData.h:156
zypp::ProgressData::Data::_receiver
ReceiverFnc _receiver
Definition: ProgressData.h:159
zypp::ProgressData::Data::_last_send
Date _last_send
Definition: ProgressData.h:161
zypp::ProgressData::Data::_state
State _state
Definition: ProgressData.h:153
zypp::ProgressData::Data::_val
value_type _val
Definition: ProgressData.h:157
zypp::ProgressData::Data::Data
Data(value_type min_r, value_type max_r, value_type val_r)
Definition: ProgressData.h:147
zypp::ProgressData::Data::clone
Data * clone() const
Definition: ProgressData.h:166
zypp::ProgressData::Data::_name
std::string _name
Definition: ProgressData.h:154
zypp::ProgressData
Maintain [min,max] and counter (value) for progress counting.
Definition: ProgressData.h:131
zypp::ProgressData::tick
bool tick()
Leave counter value unchanged (still alive).
Definition: ProgressData.h:277
zypp::ProgressData::decr
bool decr(value_type val_r=1)
Decrement counter value (default by 1).
Definition: ProgressData.h:265
zypp::ProgressData::reportPercent
bool reportPercent() const
Definition: ProgressData.h:306
zypp::ProgressData::name
const std::string & name() const
Definition: ProgressData.h:323
zypp::ProgressData::receiver
const ReceiverFnc & receiver() const
Definition: ProgressData.h:327
zypp::ProgressData::set
bool set(const ProgressData &rhs)
Set range and counter from an other ProgressData.
Definition: ProgressData.h:253
zypp::ProgressData::reportValue
value_type reportValue() const
Definition: ProgressData.h:319
zypp::ProgressData::noRange
void noRange()
Set no range [0,0].
Definition: ProgressData.h:209
zypp::ProgressData::value_type
long long value_type
Definition: ProgressData.h:133
zypp::ProgressData::sendTo
void sendTo(const ReceiverFnc &fnc_r)
Set ReceiverFnc.
Definition: ProgressData.h:226
zypp::ProgressData::reportAlive
bool reportAlive() const
Definition: ProgressData.h:313
zypp::ProgressData::ProgressData
ProgressData(value_type min_r, value_type max_r)
Ctor [min,max](min).
Definition: ProgressData.h:181
zypp::ProgressData::ProgressData
ProgressData(value_type min_r, value_type max_r, value_type val_r)
Ctor [min,max](val).
Definition: ProgressData.h:186
zypp::ProgressData::toMax
bool toMax()
Set counter value to current max value (unless no range).
Definition: ProgressData.h:273
zypp::ProgressData::name
void name(const std::string &name_r)
Set counter name.
Definition: ProgressData.h:222
zypp::ProgressData::ProgressData
ProgressData(value_type max_r)
Ctor [0,max](0).
Definition: ProgressData.h:176
zypp::ProgressData::noSend
void noSend()
Set no ReceiverFnc.
Definition: ProgressData.h:230
zypp::ProgressData::report
bool report()
Send report if necessary.
Definition: ProgressData.cc:33
zypp::ProgressData::State
State
Definition: ProgressData.h:142
zypp::ProgressData::RUN
@ RUN
Definition: ProgressData.h:142
zypp::ProgressData::END
@ END
Definition: ProgressData.h:142
zypp::ProgressData::INIT
@ INIT
Definition: ProgressData.h:142
zypp::ProgressData::ReceiverFnc
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition: ProgressData.h:139
zypp::ProgressData::~ProgressData
~ProgressData()
Definition: ProgressData.h:190
zypp::ProgressData::max
void max(value_type max_r)
Set new max value.
Definition: ProgressData.h:205
zypp::ProgressData::range
void range(value_type min_r, value_type max_r)
Set new [min,max].
Definition: ProgressData.h:217
zypp::ProgressData::min
void min(value_type min_r)
Set new min value.
Definition: ProgressData.h:201
zypp::ProgressData::incr
bool incr(value_type val_r=1)
Increment counter value (default by 1).
Definition: ProgressData.h:261
zypp::ProgressData::toMin
bool toMin()
Set counter value to current min value.
Definition: ProgressData.h:269
zypp::ProgressData::ProgressData
ProgressData()
Ctor no range [0,0](0).
Definition: ProgressData.h:171
zypp::ProgressData::max
value_type max() const
Definition: ProgressData.h:291
zypp::ProgressData::set
bool set(value_type val_r)
Set new counter value.
Definition: ProgressData.h:246
zypp::ProgressData::min
value_type min() const
Definition: ProgressData.h:287
zypp::ProgressData::finalReport
bool finalReport() const
Definition: ProgressData.h:333
zypp::ProgressData::hasRange
bool hasRange() const
Definition: ProgressData.h:299
zypp::ProgressData::val
value_type val() const
Definition: ProgressData.h:295
zypp::ProgressData::_d
RWCOW_pointer< Data > _d
Pointer to data.
Definition: ProgressData.h:343
zypp::ProgressData::range
void range(value_type max_r)
Set new [0,max].
Definition: ProgressData.h:213
str
String related utilities and Regular expression matching.
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:147
zypp::makeProgressData
ProgressData makeProgressData(const InputStream &input_r)
Definition: ProgressData.cc:117
zypp::RWCOW_pointer
RW_pointer supporting 'copy on write' functionality.
Definition: PtrTypes.h:459
zypp::base::ProvideNumericId
Base class for objects providing a numeric Id.
Definition: ProvideNumericId.h:46