libzypp 17.31.23
Functional.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#ifndef ZYPP_BASE_FUNCTIONAL_H
13#define ZYPP_BASE_FUNCTIONAL_H
14
15#include <functional>
16#include <zypp/base/Function.h>
17
19namespace zypp
20{
21
23 namespace functor
24 {
55
56 /* functor that always returns a copied value */
57 template<class TConst>
58 struct Constant
59 {
60 Constant( const TConst &value )
61 : _value(value)
62 {}
63
64 template<class Tp>
65 TConst operator()( Tp ) const
66 { return _value; }
67
68 TConst operator()() const
69 { return _value; }
70
71 TConst _value;
72 };
73
74 template<class TConst>
75 inline Constant<TConst> constant( const TConst &value )
76 { return Constant<TConst>(value); }
77
79 struct True
80 {
81 template<class Tp>
82 bool operator()( Tp ) const
83 {
84 return true;
85 }
86 };
87
89 inline True true_c()
90 { return True(); }
91
94 struct False
95 {
96 template<class Tp>
97 bool operator()( Tp ) const
98 {
99 return false;
100 }
101 };
102
104 inline False false_c()
105 { return False(); }
106
109 template<class TCondition>
110 struct Not
111 {
112 Not( TCondition cond_r )
113 : _cond( cond_r )
114 {}
115
116 template<class Tp>
117 bool operator()( Tp t ) const
118 {
119 return ! _cond( t );
120 }
121
122 TCondition _cond;
123 };
124
126 template<class TCondition>
127 inline Not<TCondition> not_c( TCondition cond_r )
128 {
129 return Not<TCondition>( cond_r );
130 }
131
134 template<class TACondition, class TBCondition>
135 struct Or
136 {
137 Or( TACondition conda_r, TBCondition condb_r )
138 : _conda( conda_r )
139 , _condb( condb_r )
140 {}
141
142 template<class Tp>
143 bool operator()( Tp t ) const
144 {
145 return _conda( t ) || _condb( t );
146 }
147
148 TACondition _conda;
149 TBCondition _condb;
150 };
151
155 template<class TACondition, class TBCondition>
156 inline Or<TACondition, TBCondition> or_c( TACondition conda_r, TBCondition condb_r )
157 {
158 return Or<TACondition, TBCondition>( conda_r, condb_r );
159 }
160
163 template<class TACondition, class TBCondition>
164 struct Chain
165 {
166 Chain( TACondition conda_r, TBCondition condb_r )
167 : _conda( conda_r )
168 , _condb( condb_r )
169 {}
170
171 template<class Tp>
172 bool operator()( Tp t ) const
173 {
174 return _conda( t ) && _condb( t );
175 }
176
177 TACondition _conda;
178 TBCondition _condb;
179 };
180
184 template<class TACondition, class TBCondition>
185 inline Chain<TACondition, TBCondition> chain( TACondition conda_r, TBCondition condb_r )
186 {
187 return Chain<TACondition, TBCondition>( conda_r, condb_r );
188 }
189
191
192
197
206 template<class Tp>
207 struct GetFirst
208 {
209 GetFirst( Tp & result_r )
210 : _result( &result_r )
211 {}
212 bool operator()( const Tp & val_r )
213 { *_result = val_r; return false; }
214
215 private:
217 };
218
220 template<class Tp>
221 GetFirst<Tp> getFirst( Tp & result_r )
222 { return GetFirst<Tp>( result_r ); }
223
224
227 template<class Tp>
228 struct GetLast
229 {
230 GetLast( Tp & result_r )
231 : _result( &result_r )
232 {}
233 bool operator()( const Tp & val_r )
234 { *_result = val_r; return true; }
235
236 private:
238 };
239
241 template<class Tp>
242 GetLast<Tp> getLast( Tp & result_r )
243 { return GetLast<Tp>( result_r ); }
244
245
253 template<class TOutputIterator>
254 struct GetAll
255 {
256 GetAll( TOutputIterator result_r )
257 : _result( result_r )
258 {}
259
260 template<class Tp>
261 bool operator()( const Tp & val_r ) const
262 { *(_result++) = val_r; return true; }
263
264 private:
265 mutable TOutputIterator _result;
266 };
267
269 template<class TOutputIterator>
270 GetAll<TOutputIterator> getAll( TOutputIterator result_r )
271 { return GetAll<TOutputIterator>( result_r ); }
272
274
275
277 } // namespace functor
280} // namespace zypp
282#endif // ZYPP_BASE_FUNCTIONAL_H
Constant< TConst > constant(const TConst &value)
Definition: Functional.h:75
False false_c()
Convenience function for creating a False.
Definition: Functional.h:104
Not< TCondition > not_c(TCondition cond_r)
Convenience function for creating a Not from TCondition.
Definition: Functional.h:127
Chain< TACondition, TBCondition > chain(TACondition conda_r, TBCondition condb_r)
Convenience function for creating a Chain from two conditions conda_r and condb_r.
Definition: Functional.h:185
GetFirst< Tp > getFirst(Tp &result_r)
Convenience function for creating GetFirst.
Definition: Functional.h:221
GetLast< Tp > getLast(Tp &result_r)
Convenience function for creating GetLast.
Definition: Functional.h:242
True true_c()
Convenience function for creating a True.
Definition: Functional.h:89
Or< TACondition, TBCondition > or_c(TACondition conda_r, TBCondition condb_r)
Convenience function for creating a Or from two conditions conda_r OR condb_r.
Definition: Functional.h:156
GetAll< TOutputIterator > getAll(TOutputIterator result_r)
Convenience function for creating GetAll.
Definition: Functional.h:270
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
Logical functor chaining TACondition AND TBCondition.
Definition: Functional.h:165
bool operator()(Tp t) const
Definition: Functional.h:172
TBCondition _condb
Definition: Functional.h:178
Chain(TACondition conda_r, TBCondition condb_r)
Definition: Functional.h:166
TACondition _conda
Definition: Functional.h:177
TConst operator()(Tp) const
Definition: Functional.h:65
TConst operator()() const
Definition: Functional.h:68
Constant(const TConst &value)
Definition: Functional.h:60
Logical functor always false.
Definition: Functional.h:95
bool operator()(Tp) const
Definition: Functional.h:97
Store all results found to some output_iterator.
Definition: Functional.h:255
GetAll(TOutputIterator result_r)
Definition: Functional.h:256
bool operator()(const Tp &val_r) const
Definition: Functional.h:261
TOutputIterator _result
Definition: Functional.h:265
Strore the 1st result found in the variable passed to the ctor.
Definition: Functional.h:208
bool operator()(const Tp &val_r)
Definition: Functional.h:212
GetFirst(Tp &result_r)
Definition: Functional.h:209
Strore the last result found in the variable passed to the ctor.
Definition: Functional.h:229
GetLast(Tp &result_r)
Definition: Functional.h:230
bool operator()(const Tp &val_r)
Definition: Functional.h:233
Logical functor inverting TCondition.
Definition: Functional.h:111
TCondition _cond
Definition: Functional.h:122
Not(TCondition cond_r)
Definition: Functional.h:112
bool operator()(Tp t) const
Definition: Functional.h:117
Logical functor chaining TACondition OR TBCondition.
Definition: Functional.h:136
TACondition _conda
Definition: Functional.h:148
Or(TACondition conda_r, TBCondition condb_r)
Definition: Functional.h:137
bool operator()(Tp t) const
Definition: Functional.h:143
TBCondition _condb
Definition: Functional.h:149
Logical functor always true.
Definition: Functional.h:80
bool operator()(Tp) const
Definition: Functional.h:82