libzypp  17.14.0
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 <boost/functional.hpp>
16 
17 #include "zypp/base/Function.h"
18 
20 namespace zypp
21 {
22 
23  /* http://www.boost.org/libs/functional/mem_fun.html
24 
25  The header functional.hpp includes improved versions of
26  the full range of member function adapters from the
27  C++ Standard Library.
28  */
29  using boost::mem_fun;
30  using boost::mem_fun_ref;
31 
33  namespace functor
34  {
35 
77  namespace functor_detail
79  {
80  template <class TFunctor, class res_type>
81  struct FunctorRef0
82  {
83  FunctorRef0( TFunctor & f_r )
84  : _f( f_r )
85  {}
86 
87  res_type operator()() const
88  {
89  return _f();
90  }
91 
92  private:
93  TFunctor & _f;
94  };
95 
96  template <class TFunctor, class res_type, class arg1_type>
97  struct FunctorRef1 : public std::unary_function<arg1_type, res_type>
98  {
99  FunctorRef1( TFunctor & f_r )
100  : _f( f_r )
101  {}
102 
103  res_type operator()( arg1_type a1 ) const
104  {
105  return _f( a1 );
106  }
107 
108  private:
109  TFunctor & _f;
110  };
111 
112  template <class TFunctor, class res_type, class arg1_type, class arg2_type>
113  struct FunctorRef2 : public std::binary_function<arg1_type, arg2_type, res_type>
114  {
115  FunctorRef2( TFunctor & f_r )
116  : _f( f_r )
117  {}
118 
119  res_type operator()( arg1_type a1, arg2_type a2 ) const
120  {
121  return _f( a1, a2 );
122  }
123 
124  private:
125  TFunctor & _f;
126  };
127 
128  struct nil
129  {};
130  }
132 
136  template <class TFunctor, class res_type, class arg1_type = functor_detail::nil,
137  class arg2_type = functor_detail::nil>
138  struct FunctorRef
139  : public functor_detail::FunctorRef2<TFunctor, res_type, arg1_type, arg2_type>
140  {
141  FunctorRef( TFunctor & f_r )
142  : functor_detail::FunctorRef2<TFunctor, res_type, arg1_type, arg2_type>( f_r )
143  {}
144  };
145 
149  template <class TFunctor, class res_type, class arg1_type>
150  struct FunctorRef<TFunctor, res_type, arg1_type>
151  : public functor_detail::FunctorRef1<TFunctor, res_type, arg1_type>
152  {
153  FunctorRef( TFunctor & f_r )
154  : functor_detail::FunctorRef1<TFunctor, res_type, arg1_type>( f_r )
155  {}
156  };
157 
161  template <class TFunctor, class res_type>
162  struct FunctorRef<TFunctor, res_type>
163  : public functor_detail::FunctorRef0<TFunctor, res_type>
164  {
165  FunctorRef( TFunctor & f_r )
166  : functor_detail::FunctorRef0<TFunctor, res_type>( f_r )
167  {}
168  };
169 
171  template <class res_type, class arg1_type, class arg2_type, class TFunctor>
172  FunctorRef<TFunctor, res_type, arg1_type, arg2_type>
173  functorRef( TFunctor & f_r )
175  template <class res_type, class arg1_type, class TFunctor>
176  FunctorRef<TFunctor, res_type, arg1_type>
177  functorRef( TFunctor & f_r )
179  template <class res_type, class TFunctor>
180  FunctorRef<TFunctor, res_type>
181  functorRef( TFunctor & f_r )
182  { return FunctorRef<TFunctor, res_type>( f_r ); }
183 
185 
216 
217  /* functor that always returns a copied value */
218  template<class TConst>
219  struct Constant
220  {
221  Constant( const TConst &value )
222  : _value(value)
223  {}
224 
225  template<class Tp>
226  TConst operator()( Tp ) const
227  { return _value; }
228 
229  TConst operator()() const
230  { return _value; }
231 
232  TConst _value;
233  };
234 
235  template<class TConst>
236  inline Constant<TConst> constant( const TConst &value )
237  { return Constant<TConst>(value); }
238 
240  struct True
241  {
242  template<class Tp>
243  bool operator()( Tp ) const
244  {
245  return true;
246  }
247  };
248 
250  inline True true_c()
251  { return True(); }
252 
255  struct False
256  {
257  template<class Tp>
258  bool operator()( Tp ) const
259  {
260  return false;
261  }
262  };
263 
265  inline False false_c()
266  { return False(); }
267 
270  template<class TCondition>
271  struct Not
272  {
273  Not( TCondition cond_r )
274  : _cond( cond_r )
275  {}
276 
277  template<class Tp>
278  bool operator()( Tp t ) const
279  {
280  return ! _cond( t );
281  }
282 
283  TCondition _cond;
284  };
285 
287  template<class TCondition>
288  inline Not<TCondition> not_c( TCondition cond_r )
289  {
290  return Not<TCondition>( cond_r );
291  }
292 
295  template<class TACondition, class TBCondition>
296  struct Or
297  {
298  Or( TACondition conda_r, TBCondition condb_r )
299  : _conda( conda_r )
300  , _condb( condb_r )
301  {}
302 
303  template<class Tp>
304  bool operator()( Tp t ) const
305  {
306  return _conda( t ) || _condb( t );
307  }
308 
309  TACondition _conda;
310  TBCondition _condb;
311  };
312 
316  template<class TACondition, class TBCondition>
317  inline Or<TACondition, TBCondition> or_c( TACondition conda_r, TBCondition condb_r )
318  {
319  return Or<TACondition, TBCondition>( conda_r, condb_r );
320  }
321 
324  template<class TACondition, class TBCondition>
325  struct Chain
326  {
327  Chain( TACondition conda_r, TBCondition condb_r )
328  : _conda( conda_r )
329  , _condb( condb_r )
330  {}
331 
332  template<class Tp>
333  bool operator()( Tp t ) const
334  {
335  return _conda( t ) && _condb( t );
336  }
337 
338  TACondition _conda;
339  TBCondition _condb;
340  };
341 
345  template<class TACondition, class TBCondition>
346  inline Chain<TACondition, TBCondition> chain( TACondition conda_r, TBCondition condb_r )
347  {
348  return Chain<TACondition, TBCondition>( conda_r, condb_r );
349  }
350 
352 
358 
367  template<class Tp>
368  struct GetFirst
369  {
370  GetFirst( Tp & result_r )
371  : _result( &result_r )
372  {}
373  bool operator()( const Tp & val_r )
374  { *_result = val_r; return false; }
375 
376  private:
377  Tp * _result;
378  };
379 
381  template<class Tp>
382  GetFirst<Tp> getFirst( Tp & result_r )
383  { return GetFirst<Tp>( result_r ); }
384 
385 
388  template<class Tp>
389  struct GetLast
390  {
391  GetLast( Tp & result_r )
392  : _result( &result_r )
393  {}
394  bool operator()( const Tp & val_r )
395  { *_result = val_r; return true; }
396 
397  private:
398  Tp * _result;
399  };
400 
402  template<class Tp>
403  GetLast<Tp> getLast( Tp & result_r )
404  { return GetLast<Tp>( result_r ); }
405 
406 
414  template<class TOutputIterator>
415  struct GetAll
416  {
417  GetAll( TOutputIterator result_r )
418  : _result( result_r )
419  {}
420 
421  template<class Tp>
422  bool operator()( const Tp & val_r ) const
423  { *(_result++) = val_r; return true; }
424 
425  private:
426  mutable TOutputIterator _result;
427  };
428 
430  template<class TOutputIterator>
431  GetAll<TOutputIterator> getAll( TOutputIterator result_r )
432  { return GetAll<TOutputIterator>( result_r ); }
433 
435 
438  } // namespace functor
441 } // namespace zypp
443 #endif // ZYPP_BASE_FUNCTIONAL_H
bool operator()(const Tp &val_r) const
Definition: Functional.h:422
bool operator()(const Tp &val_r)
Definition: Functional.h:373
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:317
Logical functor chaining TACondition OR TBCondition.
Definition: Functional.h:296
GetLast< Tp > getLast(Tp &result_r)
Convenience function for creating GetLast.
Definition: Functional.h:403
Store all results found to some output_iterator.
Definition: Functional.h:415
FunctorRef(TFunctor &f_r)
Definition: Functional.h:141
GetFirst< Tp > getFirst(Tp &result_r)
Convenience function for creating GetFirst.
Definition: Functional.h:382
TConst operator()(Tp) const
Definition: Functional.h:226
TCondition _cond
Definition: Functional.h:283
TACondition _conda
Definition: Functional.h:338
TBCondition _condb
Definition: Functional.h:339
False false_c()
Convenience function for creating a False.
Definition: Functional.h:265
Or(TACondition conda_r, TBCondition condb_r)
Definition: Functional.h:298
bool operator()(Tp t) const
Definition: Functional.h:304
A binary FunctorRef.
Definition: Functional.h:138
bool operator()(Tp) const
Definition: Functional.h:243
Constant< TConst > constant(const TConst &value)
Definition: Functional.h:236
res_type operator()(arg1_type a1) const
Definition: Functional.h:103
Logical functor always false.
Definition: Functional.h:255
bool operator()(Tp) const
Definition: Functional.h:258
GetAll(TOutputIterator result_r)
Definition: Functional.h:417
True true_c()
Convenience function for creating a True.
Definition: Functional.h:250
Strore the 1st result found in the variable passed to the ctor.
Definition: Functional.h:368
res_type operator()(arg1_type a1, arg2_type a2) const
Definition: Functional.h:119
TACondition _conda
Definition: Functional.h:309
Constant(const TConst &value)
Definition: Functional.h:221
Logical functor chaining TACondition AND TBCondition.
Definition: Functional.h:325
bool operator()(Tp t) const
Definition: Functional.h:333
TOutputIterator _result
Definition: Functional.h:426
Strore the last result found in the variable passed to the ctor.
Definition: Functional.h:389
Not< TCondition > not_c(TCondition cond_r)
Convenience function for creating a Not from TCondition.
Definition: Functional.h:288
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:346
Not(TCondition cond_r)
Definition: Functional.h:273
GetFirst(Tp &result_r)
Definition: Functional.h:370
FunctorRef< TFunctor, res_type, arg1_type, arg2_type > functorRef(TFunctor &f_r)
Convenience function creating a binary FunctorRef.
Definition: Functional.h:173
Chain(TACondition conda_r, TBCondition condb_r)
Definition: Functional.h:327
bool operator()(Tp t) const
Definition: Functional.h:278
TBCondition _condb
Definition: Functional.h:310
GetLast(Tp &result_r)
Definition: Functional.h:391
bool operator()(const Tp &val_r)
Definition: Functional.h:394
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
Logical functor inverting TCondition.
Definition: Functional.h:271
Logical functor always true.
Definition: Functional.h:240
GetAll< TOutputIterator > getAll(TOutputIterator result_r)
Convenience function for creating GetAll.
Definition: Functional.h:431
TConst operator()() const
Definition: Functional.h:229