00001
00002
00003
00004
00005
00006
00007
00008
00012 #ifndef ZYPP_BASE_FUNCTIONAL_H
00013 #define ZYPP_BASE_FUNCTIONAL_H
00014
00015 #include <functional>
00016
00018 namespace zypp
00019 {
00020
00021 namespace functor
00022 {
00023
00063
00064 namespace functor_detail
00065 {
00066 template <class _Functor, class res_type>
00067 struct FunctorRef0
00068 {
00069 FunctorRef0( _Functor & f_r )
00070 : _f( f_r )
00071 {}
00072
00073 res_type operator()() const
00074 {
00075 return _f();
00076 }
00077
00078 private:
00079 _Functor & _f;
00080 };
00081
00082 template <class _Functor, class res_type, class arg1_type>
00083 struct FunctorRef1 : public std::unary_function<arg1_type, res_type>
00084 {
00085 FunctorRef1( _Functor & f_r )
00086 : _f( f_r )
00087 {}
00088
00089 res_type operator()( arg1_type a1 ) const
00090 {
00091 return _f( a1 );
00092 }
00093
00094 private:
00095 _Functor & _f;
00096 };
00097
00098 template <class _Functor, class res_type, class arg1_type, class arg2_type>
00099 struct FunctorRef2 : public std::binary_function<arg1_type, arg2_type, res_type>
00100 {
00101 FunctorRef2( _Functor & f_r )
00102 : _f( f_r )
00103 {}
00104
00105 res_type operator()( arg1_type a1, arg2_type a2 ) const
00106 {
00107 return _f( a1, a2 );
00108 }
00109
00110 private:
00111 _Functor & _f;
00112 };
00113
00114 struct nil
00115 {};
00116 }
00118
00122 template <class _Functor, class res_type, class arg1_type = functor_detail::nil,
00123 class arg2_type = functor_detail::nil>
00124 struct FunctorRef
00125 : public functor_detail::FunctorRef2<_Functor, res_type, arg1_type, arg2_type>
00126 {
00127 FunctorRef( _Functor & f_r )
00128 : functor_detail::FunctorRef2<_Functor, res_type, arg1_type, arg2_type>( f_r )
00129 {}
00130 };
00131
00135 template <class _Functor, class res_type, class arg1_type>
00136 struct FunctorRef<_Functor, res_type, arg1_type>
00137 : public functor_detail::FunctorRef1<_Functor, res_type, arg1_type>
00138 {
00139 FunctorRef( _Functor & f_r )
00140 : functor_detail::FunctorRef1<_Functor, res_type, arg1_type>( f_r )
00141 {}
00142 };
00143
00147 template <class _Functor, class res_type>
00148 struct FunctorRef<_Functor, res_type>
00149 : public functor_detail::FunctorRef0<_Functor, res_type>
00150 {
00151 FunctorRef( _Functor & f_r )
00152 : functor_detail::FunctorRef0<_Functor, res_type>( f_r )
00153 {}
00154 };
00155
00157 template <class res_type, class arg1_type, class arg2_type, class _Functor>
00158 FunctorRef<_Functor, res_type, arg1_type, arg2_type>
00159 functorRef( _Functor & f_r )
00160 { return FunctorRef<_Functor, res_type, arg1_type, arg2_type>( f_r ); }
00162 template <class res_type, class arg1_type, class _Functor>
00163 FunctorRef<_Functor, res_type, arg1_type>
00164 functorRef( _Functor & f_r )
00165 { return FunctorRef<_Functor, res_type, arg1_type>( f_r ); }
00167 template <class res_type, class _Functor>
00168 FunctorRef<_Functor, res_type>
00169 functorRef( _Functor & f_r )
00170 { return FunctorRef<_Functor, res_type>( f_r ); }
00171
00173
00204
00207 struct True
00208 {
00209 template<class _Tp>
00210 bool operator()( _Tp ) const
00211 {
00212 return true;
00213 }
00214 };
00215
00217 inline True true_c()
00218 { return True(); }
00219
00222 struct False
00223 {
00224 template<class _Tp>
00225 bool operator()( _Tp ) const
00226 {
00227 return false;
00228 }
00229 };
00230
00232 inline False false_c()
00233 { return False(); }
00234
00237 template<class _Condition>
00238 struct Not
00239 {
00240 Not( _Condition cond_r )
00241 : _cond( cond_r )
00242 {}
00243
00244 template<class _Tp>
00245 bool operator()( _Tp t ) const
00246 {
00247 return ! _cond( t );
00248 }
00249
00250 _Condition _cond;
00251 };
00252
00254 template<class _Condition>
00255 inline Not<_Condition> not_c( _Condition cond_r )
00256 {
00257 return Not<_Condition>( cond_r );
00258 }
00259
00262 template<class _ACondition, class _BCondition>
00263 struct Or
00264 {
00265 Or( _ACondition conda_r, _BCondition condb_r )
00266 : _conda( conda_r )
00267 , _condb( condb_r )
00268 {}
00269
00270 template<class _Tp>
00271 bool operator()( _Tp t ) const
00272 {
00273 return _conda( t ) || _condb( t );
00274 }
00275
00276 _ACondition _conda;
00277 _BCondition _condb;
00278 };
00279
00283 template<class _ACondition, class _BCondition>
00284 inline Or<_ACondition, _BCondition> or_c( _ACondition conda_r, _BCondition condb_r )
00285 {
00286 return Or<_ACondition, _BCondition>( conda_r, condb_r );
00287 }
00288
00291 template<class _ACondition, class _BCondition>
00292 struct Chain
00293 {
00294 Chain( _ACondition conda_r, _BCondition condb_r )
00295 : _conda( conda_r )
00296 , _condb( condb_r )
00297 {}
00298
00299 template<class _Tp>
00300 bool operator()( _Tp t ) const
00301 {
00302 return _conda( t ) && _condb( t );
00303 }
00304
00305 _ACondition _conda;
00306 _BCondition _condb;
00307 };
00308
00312 template<class _ACondition, class _BCondition>
00313 inline Chain<_ACondition, _BCondition> chain( _ACondition conda_r, _BCondition condb_r )
00314 {
00315 return Chain<_ACondition, _BCondition>( conda_r, condb_r );
00316 }
00317
00319
00320
00322 }
00325 }
00327 #endif // ZYPP_BASE_FUNCTIONAL_H