libzypp 17.37.17
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
20namespace zyppng {
21
22 namespace detail {
23
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 }
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 }
64 } else {
65 this->setReady ( std::move(res) );
66 return;
67 }
68
69 }
70
73 _currentPipeline->onReady( [ this ]( AsyncResType &&res ) {
74 this->resultReady( std::move(res));
75 });
76 }
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;
89 };
90
91 template < class Transformation, class Predicate, class DefaultType >
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
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
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
Base class for Exception.
Definition Exception.h:153
Exception()
Default ctor.
Definition Exception.cc:94
Definition Arch.h:364
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
Easy-to use interface to the ZYPP dependency resolver.
bool containsOneOfExceptionImpl(const std::exception_ptr &exceptionPtr)
Definition algorithm.h:156
constexpr bool is_async_op_v
Definition asyncop.h:61
std::shared_ptr< AsyncOp< T > > AsyncOpRef
Definition asyncop.h:255
typename remove_smart_ptr< T >::type remove_smart_ptr_t
bool containsOneOfException(const std::exception_ptr &exceptionPtr)
Definition algorithm.h:187
auto firstOf(Transformation &&transformFunc, DefaultType &&def, Predicate &&predicate=detail::ContinueUntilValidPredicate())
Definition algorithm.h:149
bool containsException(const std::exception_ptr &exceptionPtr)
Definition algorithm.h:208
void setReady(value_type &&val)
Definition asyncop.h:183
AsyncOpRef< AsyncResType > _currentPipeline
Definition algorithm.h:88
Container::iterator _currIter
Definition algorithm.h:87
AsyncFirstOfImpl(Container &&inData, Transformation &&transFunc, DefaultType &&defaultVal, Predicate &&predicate)
Definition algorithm.h:39
void resultReady(AsyncResType &&res)
Definition algorithm.h:55
FirstOfHelper(Transformation transFunc, DefaultType defaultVal, Predicate predicate)
Definition algorithm.h:94
auto operator()(Container &&container)
Definition algorithm.h:101