libzypp: zypp-core/ng/ui/progressobserver.h Source File

libzypp 17.38.14

progressobserver.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9#ifndef ZYPPNG_PROGRESSOBSERVER_H
10#define ZYPPNG_PROGRESSOBSERVER_H
11
12#include <zypp-core/ng/base/Base>
13#include <zypp-core/ng/base/Signals>
14#include <zypp-core/ng/pipelines/Expected>
15#include <optional>
16#include <string>
17
18// backwards compat
19#include <zypp-core/ui/progressdata.h>
20
21
22namespace zyppng {
23
24 ZYPP_FWD_DECL_TYPE_WITH_REFS( ProgressObserver );
25 class ProgressObserverPrivate;
26
27 class ProgressObserver : public Base
28 {
29 ZYPP_DECLARE_PRIVATE(ProgressObserver)
30 ZYPP_ADD_CREATE_FUNC(ProgressObserver)
31
32 public:
33
34 enum FinishResult {
35 Success,
36 Error
37 };
38
39 ZYPP_DECL_PRIVATE_CONSTR_ARGS(ProgressObserver, const std::string &label = std::string(), int steps = 100 );
40
41 void setBaseSteps( int steps );
42 int baseSteps ( ) const;
43 int steps ( ) const;
44 bool started ( ) const;
45
52 void start ( );
53 void reset ( );
54 void setCurrent ( double curr );
55 void setFinished ( FinishResult result = Success );
56 void inc ( double inc = 1.0, const std::optional<std::string> &newLabel = {} );
57
58 double progress() const;
59 double current() const;
60
61
62 inline static ProgressObserverRef makeSubTask( ProgressObserverRef parentProgress, float weight = 1.0, const std::string &label = std::string(), int steps = 100 ) {
63 if ( parentProgress ) return parentProgress->makeSubTask( weight, label, steps );
64 return nullptr;
65 }
66
67 inline static void start ( ProgressObserverRef progress ) {
68 if ( progress ) progress->start();
69 }
70
71 inline static void setup( ProgressObserverRef progress, const std::string &label = std::string(), int steps = 100 ) {
72 if ( progress ) {
73 progress->setLabel( label );
74 progress->setBaseSteps ( steps );
75 }
76 }
77
78 inline static void increase( ProgressObserverRef progress, double inc = 1.0, const std::optional<std::string> &newLabel = {} ) {
79 if ( progress ) progress->inc ( inc, newLabel );
80 }
81
82 inline static void setCurrent ( ProgressObserverRef progress, double curr ) {
83 if ( progress ) progress->setCurrent ( curr );
84 }
85
86 inline static void setLabel ( ProgressObserverRef progress, const std::string &label ) {
87 if ( progress ) progress->setLabel ( label );
88 }
89
90 inline static void setSteps ( ProgressObserverRef progress, int steps ) {
91 if ( progress ) progress->setBaseSteps ( steps );
92 }
93
94 inline static void finish ( ProgressObserverRef progress, ProgressObserver::FinishResult result = ProgressObserver::Success ) {
95 if ( progress ) progress->setFinished ( result );
96 }
97
98 const std::vector<zyppng::ProgressObserverRef> &children();
99
100 void setLabel ( const std::string &label );
101 const std::string &label () const;
102
103 void registerSubTask ( const ProgressObserverRef& child, float weight = 1.0 );
104
105 ProgressObserverRef makeSubTask( float weight = 1.0 ,const std::string &label = std::string(), int steps = 100 );
106
107 zypp::ProgressData::ReceiverFnc makeProgressDataReceiver ();
108
109 SignalProxy<void ( ProgressObserver &sender )> sigStarted ();
110 SignalProxy<void ( ProgressObserver &sender, const std::string &str )> sigLabelChanged ();
111 SignalProxy<void ( ProgressObserver &sender, double steps )> sigStepsChanged();
112 SignalProxy<void ( ProgressObserver &sender, double current ) > sigValueChanged();
113 SignalProxy<void ( ProgressObserver &sender, double progress )> sigProgressChanged();
114 SignalProxy<void ( ProgressObserver &sender, FinishResult result )> sigFinished();
115 SignalProxy<void ( ProgressObserver &sender, ProgressObserverRef child )> sigNewSubprogress();
116
117 };
118
119 namespace operators {
120
121 namespace detail {
122
123 enum class progress_helper_mode {
124 Start,
125 Increase,
126 Set,
127 Finish
128 };
129
130 template <progress_helper_mode mode>
131 struct progress_helper {
132
133 progress_helper( ProgressObserverRef &&progressObserver, std::optional<std::string> &&newStr, double inc )
134 : _progressObserver( std::move(progressObserver) )
135 , _newString( std::move(newStr) )
136 , _progressInc( inc )
137 {}
138
139 template <typename T>
140 auto operator() ( T &&t ) {
141 update();
142 return std::forward<T>(t);
143 }
144
145 void operator()() {
146 update();
147 }
148
149 void update() {
150 if ( _progressObserver ) {
151 if constexpr ( mode == progress_helper_mode::Increase ) {
152 _progressObserver->inc( _progressInc, _newString );
153 } else {
154 _progressObserver->setCurrent ( _progressInc );
155 if ( _newString )
156 _progressObserver->setLabel ( *_newString );
157 }
158 }
159 }
160
161 private:
162 ProgressObserverRef _progressObserver;
163 std::optional<std::string> _newString;
164 double _progressInc;
165 };
166
167 template <>
168 struct progress_helper<progress_helper_mode::Start>
169 {
170 progress_helper( ProgressObserverRef &&progressObserver )
171 : _progressObserver( std::move(progressObserver) ){}
172
173 template <typename T>
174 auto operator() ( T &&t ) {
175 update();
176 return std::forward<T>(t);
177 }
178
179 void operator()() {
180 update();
181 }
182
183 void update() {
184 if ( _progressObserver ) { _progressObserver->start(); }
185 }
186
187 private:
188 ProgressObserverRef _progressObserver;
189 };
190
191 template <>
192 struct progress_helper<progress_helper_mode::Finish>
193 {
194 progress_helper( ProgressObserverRef &&progressObserver, ProgressObserver::FinishResult result = ProgressObserver::Success )
195 : _progressObserver( std::move(progressObserver) )
196 , _result(result){}
197
198 template <typename T>
199 auto operator() ( T &&t ) {
200 update();
201 return std::forward<T>(t);
202 }
203
204 void operator()() {
205 update();
206 }
207
208 void update() {
209 if ( _progressObserver ) { _progressObserver->setFinished( _result ); }
210 }
211
212 private:
213 ProgressObserverRef _progressObserver;
214 ProgressObserver::FinishResult _result;
215 };
216 }
217
221 inline auto startProgress( ProgressObserverRef progressObserver ) {
222 return detail::progress_helper<detail::progress_helper_mode::Start>( std::move(progressObserver) );
223 }
224
229 inline auto incProgress( ProgressObserverRef progressObserver, double progrIncrease = 1.0, std::optional<std::string> newStr = {} ) {
230 return detail::progress_helper<detail::progress_helper_mode::Increase>( std::move(progressObserver), std::move(newStr), progrIncrease );
231 }
232
237 inline auto setProgress( ProgressObserverRef progressObserver, double progrValue, std::optional<std::string> newStr = {} ) {
238 return detail::progress_helper<detail::progress_helper_mode::Set>( std::move(progressObserver), std::move(newStr), progrValue );
239 }
240
244 inline auto setProgressLabel( ProgressObserverRef progressObserver, std::string newStr ) {
245 // use the Increase functor, it allows us to let the progress value untouched and just sets the strings
246 return detail::progress_helper<detail::progress_helper_mode::Increase>( std::move(progressObserver), std::move(newStr), 0.0 );
247 }
248
252 inline auto finishProgress( ProgressObserverRef progressObserver, ProgressObserver::FinishResult result = ProgressObserver::Success ) {
253 return detail::progress_helper<detail::progress_helper_mode::Finish>( std::move(progressObserver), result );
254 }
255
256
257 }
258
259} // namespace zyppng
260
261#endif // ZYPPNG_PROGRESSOBSERVER_H
zypp::ProgressData::ReceiverFnc
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition progressdata.h:140
zyppng::Base::Base
Base()
Definition base.cc:20
zyppng::ProgressObserverPrivate
Definition progressobserver.cc:9
zyppng::ProgressObserver
Definition progressobserver.h:28
zyppng::ProgressObserver::sigValueChanged
SignalProxy< void(ProgressObserver &sender, double current) > sigValueChanged()
Definition progressobserver.cc:221
zyppng::ProgressObserver::sigStarted
SignalProxy< void(ProgressObserver &sender)> sigStarted()
Definition progressobserver.cc:206
zyppng::ProgressObserver::inc
void inc(double inc=1.0, const std::optional< std::string > &newLabel={})
Definition progressobserver.cc:295
zyppng::ProgressObserver::sigFinished
SignalProxy< void(ProgressObserver &sender, FinishResult result)> sigFinished()
Definition progressobserver.cc:231
zyppng::ProgressObserver::setSteps
static void setSteps(ProgressObserverRef progress, int steps)
Definition progressobserver.h:90
zyppng::ProgressObserver::increase
static void increase(ProgressObserverRef progress, double inc=1.0, const std::optional< std::string > &newLabel={})
Definition progressobserver.h:78
zyppng::ProgressObserver::steps
int steps() const
Definition progressobserver.cc:153
zyppng::ProgressObserver::setBaseSteps
void setBaseSteps(int steps)
Definition progressobserver.cc:241
zyppng::ProgressObserver::setLabel
static void setLabel(ProgressObserverRef progress, const std::string &label)
Definition progressobserver.h:86
zyppng::ProgressObserver::ZYPP_DECL_PRIVATE_CONSTR_ARGS
ZYPP_DECL_PRIVATE_CONSTR_ARGS(ProgressObserver, const std::string &label=std::string(), int steps=100)
zyppng::ProgressObserver::setCurrent
static void setCurrent(ProgressObserverRef progress, double curr)
Definition progressobserver.h:82
zyppng::ProgressObserver::makeSubTask
static ProgressObserverRef makeSubTask(ProgressObserverRef parentProgress, float weight=1.0, const std::string &label=std::string(), int steps=100)
Definition progressobserver.h:62
zyppng::ProgressObserver::children
const std::vector< zyppng::ProgressObserverRef > & children()
Definition progressobserver.cc:196
zyppng::ProgressObserver::sigStepsChanged
SignalProxy< void(ProgressObserver &sender, double steps)> sigStepsChanged()
Definition progressobserver.cc:216
zyppng::ProgressObserver::current
double current() const
Definition progressobserver.cc:191
zyppng::ProgressObserver::makeProgressDataReceiver
zypp::ProgressData::ReceiverFnc makeProgressDataReceiver()
Definition progressobserver.cc:344
zyppng::ProgressObserver::label
const std::string & label() const
Definition progressobserver.cc:201
zyppng::ProgressObserver::started
bool started() const
Definition progressobserver.cc:158
zyppng::ProgressObserver::setup
static void setup(ProgressObserverRef progress, const std::string &label=std::string(), int steps=100)
Definition progressobserver.h:71
zyppng::ProgressObserver::progress
double progress() const
Definition progressobserver.cc:186
zyppng::ProgressObserver::finish
static void finish(ProgressObserverRef progress, ProgressObserver::FinishResult result=ProgressObserver::Success)
Definition progressobserver.h:94
zyppng::ProgressObserver::setCurrent
void setCurrent(double curr)
Definition progressobserver.cc:258
zyppng::ProgressObserver::sigProgressChanged
SignalProxy< void(ProgressObserver &sender, double progress)> sigProgressChanged()
Definition progressobserver.cc:226
zyppng::ProgressObserver::start
static void start(ProgressObserverRef progress)
Definition progressobserver.h:67
zyppng::ProgressObserver::setFinished
void setFinished(FinishResult result=Success)
Definition progressobserver.cc:275
zyppng::ProgressObserver::reset
void reset()
Definition progressobserver.cc:175
zyppng::ProgressObserver::baseSteps
int baseSteps() const
Definition progressobserver.cc:270
zyppng::ProgressObserver::FinishResult
FinishResult
Definition progressobserver.h:34
zyppng::ProgressObserver::Error
@ Error
Definition progressobserver.h:36
zyppng::ProgressObserver::Success
@ Success
Definition progressobserver.h:35
zyppng::ProgressObserver::sigNewSubprogress
SignalProxy< void(ProgressObserver &sender, ProgressObserverRef child)> sigNewSubprogress()
Definition progressobserver.cc:236
zyppng::ProgressObserver::registerSubTask
void registerSubTask(const ProgressObserverRef &child, float weight=1.0)
Definition progressobserver.cc:301
zyppng::ProgressObserver::sigLabelChanged
SignalProxy< void(ProgressObserver &sender, const std::string &str)> sigLabelChanged()
Definition progressobserver.cc:211
zyppng::ProgressObserver::start
void start()
Definition progressobserver.cc:163
zyppng::SignalProxy
Definition signals.h:325
std
Definition ansi.h:855
str
String related utilities and Regular expression matching.
zyppng::operators::detail::progress_helper_mode
progress_helper_mode
Definition progressobserver.h:123
zyppng::operators::detail::progress_helper_mode::Set
@ Set
Definition progressobserver.h:126
zyppng::operators::detail::progress_helper_mode::Finish
@ Finish
Definition progressobserver.h:127
zyppng::operators::detail::progress_helper_mode::Start
@ Start
Definition progressobserver.h:124
zyppng::operators::detail::progress_helper_mode::Increase
@ Increase
Definition progressobserver.h:125
zyppng::operators::setProgress
auto setProgress(ProgressObserverRef progressObserver, double progrValue, std::optional< std::string > newStr={})
Definition progressobserver.h:237
zyppng::operators::finishProgress
auto finishProgress(ProgressObserverRef progressObserver, ProgressObserver::FinishResult result=ProgressObserver::Success)
Definition progressobserver.h:252
zyppng::operators::setProgressLabel
auto setProgressLabel(ProgressObserverRef progressObserver, std::string newStr)
Definition progressobserver.h:244
zyppng::operators::incProgress
auto incProgress(ProgressObserverRef progressObserver, double progrIncrease=1.0, std::optional< std::string > newStr={})
Definition progressobserver.h:229
zyppng::operators::startProgress
auto startProgress(ProgressObserverRef progressObserver)
Definition progressobserver.h:221
zyppng
Definition MediaNetworkRequestExecutor.h:16
progressdata.h
zyppng::operators::detail::progress_helper< progress_helper_mode::Finish >::_result
ProgressObserver::FinishResult _result
Definition progressobserver.h:214
zyppng::operators::detail::progress_helper< progress_helper_mode::Finish >::update
void update()
Definition progressobserver.h:208
zyppng::operators::detail::progress_helper< progress_helper_mode::Finish >::operator()
void operator()()
Definition progressobserver.h:204
zyppng::operators::detail::progress_helper< progress_helper_mode::Finish >::_progressObserver
ProgressObserverRef _progressObserver
Definition progressobserver.h:213
zyppng::operators::detail::progress_helper< progress_helper_mode::Finish >::progress_helper
progress_helper(ProgressObserverRef &&progressObserver, ProgressObserver::FinishResult result=ProgressObserver::Success)
Definition progressobserver.h:194
zyppng::operators::detail::progress_helper< progress_helper_mode::Start >::operator()
void operator()()
Definition progressobserver.h:179
zyppng::operators::detail::progress_helper< progress_helper_mode::Start >::progress_helper
progress_helper(ProgressObserverRef &&progressObserver)
Definition progressobserver.h:170
zyppng::operators::detail::progress_helper< progress_helper_mode::Start >::_progressObserver
ProgressObserverRef _progressObserver
Definition progressobserver.h:188
zyppng::operators::detail::progress_helper< progress_helper_mode::Start >::update
void update()
Definition progressobserver.h:183
zyppng::operators::detail::progress_helper
Definition progressobserver.h:131
zyppng::operators::detail::progress_helper::_newString
std::optional< std::string > _newString
Definition progressobserver.h:163
zyppng::operators::detail::progress_helper::operator()
void operator()()
Definition progressobserver.h:145
zyppng::operators::detail::progress_helper::update
void update()
Definition progressobserver.h:149
zyppng::operators::detail::progress_helper::_progressInc
double _progressInc
Definition progressobserver.h:164
zyppng::operators::detail::progress_helper::progress_helper
progress_helper(ProgressObserverRef &&progressObserver, std::optional< std::string > &&newStr, double inc)
Definition progressobserver.h:133
zyppng::operators::detail::progress_helper::_progressObserver
ProgressObserverRef _progressObserver
Definition progressobserver.h:162
ZYPP_ADD_CREATE_FUNC
#define ZYPP_ADD_CREATE_FUNC(Class)
Definition zyppglobal.h:198
ZYPP_DECLARE_PRIVATE
#define ZYPP_DECLARE_PRIVATE(Class)
Definition zyppglobal.h:80
ZYPP_FWD_DECL_TYPE_WITH_REFS
#define ZYPP_FWD_DECL_TYPE_WITH_REFS(T)
Definition zyppglobal.h:119