libzypp 17.37.10

algorithm.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 ZYPPNG_PIPELINES_ALGORITHM_H_INCLUDED
15#define ZYPPNG_PIPELINES_ALGORITHM_H_INCLUDED
16
17#include <zypp-core/zyppng/async/AsyncOp>
18#include <functional>
19
20 namespace zyppng {
21
22 namespace detail {
23
24 struct ContinueUntilValidPredicate {
25 template< class Arg >
26 bool operator()( const Arg &value ) {
27 // works if operator bool() const is implemented by type Arg
28 if ( value ) return true;
29 return false;
30 }
31 };
32
33 template <typename T>
34 struct showme;
35
36 template < class Container, class AsyncResType, class Transformation, class Predicate, class DefaultType >
37 struct AsyncFirstOfImpl : public AsyncOp<AsyncResType> {
38
39 AsyncFirstOfImpl( Container &&inData, Transformation &&transFunc, DefaultType &&defaultVal, Predicate &&predicate )
40 : _inData ( std::move(inData) )
41 , _transFunc ( std::move(transFunc) )
42 , _defaultVal( std::move(defaultVal) )
43 , _predicate ( std::move(predicate))
44 , _currIter ( _inData.begin() ) { execute(); }
45
46 private:
47 void execute() {
48 // seems this is empty we are ready
49 if ( _currIter == _inData.end() ) {
50 return returnDefault();
51 }
52 invokeCurrent();
53 }
54
55 void resultReady ( AsyncResType &&res ) {
56
57 if ( !_predicate(res) ) {
58 _currIter = std::next( _currIter );
59 // seems we reached the end
60 if ( _currIter == _inData.end() ) {
61 return returnDefault();
62 }
63 invokeCurrent();
64 } else {
65 this->setReady ( std::move(res) );
66 return;
67 }
68
69 }
70
71 void invokeCurrent() {
72 _currentPipeline = std::invoke( _transFunc, std::move( *_currIter ) );
73 _currentPipeline->onReady( [ this ]( AsyncResType &&res ) {
74 this->resultReady( std::move(res));
75 });
76 }
77 void returnDefault() {
78 this->setReady ( std::move(_defaultVal) );
79 return;
80 }
81
82 private:
83 Container _inData;
84 Transformation _transFunc;
85 DefaultType _defaultVal;
86 Predicate _predicate;
87 typename Container::iterator _currIter;
88 AsyncOpRef<AsyncResType> _currentPipeline;
89 };
90
91 template < class Transformation, class Predicate, class DefaultType >
92 struct FirstOfHelper {
93
94 FirstOfHelper( Transformation transFunc, DefaultType defaultVal, Predicate predicate )
95 : _transFunc ( std::move(transFunc) )
96 , _defaultVal( std::move(defaultVal) )
97 , _predicate ( std::move(predicate)) { }
98
99 template < class Container
100 , typename ...CArgs>
101 auto operator()( Container &&container ) {
102
103 using InputType = typename Container::value_type;
104 static_assert( std::is_invocable_v<Transformation, InputType>, "Transformation function must take the container value type as input " );
105 static_assert( std::is_rvalue_reference_v<decltype(std::forward<Container>(container))>, "Input container must be a rvalue reference" );
106
107 using OutputType = std::invoke_result_t<Transformation, InputType>;
108
109 if constexpr ( detail::is_async_op_v<OutputType> ) {
110
111 using AsyncResultType = typename remove_smart_ptr_t<OutputType>::value_type;
112
113 static_assert( std::is_same_v<AsyncResultType, DefaultType>, "Default type and transformation result type must match" );
114
115 using ImplType = AsyncFirstOfImpl<Container, AsyncResultType,Transformation, Predicate, DefaultType>;
116 return static_cast<AsyncOpRef<AsyncResultType>>( std::make_shared<ImplType>( std::forward<Container>(container), std::move(_transFunc), std::move(_defaultVal), std::move(_predicate) ) );
117
118 } else {
119
120 static_assert( std::is_same_v<OutputType, DefaultType>, "Default type and transformation result type must match" );
121
122 for ( auto &in : std::forward<Container>(container) ) {
123 OutputType res = std::invoke( _transFunc, std::move(in) );
124 if ( _predicate(res) ) {
125 return res;
126 }
127 }
128 return _defaultVal;
129 }
130 }
131
132 private:
133 Transformation _transFunc;
134 DefaultType _defaultVal;
135 Predicate _predicate;
136 };
137
138 }
139
143 class NotFoundException : public zypp::Exception {
144 public:
145 NotFoundException() : zypp::Exception("No Entry found"){}
146 };
147
148 template < class Transformation, class DefaultType, class Predicate >
149 inline auto firstOf( Transformation &&transformFunc, DefaultType &&def, Predicate &&predicate = detail::ContinueUntilValidPredicate() ) {
150 return detail::FirstOfHelper<Transformation, Predicate, DefaultType> ( std::forward<Transformation>(transformFunc), std::forward<DefaultType>(def), std::forward<Predicate>(predicate) );
151 }
152
153 namespace detail {
154
155 template <typename Excpt, typename ...Rest>
156 bool containsOneOfExceptionImpl( const std::exception_ptr &exceptionPtr ) {
157 try {
158 if constexpr ( sizeof...(Rest) == 0 ) {
159 // on the lowest level we throw the exception
160 std::rethrow_exception ( exceptionPtr );
161 } else {
162 return containsOneOfExceptionImpl<Rest...>(exceptionPtr);
163 }
164 } catch ( const Excpt &e ) {
165 return true;
166 }
167 }
168
169 }
170
186 template <typename ...Excpt>
187 bool containsOneOfException( const std::exception_ptr &exceptionPtr ) {
188 try {
189 return detail::containsOneOfExceptionImpl<Excpt...>( exceptionPtr );
190 } catch ( ... ) {}
191 return false;
192 }
193
207 template <typename Excpt>
208 bool containsException( const std::exception_ptr &exceptionPtr ) {
209 try {
210 std::rethrow_exception ( exceptionPtr );;
211 } catch ( const Excpt &e ) {
212 return true;
213 } catch ( ... ) {}
214 return false;
215 }
216
217
218}
219
220
221
222
223#endif
zypp::Exception
Base class for Exception.
Definition Exception.h:153
zypp::Exception::Exception
Exception()
Default ctor.
Definition Exception.cc:94
zyppng::NotFoundException::NotFoundException
NotFoundException()
Definition algorithm.h:145
std
Definition Arch.h:364
std::invoke
std::enable_if< std::is_member_pointer< typenamestd::decay< Functor >::type >::value, typenamestd::result_of< Functor &&(Args &&...)>::type >::type invoke(Functor &&f, Args &&... args)
Definition functional.h:32
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
zyppng::detail
Definition reporthelper.h:31
zyppng::detail::containsOneOfExceptionImpl
bool containsOneOfExceptionImpl(const std::exception_ptr &exceptionPtr)
Definition algorithm.h:156
zyppng::detail::is_async_op_v
constexpr bool is_async_op_v
Definition asyncop.h:61
zyppng
Definition MediaNetworkRequestExecutor.h:16
zyppng::AsyncOpRef
std::shared_ptr< AsyncOp< T > > AsyncOpRef
Definition asyncop.h:255
zyppng::remove_smart_ptr_t
typename remove_smart_ptr< T >::type remove_smart_ptr_t
Definition type_traits.h:133
zyppng::containsOneOfException
bool containsOneOfException(const std::exception_ptr &exceptionPtr)
Definition algorithm.h:187
zyppng::firstOf
auto firstOf(Transformation &&transformFunc, DefaultType &&def, Predicate &&predicate=detail::ContinueUntilValidPredicate())
Definition algorithm.h:149
zyppng::containsException
bool containsException(const std::exception_ptr &exceptionPtr)
Definition algorithm.h:208
zyppng::AsyncOp< AsyncResType >::setReady
void setReady(value_type &&val)
Definition asyncop.h:183
zyppng::AsyncOp< AsyncResType >::AsyncOp
AsyncOp()=default
zyppng::detail::AsyncFirstOfImpl
Definition algorithm.h:37
zyppng::detail::AsyncFirstOfImpl::_inData
Container _inData
Definition algorithm.h:83
zyppng::detail::AsyncFirstOfImpl::invokeCurrent
void invokeCurrent()
Definition algorithm.h:71
zyppng::detail::AsyncFirstOfImpl::_currentPipeline
AsyncOpRef< AsyncResType > _currentPipeline
Definition algorithm.h:88
zyppng::detail::AsyncFirstOfImpl::execute
void execute()
Definition algorithm.h:47
zyppng::detail::AsyncFirstOfImpl::_currIter
Container::iterator _currIter
Definition algorithm.h:87
zyppng::detail::AsyncFirstOfImpl::returnDefault
void returnDefault()
Definition algorithm.h:77
zyppng::detail::AsyncFirstOfImpl::AsyncFirstOfImpl
AsyncFirstOfImpl(Container &&inData, Transformation &&transFunc, DefaultType &&defaultVal, Predicate &&predicate)
Definition algorithm.h:39
zyppng::detail::AsyncFirstOfImpl::_transFunc
Transformation _transFunc
Definition algorithm.h:84
zyppng::detail::AsyncFirstOfImpl::resultReady
void resultReady(AsyncResType &&res)
Definition algorithm.h:55
zyppng::detail::AsyncFirstOfImpl::_predicate
Predicate _predicate
Definition algorithm.h:86
zyppng::detail::AsyncFirstOfImpl::_defaultVal
DefaultType _defaultVal
Definition algorithm.h:85
zyppng::detail::ContinueUntilValidPredicate
Definition algorithm.h:24
zyppng::detail::ContinueUntilValidPredicate::operator()
bool operator()(const Arg &value)
Definition algorithm.h:26
zyppng::detail::FirstOfHelper
Definition algorithm.h:92
zyppng::detail::FirstOfHelper::_predicate
Predicate _predicate
Definition algorithm.h:135
zyppng::detail::FirstOfHelper::_defaultVal
DefaultType _defaultVal
Definition algorithm.h:134
zyppng::detail::FirstOfHelper::FirstOfHelper
FirstOfHelper(Transformation transFunc, DefaultType defaultVal, Predicate predicate)
Definition algorithm.h:94
zyppng::detail::FirstOfHelper::_transFunc
Transformation _transFunc
Definition algorithm.h:133
zyppng::detail::FirstOfHelper::operator()
auto operator()(Container &&container)
Definition algorithm.h:101
zyppng::detail::showme
Definition algorithm.h:34