libzypp 17.38.5
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 <functional>
19#include <zypp-core/ng/pipelines/operators.h>
20
21namespace zyppng {
22
23 namespace detail {
24
26 template< class Arg >
27 bool operator()( const Arg &value ) {
28 // works if operator bool() const is implemented by type Arg
29 if ( value ) return true;
30 return false;
31 }
32 };
33
34 template <typename T>
35 struct showme;
36
37 template < class Container, class Transformation, class Predicate, class DefaultType, typename sfinae = void >
38 struct FirstOfImpl;
39
40 template < class Container, class Transformation, class Predicate, class DefaultType, typename sfinae >
42 {
43 using InputType = typename Container::value_type;
44 static_assert( std::is_invocable_v<Transformation, InputType>, "Transformation function must take the container value type as input " );
45
46 using OutputType = std::invoke_result_t<Transformation, InputType>;
47
48 template <typename C = Container>
49 static auto execute ( C &&container, Transformation transFunc, DefaultType defaultVal, Predicate predicate) {
50 static_assert( std::is_same_v<OutputType, DefaultType>, "Default type and transformation result type must match" );
51
52 for ( auto &in : std::forward<C>(container) ) {
53 OutputType res = std::invoke( transFunc, std::move(in) );
54 if ( predicate(res) ) {
55 return res;
56 }
57 }
58 return defaultVal;
59 }
60 };
61
62 template < class Transformation, class Predicate, class DefaultType >
64
65 FirstOfHelper( Transformation transFunc, DefaultType defaultVal, Predicate predicate )
66 : _transFunc ( std::move(transFunc) )
67 , _defaultVal( std::move(defaultVal) )
68 , _predicate ( std::move(predicate)) { }
69
70 template < class Container
71 , typename ...CArgs>
72 auto operator()( Container &&container ) {
73 static_assert( std::is_rvalue_reference_v<decltype(std::forward<Container>(container))>, "Input container must be a rvalue reference" );
74 return FirstOfImpl<Container, Transformation, Predicate, DefaultType>::execute( std::forward<Container>(container), std::move(_transFunc), std::move(_defaultVal), std::move(_predicate) );
75 }
76
77 private:
78 Transformation _transFunc;
79 DefaultType _defaultVal;
80 Predicate _predicate;
81 };
82
83 }
84
89 public:
90 NotFoundException() : zypp::Exception("No Entry found"){}
91 };
92
93 template < class Transformation, class DefaultType, class Predicate >
94 inline auto firstOf( Transformation &&transformFunc, DefaultType &&def, Predicate &&predicate = detail::ContinueUntilValidPredicate() ) {
95 return detail::FirstOfHelper<Transformation, Predicate, DefaultType> ( std::forward<Transformation>(transformFunc), std::forward<DefaultType>(def), std::forward<Predicate>(predicate) );
96 }
97
98 namespace detail {
99
100 template <typename Excpt, typename ...Rest>
101 bool containsOneOfExceptionImpl( const std::exception_ptr &exceptionPtr ) {
102 try {
103 if constexpr ( sizeof...(Rest) == 0 ) {
104 // on the lowest level we throw the exception
105 std::rethrow_exception ( exceptionPtr );
106 } else {
107 return containsOneOfExceptionImpl<Rest...>(exceptionPtr);
108 }
109 } catch ( const Excpt &e ) {
110 return true;
111 }
112 }
113
114 }
115
131 template <typename ...Excpt>
132 bool containsOneOfException( const std::exception_ptr &exceptionPtr ) {
133 try {
134 return detail::containsOneOfExceptionImpl<Excpt...>( exceptionPtr );
135 } catch ( ... ) {}
136 return false;
137 }
138
152 template <typename Excpt>
153 bool containsException( const std::exception_ptr &exceptionPtr ) {
154 try {
155 std::rethrow_exception ( exceptionPtr );;
156 } catch ( const Excpt &e ) {
157 return true;
158 } catch ( ... ) {}
159 return false;
160 }
161
162
163}
164
165#ifdef ZYPP_ENABLE_ASYNC
166#include <zypp-core/ng/async/pipelines/algorithm.hpp>
167#endif
168
169#endif
Base class for Exception.
Definition Exception.h:153
Exception()
Default ctor.
Definition Exception.cc:94
Definition ansi.h:855
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:101
bool containsOneOfException(const std::exception_ptr &exceptionPtr)
Definition algorithm.h:132
auto firstOf(Transformation &&transformFunc, DefaultType &&def, Predicate &&predicate=detail::ContinueUntilValidPredicate())
Definition algorithm.h:94
bool containsException(const std::exception_ptr &exceptionPtr)
Definition algorithm.h:153
FirstOfHelper(Transformation transFunc, DefaultType defaultVal, Predicate predicate)
Definition algorithm.h:65
auto operator()(Container &&container)
Definition algorithm.h:72
typename Container::value_type InputType
Definition algorithm.h:43
std::invoke_result_t< Transformation, InputType > OutputType
Definition algorithm.h:46
static auto execute(C &&container, Transformation transFunc, DefaultType defaultVal, Predicate predicate)
Definition algorithm.h:49