libzypp: zypp/ng/workflows/logichelpers.h Source File

libzypp 17.36.6

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/zyppng/async/AsyncOp>
13
14namespace zyppng
15{
16
17 namespace detail {
18 template <typename Op, typename = void>
19 struct LogicBaseExec : public Op { };
20
21 template <typename Op>
22 struct LogicBaseExec<Op, std::enable_if_t<detail::is_async_op_v<Op>>> : public Op
23 {
24 protected:
25 AsyncOpRef<typename Op::value_type> _innerPipeline;
26 };
27
28 }
29
34 template <bool isAsync>
35 struct MaybeAsyncMixin
36 {
37 // Make the isAsync flag accessible for subclasses.
38 static constexpr bool is_async = isAsync;
39
43 template<class Type>
44 using MaybeAsyncRef = std::conditional_t< isAsync, AsyncOpRef<Type>, Type>;
45
46
47 protected:
52 template <typename T>
53 inline auto makeReadyResult( T &&res ) {
54 return zyppng::makeReadyResult<T, isAsync> ( std::forward<T>(res) );
55 }
56 };
57
62 #define ZYPP_ENABLE_MAYBE_ASYNC_MIXIN( IsAsync ) \
63 using MaybeAsyncMixin<IsAsync>::makeReadyResult; \
64 template<class T> \
65 using MaybeAsyncRef = typename MaybeAsyncMixin<IsAsync>:: template MaybeAsyncRef<T>
66
67
68
160 template <typename Executor, typename OpType>
161 struct LogicBase : public detail::LogicBaseExec<OpType>, public MaybeAsyncMixin<detail::is_async_op_v<OpType>> {
162
163 using ExecutorType = Executor;
164 using Result = typename OpType::value_type;
165
166 LogicBase( ){ }
167 virtual ~LogicBase(){}
168
169 template <typename ...Args, typename FOpType = OpType>
170 static std::enable_if_t< detail::is_async_op_v<FOpType>, AsyncOpRef<Result>> run( Args &&...args ) {
171 auto op = std::make_shared<Executor>( std::forward<Args>(args)... );
172 op->asyncExecute();
173 return op;
174 }
175
176 template <typename ...Args, typename FOpType = OpType>
177 static std::enable_if_t< !detail::is_async_op_v<FOpType>, Result> run( Args &&...args ) {
178 return Executor( std::forward<Args>(args)... ).execute();
179 }
180
181
186 Executor *executor () {
187 return static_cast<Executor *>(this);
188 }
189
190 private:
191 template <typename FOpType = OpType>
192 std::enable_if_t< detail::is_async_op_v<FOpType>, void> asyncExecute() {
193 this->_innerPipeline = static_cast<Executor*>(this)->execute();
194 this->_innerPipeline->onReady([this]( auto &&val ){
195 this->setReady( std::forward<decltype(val)>(val) );
196 });
197 }
198 };
199
204 template <typename Result>
205 struct SyncOp : public Base {
206 using value_type = Result;
207 };
208
209
210 template <template<typename, typename> typename Logic , typename OpType>
211 struct SimpleExecutor : public Logic<SimpleExecutor<Logic, OpType>, OpType>
212 {
213 public:
214 template <typename ...Args>
215 SimpleExecutor( Args &&...args ) : Logic<SimpleExecutor<Logic, OpType>, OpType>( std::forward<Args>(args)...) {}
216 };
217
218
223 #define ZYPP_ENABLE_LOGIC_BASE(Executor, OpType) \
224 using LogicBase<Executor, OpType>::executor; \
225 using LogicBase<Executor, OpType>::makeReadyResult; \
226 template<class T> \
227 using MaybeAsyncRef = typename LogicBase<Executor, OpType>:: template MaybeAsyncRef<T>
228}
229#endif
Logic
zyppng::Base::Base
Base()
Definition base.cc:20
std
Definition Arch.h:364
std::enable_if_t
typename enable_if< B, T >::type enable_if_t
Definition TypeTraits.h:45
std::conditional_t
typename conditional< B, T, F >::type conditional_t
Definition TypeTraits.h:39
zyppng::detail
Definition reporthelper.h:31
zyppng
Definition MediaCurl2.h:22
zyppng::makeReadyResult
std::conditional_t< isAsync, AsyncOpRef< T >, T > makeReadyResult(T &&result)
Definition asyncop.h:297
zyppng::AsyncOpRef
std::shared_ptr< AsyncOp< T > > AsyncOpRef
Definition asyncop.h:255
zyppng::LogicBase::~LogicBase
virtual ~LogicBase()
Definition logichelpers.h:167
zyppng::LogicBase::executor
Executor * executor()
Definition logichelpers.h:186
zyppng::LogicBase::LogicBase
LogicBase()
Definition logichelpers.h:166
zyppng::LogicBase::run
static std::enable_if_t< detail::is_async_op_v< FOpType >, AsyncOpRef< Result > > run(Args &&...args)
Definition logichelpers.h:170
zyppng::LogicBase::run
static std::enable_if_t< !detail::is_async_op_v< FOpType >, Result > run(Args &&...args)
Definition logichelpers.h:177
zyppng::LogicBase::Result
typename OpType::value_type Result
Definition logichelpers.h:164
zyppng::LogicBase::asyncExecute
std::enable_if_t< detail::is_async_op_v< FOpType >, void > asyncExecute()
Definition logichelpers.h:192
zyppng::LogicBase::ExecutorType
Executor ExecutorType
Definition logichelpers.h:163
zyppng::MaybeAsyncMixin
Definition logichelpers.h:36
zyppng::MaybeAsyncMixin::MaybeAsyncRef
std::conditional_t< isAsync, AsyncOpRef< Type >, Type > MaybeAsyncRef
Definition logichelpers.h:44
zyppng::MaybeAsyncMixin::makeReadyResult
auto makeReadyResult(T &&res)
Definition logichelpers.h:53
zyppng::MaybeAsyncMixin::is_async
static constexpr bool is_async
Definition logichelpers.h:38
zyppng::SimpleExecutor::SimpleExecutor
SimpleExecutor(Args &&...args)
Definition logichelpers.h:215
zyppng::SyncOp
Definition logichelpers.h:205
zyppng::SyncOp::value_type
Result value_type
Definition logichelpers.h:206
zyppng::detail::LogicBaseExec< Op, std::enable_if_t< detail::is_async_op_v< Op > > >::_innerPipeline
AsyncOpRef< typename Op::value_type > _innerPipeline
Definition logichelpers.h:25
zyppng::detail::LogicBaseExec
Definition logichelpers.h:19