libzypp 17.37.17
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 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:
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:
289
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 };
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
424
426} // namespace zypp
428#endif // ZYPP_CORE_UI_PROGRESSDATA_H
bool operator()(const zypp::Arch &lhs, const zypp::Arch &rhs) const
Default order for std::container based Arch::compare.
Definition Arch.h:370
ProgressData::value_type _weight
CombinedProgressData(ProgressData &pd, ProgressData::value_type weight=0)
Ctor.
ProgressData::value_type _last_value
Helper to create and pass std::istream.
Definition inputstream.h:57
friend Data * rwcowClone(const Data *rhs)
clone for RWCOW_pointer
std::chrono::steady_clock::time_point TimePoint
Data(value_type min_r, value_type max_r, value_type val_r)
Maintain [min,max] and counter (value) for progress counting.
bool tick()
Leave counter value unchanged (still alive).
bool decr(value_type val_r=1)
Decrement counter value (default by 1).
bool reportPercent() const
bool set(const ProgressData &rhs)
Set range and counter from an other ProgressData.
value_type reportValue() const
void noRange()
Set no range [0,0].
void sendTo(const ReceiverFnc &fnc_r)
Set ReceiverFnc.
bool reportAlive() const
ProgressData(value_type min_r, value_type max_r)
Ctor [min,max](min).
const ReceiverFnc & receiver() const
ProgressData(value_type min_r, value_type max_r, value_type val_r)
Ctor [min,max](val).
bool toMax()
Set counter value to current max value (unless no range).
void name(const std::string &name_r)
Set counter name.
ProgressData(value_type max_r)
Ctor [0,max](0).
void noSend()
Set no ReceiverFnc.
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
bool report()
Send report if necessary.
void max(value_type max_r)
Set new max value.
void range(value_type min_r, value_type max_r)
Set new [min,max].
void min(value_type min_r)
Set new min value.
bool incr(value_type val_r=1)
Increment counter value (default by 1).
const std::string & name() const
bool toMin()
Set counter value to current min value.
ProgressData()
Ctor no range [0,0](0).
value_type max() const
bool set(value_type val_r)
Set new counter value.
value_type min() const
bool finalReport() const
bool hasRange() const
value_type val() const
RWCOW_pointer< Data > _d
Pointer to data.
void range(value_type max_r)
Set new [0,max].
String related utilities and Regular expression matching.
Easy-to use interface to the ZYPP dependency resolver.
ProgressData makeProgressData(const InputStream &input_r)
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
const Arch Arch_armv7hnl Arch_armv7nhl ZYPP_API
Definition Arch.h:247
RW_pointer supporting 'copy on write' functionality.
Definition PtrTypes.h:469
Base class for objects providing a numeric Id.