libzypp 17.38.5

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
35 struct MaybeAsyncMixin
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
Logic
zyppng::Base::Base
Base()
Definition base.cc:20
context.h
std
Definition ansi.h:855
zyppng::detail
Definition reporthelper.h:31
zyppng
Definition MediaNetworkRequestExecutor.h:16
zyppng::LogicBase::~LogicBase
virtual ~LogicBase()
Definition logichelpers.h:155
zyppng::LogicBase::executor
Executor * executor()
Definition logichelpers.h:172
zyppng::LogicBase::LogicBase
LogicBase()
Definition logichelpers.h:154
zyppng::LogicBase::run
static auto run(Args &&...args)
Definition logichelpers.h:158
zyppng::LogicBase::Result
typename OpType::value_type Result
Definition logichelpers.h:152
zyppng::LogicBase::ExecutorType
Executor ExecutorType
Definition logichelpers.h:151
zyppng::MaybeAsyncMixin
Definition logichelpers.h:36
zyppng::MaybeAsyncMixin::is_async
static constexpr bool is_async
Definition logichelpers.h:38
zyppng::MaybeAsyncMixin::MaybeAsyncRef
MaybeAwaitable< Type > MaybeAsyncRef
Definition logichelpers.h:44
zyppng::SimpleExecutor::SimpleExecutor
SimpleExecutor(Args &&...args)
Definition logichelpers.h:202
zyppng::SyncOp
Definition logichelpers.h:192
zyppng::SyncOp::value_type
Result value_type
Definition logichelpers.h:193
zyppng::detail::LogicBaseExec< Op, std::enable_if_t< detail::is_async_op_v< Op > > >::_innerPipeline
AsyncOpRef< typename Op::value_type > _innerPipeline
Definition logichelpers.h:26
zyppng::detail::LogicBaseExec
Definition logichelpers.h:20