libzypp: zypp-core/base/Functional.h Source File

libzypp 17.38.7

Functional.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#ifndef ZYPP_CORE_BASE_FUNCTIONAL_H
13#define ZYPP_CORE_BASE_FUNCTIONAL_H
14
15//#include <functional>
16//#include <zypp-core/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:
216 Tp * _result;
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:
237 Tp * _result;
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_CORE_BASE_FUNCTIONAL_H
zypp::functor
Definition Collector.h:20
zypp::functor::constant
Constant< TConst > constant(const TConst &value)
Definition Functional.h:75
zypp::functor::false_c
False false_c()
Convenience function for creating a False.
Definition Functional.h:104
zypp::functor::not_c
Not< TCondition > not_c(TCondition cond_r)
Convenience function for creating a Not from TCondition.
Definition Functional.h:127
zypp::functor::chain
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
zypp::functor::getFirst
GetFirst< Tp > getFirst(Tp &result_r)
Convenience function for creating GetFirst.
Definition Functional.h:221
zypp::functor::getLast
GetLast< Tp > getLast(Tp &result_r)
Convenience function for creating GetLast.
Definition Functional.h:242
zypp::functor::true_c
True true_c()
Convenience function for creating a True.
Definition Functional.h:89
zypp::functor::or_c
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
zypp::functor::getAll
GetAll< TOutputIterator > getAll(TOutputIterator result_r)
Convenience function for creating GetAll.
Definition Functional.h:270
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::functor::Chain
Logical functor chaining TACondition AND TBCondition.
Definition Functional.h:165
zypp::functor::Chain::operator()
bool operator()(Tp t) const
Definition Functional.h:172
zypp::functor::Chain::_condb
TBCondition _condb
Definition Functional.h:178
zypp::functor::Chain::Chain
Chain(TACondition conda_r, TBCondition condb_r)
Definition Functional.h:166
zypp::functor::Chain::_conda
TACondition _conda
Definition Functional.h:177
zypp::functor::Constant
Definition Functional.h:59
zypp::functor::Constant::_value
TConst _value
Definition Functional.h:71
zypp::functor::Constant::operator()
TConst operator()(Tp) const
Definition Functional.h:65
zypp::functor::Constant::operator()
TConst operator()() const
Definition Functional.h:68
zypp::functor::Constant::Constant
Constant(const TConst &value)
Definition Functional.h:60
zypp::functor::False
Logical functor always false.
Definition Functional.h:95
zypp::functor::False::operator()
bool operator()(Tp) const
Definition Functional.h:97
zypp::functor::GetAll
Store all results found to some output_iterator.
Definition Functional.h:255
zypp::functor::GetAll::GetAll
GetAll(TOutputIterator result_r)
Definition Functional.h:256
zypp::functor::GetAll::operator()
bool operator()(const Tp &val_r) const
Definition Functional.h:261
zypp::functor::GetAll::_result
TOutputIterator _result
Definition Functional.h:265
zypp::functor::GetFirst
Strore the 1st result found in the variable passed to the ctor.
Definition Functional.h:208
zypp::functor::GetFirst::_result
Tp * _result
Definition Functional.h:216
zypp::functor::GetFirst::operator()
bool operator()(const Tp &val_r)
Definition Functional.h:212
zypp::functor::GetFirst::GetFirst
GetFirst(Tp &result_r)
Definition Functional.h:209
zypp::functor::GetLast
Strore the last result found in the variable passed to the ctor.
Definition Functional.h:229
zypp::functor::GetLast::GetLast
GetLast(Tp &result_r)
Definition Functional.h:230
zypp::functor::GetLast::operator()
bool operator()(const Tp &val_r)
Definition Functional.h:233
zypp::functor::GetLast::_result
Tp * _result
Definition Functional.h:237
zypp::functor::Not
Logical functor inverting TCondition.
Definition Functional.h:111
zypp::functor::Not::_cond
TCondition _cond
Definition Functional.h:122
zypp::functor::Not::Not
Not(TCondition cond_r)
Definition Functional.h:112
zypp::functor::Not::operator()
bool operator()(Tp t) const
Definition Functional.h:117
zypp::functor::Or
Logical functor chaining TACondition OR TBCondition.
Definition Functional.h:136
zypp::functor::Or::_conda
TACondition _conda
Definition Functional.h:148
zypp::functor::Or::Or
Or(TACondition conda_r, TBCondition condb_r)
Definition Functional.h:137
zypp::functor::Or::operator()
bool operator()(Tp t) const
Definition Functional.h:143
zypp::functor::Or::_condb
TBCondition _condb
Definition Functional.h:149
zypp::functor::True
Logical functor always true.
Definition Functional.h:80
zypp::functor::True::operator()
bool operator()(Tp) const
Definition Functional.h:82