libzypp 17.37.10

wait.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8----------------------------------------------------------------------/
9*
10* This file contains private API, this might break at any time between releases.
11* You have been warned!
12*/
13
14#ifndef ZYPP_ZYPPNG_MONADIC_WAIT_H
15#define ZYPP_ZYPPNG_MONADIC_WAIT_H
16
17#include <zypp-core/zyppng/pipelines/AsyncResult>
18#include <functional>
19
20 namespace zyppng {
21
22 namespace detail {
23
24 template < template< class, class... > class Container,
25 class AsyncOp,
26 class ...CArgs
27 >
28 struct WaitForImpl : public zyppng::AsyncOp< Container<typename AsyncOp::value_type> > {
29
30 using AsyncOpRes = typename AsyncOp::value_type;
31
32 static_assert( detail::is_async_op_v<AsyncOp>, "Result type needs to be derived from AsyncOp");
33
34 WaitForImpl ( std::function<bool( const AsyncOpRes &)> canContinue = {} )
35 : _canContinue( std::move(canContinue) ){};
36
37 WaitForImpl ( const WaitForImpl &other ) = delete;
38 WaitForImpl& operator= ( const WaitForImpl &other ) = delete;
39
40 WaitForImpl& operator= ( WaitForImpl &&other ) = default;
41 WaitForImpl ( WaitForImpl &&other ) = default;
42
43 void operator()( Container< std::shared_ptr<AsyncOp>, CArgs...> &&ops ) {
44 assert( _allOps.empty() );
45
46 if ( ops.empty () ) {
47 this->setReady( std::move(_allResults) );
48 }
49
50 _allOps = std::move( ops );
51 for ( auto &op : _allOps ) {
52 op->onReady( [ this ]( AsyncOpRes &&res ) {
53 this->resultReady( std::move(res));
54 });
55 }
56 }
57
58 private:
59 void resultReady ( AsyncOpRes &&res ) {
60 _allResults.push_back( std::move( res ) );
61
62 bool done = ( _allOps.size() == _allResults.size()) || ( _canContinue && !_canContinue(_allResults.back()));
63 if ( done ) {
64 //release all ops we waited on
65 _allOps.clear();
66
67 this->setReady( std::move(_allResults) );
68 }
69 }
70
71 Container< std::shared_ptr<AsyncOp>, CArgs... > _allOps;
72 Container< AsyncOpRes > _allResults;
73 std::function<bool( const AsyncOpRes &)> _canContinue;
74 };
75
76 struct WaitForHelper
77 {
78 template<
79 template< class, class... > class Container,
80 class AsyncOp,
81 typename ...CArgs,
82 std::enable_if_t< detail::is_async_op_v<AsyncOp>, int> = 0
83 >
84 auto operator()( Container< std::shared_ptr< AsyncOp >, CArgs... > &&ops ) -> zyppng::AsyncOpRef< Container<typename AsyncOp::value_type> > {
85 auto aOp = std::make_shared<detail::WaitForImpl<Container, AsyncOp, CArgs...>>( );
86 aOp->operator()( std::move(ops) );
87 return aOp;
88 }
89
90 template<
91 template< class, class... > class Container,
92 class Res,
93 typename ...CArgs,
94 std::enable_if_t< !detail::is_async_op_v<Res>, int> = 0
95 >
96 auto operator()( Container< Res, CArgs... > ops ) -> Container< Res, CArgs... > {
97 return ops;
98 }
99 };
100
101
102 template <typename AsyncOpRes>
103 struct WaitForHelperExt
104 {
105 WaitForHelperExt( std::function<bool( const AsyncOpRes &)> &&fn ) : _cb( std::move(fn) ) {}
106
107 template<
108 template< class, class... > class Container,
109 class AsyncOp,
110 typename ...CArgs,
111 std::enable_if_t< detail::is_async_op_v<AsyncOp>, int> = 0
112 >
113 auto operator()( Container< std::shared_ptr< AsyncOp >, CArgs... > &&ops ) -> zyppng::AsyncOpRef< Container<typename AsyncOp::value_type> > {
114 auto aOp = std::make_shared<detail::WaitForImpl<Container, AsyncOp, CArgs...>>( _cb );
115 aOp->operator()( std::move(ops) );
116 return aOp;
117 }
118
119 private:
120 std::function<bool( const AsyncOpRes &)> _cb;
121 };
122
123}
124
129 inline auto waitFor ( ) {
130 return detail::WaitForHelper(); //std::make_shared<detail::WaitForImpl<zyppng::AsyncOp<Res>>>();
131}
132
133 inline auto join ( ) {
134 return detail::WaitForHelper();
135}
136
137}
138#endif
bool
std
Definition Arch.h:364
zyppng::detail
Definition reporthelper.h:31
zyppng::detail::is_async_op_v
constexpr bool is_async_op_v
Definition asyncop.h:61
zyppng
Definition MediaNetworkRequestExecutor.h:16
zyppng::join
auto join()
Definition wait.h:133
zyppng::AsyncOpRef
std::shared_ptr< AsyncOp< T > > AsyncOpRef
Definition asyncop.h:255
zyppng::waitFor
auto waitFor()
Definition wait.h:129
zyppng::AsyncOp
Definition asyncop.h:161
zyppng::AsyncOp< Container< AsyncOp::value_type > >::setReady
void setReady(value_type &&val)
Definition asyncop.h:183
zyppng::AsyncOp::value_type
Result value_type
Definition asyncop.h:165
zyppng::detail::WaitForHelperExt::WaitForHelperExt
WaitForHelperExt(std::function< bool(const AsyncOpRes &)> &&fn)
Definition wait.h:105
zyppng::detail::WaitForHelperExt::_cb
std::function< bool(const AsyncOpRes &)> _cb
Definition wait.h:120
zyppng::detail::WaitForHelper
Definition wait.h:77
zyppng::detail::WaitForImpl
Definition wait.h:28
zyppng::detail::WaitForImpl::resultReady
void resultReady(AsyncOpRes &&res)
Definition wait.h:59
zyppng::detail::WaitForImpl::_allOps
Container< std::shared_ptr< AsyncOp >, CArgs... > _allOps
Definition wait.h:71
zyppng::detail::WaitForImpl::WaitForImpl
WaitForImpl(const WaitForImpl &other)=delete
zyppng::detail::WaitForImpl::WaitForImpl
WaitForImpl(std::function< bool(const AsyncOpRes &)> canContinue={})
Definition wait.h:34
zyppng::detail::WaitForImpl::operator()
void operator()(Container< std::shared_ptr< AsyncOp >, CArgs... > &&ops)
Definition wait.h:43
zyppng::detail::WaitForImpl::WaitForImpl
WaitForImpl(WaitForImpl &&other)=default
zyppng::detail::WaitForImpl::AsyncOpRes
typename AsyncOp::value_type AsyncOpRes
Definition wait.h:30
zyppng::detail::WaitForImpl::_allResults
Container< AsyncOpRes > _allResults
Definition wait.h:72
zyppng::detail::WaitForImpl::_canContinue
std::function< bool(const AsyncOpRes &)> _canContinue
Definition wait.h:73