libzypp  17.34.1
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/zyppng/base/Base>
13 #include <zypp-core/zyppng/base/Signals>
14 #include <zypp-core/zyppng/pipelines/Expected>
15 #include <optional>
16 #include <string>
17 
18 // backwards compat
20 
21 
22 namespace zyppng {
23 
24  ZYPP_FWD_DECL_TYPE_WITH_REFS( ProgressObserver );
25  class ProgressObserverPrivate;
26 
27  class ProgressObserver : public Base
28  {
31 
32  public:
33  ZYPP_DECL_PRIVATE_CONSTR_ARGS(ProgressObserver, const std::string &label = std::string(), int steps = 100 );
34 
35  void setBaseSteps( int steps );
36  int baseSteps ( ) const;
37  int steps ( ) const;
38 
39  void reset ( );
40  void setCurrent ( double curr );
41  void setFinished ( );
42  void inc ( double inc = 1.0, const std::optional<std::string> &newLabel = {} );
43 
44  double progress() const;
45  double current() const;
46 
47  const std::vector<zyppng::ProgressObserverRef> &children();
48 
49  void setLabel ( const std::string &label );
50  const std::string &label () const;
51 
52  void registerSubTask ( const ProgressObserverRef& child, float weight = 1.0 );
53 
55 
62 
63  };
64 
65 
66  namespace operators {
67 
68  namespace detail {
69 
70  enum class progress_helper_mode {
71  Increase,
72  Set
73  };
74 
75  template <auto mode = progress_helper_mode::Increase>
76  struct progress_helper {
77 
78  progress_helper( ProgressObserverRef &&progressObserver, std::optional<std::string> &&newStr, double inc )
79  : _progressObserver( std::move(progressObserver) )
80  , _newString( std::move(newStr) )
81  , _progressInc( inc )
82  {}
83 
84  template <typename T>
85  void operator() ( T && ) {
86  if ( _progressObserver ) {
87  if constexpr ( mode == progress_helper_mode::Increase ) {
89  } else {
90  _progressObserver->setCurrent ( _progressInc );
91  if ( _newString )
92  _progressObserver->setLabel ( *_newString );
93  }
94  }
95  }
96 
97  private:
98  ProgressObserverRef _progressObserver;
99  std::optional<std::string> _newString;
100  double _progressInc;
101  };
102  }
103 
108  inline auto incProgress( ProgressObserverRef progressObserver, double progrIncrease = 1.0, std::optional<std::string> newStr = {} ) {
109  return detail::progress_helper<detail::progress_helper_mode::Increase>( std::move(progressObserver), std::move(newStr), progrIncrease );
110  }
111 
116  inline auto setProgress( ProgressObserverRef progressObserver, double progrValue, std::optional<std::string> newStr = {} ) {
117  return detail::progress_helper<detail::progress_helper_mode::Set>( std::move(progressObserver), std::move(newStr), progrValue );
118  }
119 
123  inline auto setProgressLabel( ProgressObserverRef progressObserver, std::string &&newStr ) {
124  // use the Increase functor, it allows us to let the progress value untouched and just sets the strings
125  return detail::progress_helper<detail::progress_helper_mode::Increase>( std::move(progressObserver), std::move(newStr), 0.0 );
126  }
127 
131  inline auto finishProgress( ProgressObserverRef progressObserver ) {
132  return [ progressObserver = std::move(progressObserver) ]( auto &&val ) {
133  if ( progressObserver ) progressObserver->setFinished ();
134  return std::forward<decltype(val)>(val);
135  };
136  }
137 
138 
139  }
140 
141 } // namespace zyppng
142 
143 #endif // ZYPPNG_PROGRESSOBSERVER_H
SignalProxy< void(ProgressObserver &sender)> sigFinished()
const std::string & label() const
const std::vector< zyppng::ProgressObserverRef > & children()
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition: progressdata.h:140
ZYPP_DECL_PRIVATE_CONSTR_ARGS(ProgressObserver, const std::string &label=std::string(), int steps=100)
#define ZYPP_ADD_CREATE_FUNC(Class)
Definition: zyppglobal.h:196
Definition: Arch.h:363
auto setProgressLabel(ProgressObserverRef progressObserver, std::string &&newStr)
SignalProxy< void(ProgressObserver &sender, double current) > sigValueChanged()
void setLabel(const std::string &label)
zypp::ProgressData::ReceiverFnc makeProgressDataReceiver()
ZYPP_FWD_DECL_TYPE_WITH_REFS(Context)
SignalProxy< void(ProgressObserver &sender, const std::string &str)> sigLabelChanged()
void setCurrent(double curr)
#define ZYPP_DECLARE_PRIVATE(Class)
Definition: zyppglobal.h:86
SignalProxy< void(ProgressObserver &sender, double steps)> sigStepsChanged()
auto setProgress(ProgressObserverRef progressObserver, double progrValue, std::optional< std::string > newStr={})
progress_helper(ProgressObserverRef &&progressObserver, std::optional< std::string > &&newStr, double inc)
SignalProxy< void(ProgressObserver &sender, ProgressObserverRef child)> sigNewSubprogress()
SignalProxy< void(ProgressObserver &sender, double progress)> sigProgressChanged()
auto finishProgress(ProgressObserverRef progressObserver)
void inc(double inc=1.0, const std::optional< std::string > &newLabel={})
void registerSubTask(const ProgressObserverRef &child, float weight=1.0)
auto incProgress(ProgressObserverRef progressObserver, double progrIncrease=1.0, std::optional< std::string > newStr={})
std::optional< std::string > _newString