libzypp  17.35.16
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 
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>
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
Definition: Arch.h:363
std::function< bool(const AsyncOpRes &)> _cb
Definition: wait.h:120
typename enable_if< B, T >::type enable_if_t
Definition: TypeTraits.h:45
Container< std::shared_ptr< AsyncOp >, CArgs... > _allOps
Definition: wait.h:71
Container< AsyncOpRes > _allResults
Definition: wait.h:72
auto join()
Definition: wait.h:133
std::function< bool(const AsyncOpRes &)> _canContinue
Definition: wait.h:73
std::shared_ptr< AsyncOp< T > > AsyncOpRef
Definition: asyncop.h:255
void resultReady(AsyncOpRes &&res)
Definition: wait.h:59
auto waitFor()
Definition: wait.h:129
typename AsyncOp::value_type AsyncOpRes
Definition: wait.h:30
void operator()(Container< std::shared_ptr< AsyncOp >, CArgs... > &&ops)
Definition: wait.h:43
WaitForHelperExt(std::function< bool(const AsyncOpRes &)> &&fn)
Definition: wait.h:105
Result value_type
Definition: asyncop.h:165
WaitForImpl(std::function< bool(const AsyncOpRes &)> canContinue={})
Definition: wait.h:34