00001
00002
00003
00004
00005
00006
00007
00008
00012 #ifndef ZYPP_BASE_FUNCTIONAL_H
00013 #define ZYPP_BASE_FUNCTIONAL_H
00014
00015 #include <boost/functional.hpp>
00016
00017 #include "zypp/base/Function.h"
00018
00020 namespace zypp
00021 {
00022
00023
00024
00025
00026
00027
00028
00029 using boost::mem_fun;
00030 using boost::mem_fun_ref;
00031
00033 namespace functor
00034 {
00035
00077
00078 namespace functor_detail
00079 {
00080 template <class _Functor, class res_type>
00081 struct FunctorRef0
00082 {
00083 FunctorRef0( _Functor & f_r )
00084 : _f( f_r )
00085 {}
00086
00087 res_type operator()() const
00088 {
00089 return _f();
00090 }
00091
00092 private:
00093 _Functor & _f;
00094 };
00095
00096 template <class _Functor, class res_type, class arg1_type>
00097 struct FunctorRef1 : public std::unary_function<arg1_type, res_type>
00098 {
00099 FunctorRef1( _Functor & f_r )
00100 : _f( f_r )
00101 {}
00102
00103 res_type operator()( arg1_type a1 ) const
00104 {
00105 return _f( a1 );
00106 }
00107
00108 private:
00109 _Functor & _f;
00110 };
00111
00112 template <class _Functor, class res_type, class arg1_type, class arg2_type>
00113 struct FunctorRef2 : public std::binary_function<arg1_type, arg2_type, res_type>
00114 {
00115 FunctorRef2( _Functor & f_r )
00116 : _f( f_r )
00117 {}
00118
00119 res_type operator()( arg1_type a1, arg2_type a2 ) const
00120 {
00121 return _f( a1, a2 );
00122 }
00123
00124 private:
00125 _Functor & _f;
00126 };
00127
00128 struct nil
00129 {};
00130 }
00132
00136 template <class _Functor, class res_type, class arg1_type = functor_detail::nil,
00137 class arg2_type = functor_detail::nil>
00138 struct FunctorRef
00139 : public functor_detail::FunctorRef2<_Functor, res_type, arg1_type, arg2_type>
00140 {
00141 FunctorRef( _Functor & f_r )
00142 : functor_detail::FunctorRef2<_Functor, res_type, arg1_type, arg2_type>( f_r )
00143 {}
00144 };
00145
00149 template <class _Functor, class res_type, class arg1_type>
00150 struct FunctorRef<_Functor, res_type, arg1_type>
00151 : public functor_detail::FunctorRef1<_Functor, res_type, arg1_type>
00152 {
00153 FunctorRef( _Functor & f_r )
00154 : functor_detail::FunctorRef1<_Functor, res_type, arg1_type>( f_r )
00155 {}
00156 };
00157
00161 template <class _Functor, class res_type>
00162 struct FunctorRef<_Functor, res_type>
00163 : public functor_detail::FunctorRef0<_Functor, res_type>
00164 {
00165 FunctorRef( _Functor & f_r )
00166 : functor_detail::FunctorRef0<_Functor, res_type>( f_r )
00167 {}
00168 };
00169
00171 template <class res_type, class arg1_type, class arg2_type, class _Functor>
00172 FunctorRef<_Functor, res_type, arg1_type, arg2_type>
00173 functorRef( _Functor & f_r )
00174 { return FunctorRef<_Functor, res_type, arg1_type, arg2_type>( f_r ); }
00175 template <class res_type, class arg1_type, class _Functor>
00176 FunctorRef<_Functor, res_type, arg1_type>
00177 functorRef( _Functor & f_r )
00178 { return FunctorRef<_Functor, res_type, arg1_type>( f_r ); }
00179 template <class res_type, class _Functor>
00180 FunctorRef<_Functor, res_type>
00181 functorRef( _Functor & f_r )
00182 { return FunctorRef<_Functor, res_type>( f_r ); }
00183
00185
00216
00217
00218
00219 template<class T>
00220 struct Constant
00221 {
00222 Constant( const T &value )
00223 : _value(value)
00224 {}
00225
00226 template<class _Tp>
00227 T operator()( _Tp ) const
00228 { return _value; }
00229
00230 T operator()() const
00231 { return _value; }
00232
00233 T _value;
00234 };
00235
00236 template<class T>
00237 inline Constant<T> constant( const T &value )
00238 { return Constant<T>(value); }
00239
00242 struct True
00243 {
00244 template<class _Tp>
00245 bool operator()( _Tp ) const
00246 {
00247 return true;
00248 }
00249 };
00250
00252 inline True true_c()
00253 { return True(); }
00254
00257 struct False
00258 {
00259 template<class _Tp>
00260 bool operator()( _Tp ) const
00261 {
00262 return false;
00263 }
00264 };
00265
00267 inline False false_c()
00268 { return False(); }
00269
00272 template<class _Condition>
00273 struct Not
00274 {
00275 Not( _Condition cond_r )
00276 : _cond( cond_r )
00277 {}
00278
00279 template<class _Tp>
00280 bool operator()( _Tp t ) const
00281 {
00282 return ! _cond( t );
00283 }
00284
00285 _Condition _cond;
00286 };
00287
00289 template<class _Condition>
00290 inline Not<_Condition> not_c( _Condition cond_r )
00291 {
00292 return Not<_Condition>( cond_r );
00293 }
00294
00297 template<class _ACondition, class _BCondition>
00298 struct Or
00299 {
00300 Or( _ACondition conda_r, _BCondition condb_r )
00301 : _conda( conda_r )
00302 , _condb( condb_r )
00303 {}
00304
00305 template<class _Tp>
00306 bool operator()( _Tp t ) const
00307 {
00308 return _conda( t ) || _condb( t );
00309 }
00310
00311 _ACondition _conda;
00312 _BCondition _condb;
00313 };
00314
00318 template<class _ACondition, class _BCondition>
00319 inline Or<_ACondition, _BCondition> or_c( _ACondition conda_r, _BCondition condb_r )
00320 {
00321 return Or<_ACondition, _BCondition>( conda_r, condb_r );
00322 }
00323
00326 template<class _ACondition, class _BCondition>
00327 struct Chain
00328 {
00329 Chain( _ACondition conda_r, _BCondition condb_r )
00330 : _conda( conda_r )
00331 , _condb( condb_r )
00332 {}
00333
00334 template<class _Tp>
00335 bool operator()( _Tp t ) const
00336 {
00337 return _conda( t ) && _condb( t );
00338 }
00339
00340 _ACondition _conda;
00341 _BCondition _condb;
00342 };
00343
00347 template<class _ACondition, class _BCondition>
00348 inline Chain<_ACondition, _BCondition> chain( _ACondition conda_r, _BCondition condb_r )
00349 {
00350 return Chain<_ACondition, _BCondition>( conda_r, condb_r );
00351 }
00352
00354
00355
00360
00369 template<class _Tp>
00370 struct GetFirst
00371 {
00372 GetFirst( _Tp & result_r )
00373 : _result( &result_r )
00374 {}
00375 bool operator()( const _Tp & val_r )
00376 { *_result = val_r; return false; }
00377
00378 private:
00379 _Tp * _result;
00380 };
00381
00383 template<class _Tp>
00384 GetFirst<_Tp> getFirst( _Tp & result_r )
00385 { return GetFirst<_Tp>( result_r ); }
00386
00387
00390 template<class _Tp>
00391 struct GetLast
00392 {
00393 GetLast( _Tp & result_r )
00394 : _result( &result_r )
00395 {}
00396 bool operator()( const _Tp & val_r )
00397 { *_result = val_r; return true; }
00398
00399 private:
00400 _Tp * _result;
00401 };
00402
00404 template<class _Tp>
00405 GetLast<_Tp> getLast( _Tp & result_r )
00406 { return GetLast<_Tp>( result_r ); }
00407
00408
00416 template<class _OutputIterator>
00417 struct GetAll
00418 {
00419 GetAll( _OutputIterator result_r )
00420 : _result( result_r )
00421 {}
00422
00423 template<class _Tp>
00424 bool operator()( const _Tp & val_r ) const
00425 { *(_result++) = val_r; return true; }
00426
00427 private:
00428 mutable _OutputIterator _result;
00429 };
00430
00432 template<class _OutputIterator>
00433 GetAll<_OutputIterator> getAll( _OutputIterator result_r )
00434 { return GetAll<_OutputIterator>( result_r ); }
00435
00437
00438
00440 }
00443 }
00445 #endif // ZYPP_BASE_FUNCTIONAL_H