libzypp: zypp-core/ui/progressdata.h Source File

libzypp 17.38.11

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-core/base/PtrTypes.h>
20#include <zypp-core/base/Function.h>
21#include <zypp-core/base/ProvideNumericId>
22
23#include <zypp-core/Date.h>
24
26namespace zypp
27{
28
30 //
31 // CLASS NAME : ProgressData
32 //
131 class ZYPP_API ProgressData : public base::ProvideNumericId<ProgressData,unsigned>
132 {
133 public:
134 using value_type = long long;
140 using ReceiverFnc = function<bool (const ProgressData &)>;
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:
156 State _state;
157 std::string _name;
158 value_type _min;
159 value_type _max;
160 value_type _val;
161
162 ReceiverFnc _receiver;
163 value_type _last_val;
164 TimePoint _last_send;
165
166 private:
168 friend Data * rwcowClone<Data>( const Data * rhs );
169 Data * clone() const { return new Data( *this ); }
170 };
171
172 public:
174 ProgressData()
175 : _d( new Data( 0, 0, 0 ) )
176 {}
177
179 ProgressData( value_type max_r )
180 : _d( new Data( 0, max_r, 0 ) )
181 {}
182
184 ProgressData( value_type min_r, value_type max_r )
185 : _d( new Data( min_r, max_r, min_r ) )
186 {}
187
189 ProgressData( value_type min_r, value_type max_r, value_type val_r )
190 : _d( new Data( min_r, max_r, val_r ) )
191 {}
192
193 ~ProgressData()
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:
289
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
322 value_type reportValue() const
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
346 RWCOW_pointer<Data> _d;
347 };
348
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
393 class ZYPP_API CombinedProgressData
394 {
395 public:
410 CombinedProgressData( ProgressData &pd,
411 ProgressData::value_type weight = 0 );
412
417 bool operator()( const ProgressData &progress );
418
419 private:
420 ProgressData::value_type _weight;
421 ProgressData::value_type _last_value;
422 ProgressData &_pd;
423 };
424
426} // namespace zypp
428#endif // ZYPP_CORE_UI_PROGRESSDATA_H
Date.h
Function.h
PtrTypes.h
bool
zypp::CombinedProgressData::_pd
ProgressData & _pd
Definition progressdata.h:422
zypp::CombinedProgressData::operator()
bool operator()(const ProgressData &progress)
Implements the ProgressData::ReceiverFnc callback interface.
Definition progressdata.cc:142
zypp::CombinedProgressData::_weight
ProgressData::value_type _weight
Definition progressdata.h:420
zypp::CombinedProgressData::CombinedProgressData
CombinedProgressData(ProgressData &pd, ProgressData::value_type weight=0)
Ctor.
Definition progressdata.cc:133
zypp::CombinedProgressData::_last_value
ProgressData::value_type _last_value
Definition progressdata.h:421
zypp::InputStream
Helper to create and pass std::istream.
Definition inputstream.h:57
zypp::ProgressData::Data
Definition progressdata.h:146
zypp::ProgressData::Data::clone
Data * clone() const
Definition progressdata.h:169
zypp::ProgressData::Data::_last_val
value_type _last_val
Definition progressdata.h:163
zypp::ProgressData::Data::_min
value_type _min
Definition progressdata.h:158
zypp::ProgressData::Data::rwcowClone
friend Data * rwcowClone(const Data *rhs)
clone for RWCOW_pointer
zypp::ProgressData::Data::_max
value_type _max
Definition progressdata.h:159
zypp::ProgressData::Data::_last_send
TimePoint _last_send
Definition progressdata.h:164
zypp::ProgressData::Data::TimePoint
std::chrono::steady_clock::time_point TimePoint
Definition progressdata.h:148
zypp::ProgressData::Data::_receiver
ReceiverFnc _receiver
Definition progressdata.h:162
zypp::ProgressData::Data::_state
State _state
Definition progressdata.h:156
zypp::ProgressData::Data::_val
value_type _val
Definition progressdata.h:160
zypp::ProgressData::Data::Data
Data(value_type min_r, value_type max_r, value_type val_r)
Definition progressdata.h:150
zypp::ProgressData::Data::_name
std::string _name
Definition progressdata.h:157
zypp::ProgressData
Maintain [min,max] and counter (value) for progress counting.
Definition progressdata.h:132
zypp::ProgressData::tick
bool tick()
Leave counter value unchanged (still alive).
Definition progressdata.h:280
zypp::ProgressData::decr
bool decr(value_type val_r=1)
Decrement counter value (default by 1).
Definition progressdata.h:268
zypp::ProgressData::value_type
long long value_type
Definition progressdata.h:134
zypp::ProgressData::reportPercent
bool reportPercent() const
Definition progressdata.h:309
zypp::ProgressData::set
bool set(const ProgressData &rhs)
Set range and counter from an other ProgressData.
Definition progressdata.h:256
zypp::ProgressData::reportValue
value_type reportValue() const
Definition progressdata.h:322
zypp::ProgressData::noRange
void noRange()
Set no range [0,0].
Definition progressdata.h:212
zypp::ProgressData::sendTo
void sendTo(const ReceiverFnc &fnc_r)
Set ReceiverFnc.
Definition progressdata.h:229
zypp::ProgressData::reportAlive
bool reportAlive() const
Definition progressdata.h:316
zypp::ProgressData::ProgressData
ProgressData(value_type min_r, value_type max_r)
Ctor [min,max](min).
Definition progressdata.h:184
zypp::ProgressData::receiver
const ReceiverFnc & receiver() const
Definition progressdata.h:330
zypp::ProgressData::ProgressData
ProgressData(value_type min_r, value_type max_r, value_type val_r)
Ctor [min,max](val).
Definition progressdata.h:189
zypp::ProgressData::toMax
bool toMax()
Set counter value to current max value (unless no range).
Definition progressdata.h:276
zypp::ProgressData::name
void name(const std::string &name_r)
Set counter name.
Definition progressdata.h:225
zypp::ProgressData::ProgressData
ProgressData(value_type max_r)
Ctor [0,max](0).
Definition progressdata.h:179
zypp::ProgressData::noSend
void noSend()
Set no ReceiverFnc.
Definition progressdata.h:233
zypp::ProgressData::ReceiverFnc
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition progressdata.h:140
zypp::ProgressData::report
bool report()
Send report if necessary.
Definition progressdata.cc:33
zypp::ProgressData::State
State
Definition progressdata.h:143
zypp::ProgressData::RUN
@ RUN
Definition progressdata.h:143
zypp::ProgressData::END
@ END
Definition progressdata.h:143
zypp::ProgressData::INIT
@ INIT
Definition progressdata.h:143
zypp::ProgressData::~ProgressData
~ProgressData()
Definition progressdata.h:193
zypp::ProgressData::max
void max(value_type max_r)
Set new max value.
Definition progressdata.h:208
zypp::ProgressData::range
void range(value_type min_r, value_type max_r)
Set new [min,max].
Definition progressdata.h:220
zypp::ProgressData::min
void min(value_type min_r)
Set new min value.
Definition progressdata.h:204
zypp::ProgressData::incr
bool incr(value_type val_r=1)
Increment counter value (default by 1).
Definition progressdata.h:264
zypp::ProgressData::name
const std::string & name() const
Definition progressdata.h:326
zypp::ProgressData::toMin
bool toMin()
Set counter value to current min value.
Definition progressdata.h:272
zypp::ProgressData::ProgressData
ProgressData()
Ctor no range [0,0](0).
Definition progressdata.h:174
zypp::ProgressData::max
value_type max() const
Definition progressdata.h:294
zypp::ProgressData::set
bool set(value_type val_r)
Set new counter value.
Definition progressdata.h:249
zypp::ProgressData::min
value_type min() const
Definition progressdata.h:290
zypp::ProgressData::finalReport
bool finalReport() const
Definition progressdata.h:336
zypp::ProgressData::hasRange
bool hasRange() const
Definition progressdata.h:302
zypp::ProgressData::val
value_type val() const
Definition progressdata.h:298
zypp::ProgressData::_d
RWCOW_pointer< Data > _d
Pointer to data.
Definition progressdata.h:346
zypp::ProgressData::range
void range(value_type max_r)
Set new [0,max].
Definition progressdata.h:216
str
String related utilities and Regular expression matching.
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::makeProgressData
ProgressData makeProgressData(const InputStream &input_r)
relates: ProgressData Setup from InputStream.
Definition progressdata.cc:124
zypp::ZYPP_API
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition Arch.h:173
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const Capabilities &obj)
relates: Capabilities Stream output
Definition Capabilities.cc:65
zypp::RWCOW_pointer
RW_pointer supporting 'copy on write' functionality.
Definition PtrTypes.h:469
zypp::base::ProvideNumericId
Base class for objects providing a numeric Id.
Definition providenumericid.h:46