libzypp  17.38.7
logichelpers.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
9 #ifndef ZYPP_NG_LOGICHELPERS_INCLUDED
10 #define ZYPP_NG_LOGICHELPERS_INCLUDED
11 
12 #include <zypp-core/ng/async/awaitable.h>
13 #include <zypp/ng/context.h>
14 
15 namespace zyppng
16 {
17 
18  namespace detail {
19  template <typename Op, typename = void>
20  struct LogicBaseExec : public Op { };
21 
22  template <typename Op>
23  struct LogicBaseExec<Op, std::enable_if_t<detail::is_async_op_v<Op>>> : public Op
24  {
25  protected:
26  AsyncOpRef<typename Op::value_type> _innerPipeline;
27  };
28 
29  }
30 
36  {
37  // Make the isAsync flag accessible for subclasses.
38  static constexpr bool is_async = ZYPP_IS_ASYNC;
39 
43  template<class Type>
44  using MaybeAsyncRef = MaybeAwaitable<Type>;
45  };
46 
51  #define ZYPP_ENABLE_MAYBE_ASYNC_MIXIN( IsAsync ) \
52  template<class T> \
53  using MaybeAsyncRef = MaybeAwaitable<T>
54 
55 
56 
148  template <typename Executor, typename OpType>
149  struct LogicBase : public detail::LogicBaseExec<OpType>, public MaybeAsyncMixin {
150 
151  using ExecutorType = Executor;
152  using Result = typename OpType::value_type;
153 
154  LogicBase( ){ }
155  virtual ~LogicBase(){}
156 
157  template <typename ...Args>
158  static auto run( Args &&...args ) {
159 #ifdef ZYPP_ENABLE_ASYNC
160  auto op = std::make_shared<Executor>( std::forward<Args>(args)... );
161  op->asyncExecute();
162  return op;
163 #else
164  return Executor( std::forward<Args>(args)... ).execute();
165 #endif
166  }
167 
172  Executor *executor () {
173  return static_cast<Executor *>(this);
174  }
175 
176  private:
177 #ifdef ZYPP_ENABLE_ASYNC
178  void asyncExecute() {
179  this->_innerPipeline = static_cast<Executor*>(this)->execute();
180  this->_innerPipeline->onReady([this]( auto &&val ){
181  this->setReady( std::forward<decltype(val)>(val) );
182  });
183  }
184 #endif
185  };
186 
191  template <typename Result>
192  struct SyncOp : public Base {
193  using value_type = Result;
194  };
195 
196 
197  template <template<typename, typename> typename Logic , typename OpType>
198  struct SimpleExecutor : public Logic<SimpleExecutor<Logic, OpType>, OpType>
199  {
200  public:
201  template <typename ...Args>
202  SimpleExecutor( Args &&...args ) : Logic<SimpleExecutor<Logic, OpType>, OpType>( std::forward<Args>(args)...) {}
203  };
204 
205 
210  #define ZYPP_ENABLE_LOGIC_BASE(Executor, OpType) \
211  using LogicBase<Executor, OpType>::executor; \
212  template<class T> \
213  using MaybeAsyncRef = typename LogicBase<Executor, OpType>:: template MaybeAsyncRef<T>
214 }
215 #endif
Executor ExecutorType
Definition: logichelpers.h:151
SimpleExecutor(Args &&...args)
Definition: logichelpers.h:202
Definition: ansi.h:854
typename enable_if< B, T >::type enable_if_t
Definition: TypeTraits.h:45
MaybeAwaitable< Type > MaybeAsyncRef
Definition: logichelpers.h:44
Executor * executor()
Definition: logichelpers.h:172
virtual ~LogicBase()
Definition: logichelpers.h:155
static constexpr bool is_async
Definition: logichelpers.h:38
typename OpType::value_type Result
Definition: logichelpers.h:152
static auto run(Args &&...args)
Definition: logichelpers.h:158