Functional.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #ifndef ZYPP_BASE_FUNCTIONAL_H
00013 #define ZYPP_BASE_FUNCTIONAL_H
00014 
00015 //#include <functional>
00016 #include <boost/functional.hpp>
00017 
00019 namespace zypp
00020 { 
00021 
00022   /* http://www.boost.org/libs/functional/mem_fun.html
00023 
00024    The header functional.hpp includes improved versions of
00025    the full range of member function adapters from the
00026    C++ Standard Library.
00027   */
00028   using boost::mem_fun;
00029   using boost::mem_fun_ref;
00030 
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 
00219     struct True
00220     {
00221       template<class _Tp>
00222         bool operator()( _Tp ) const
00223         {
00224           return true;
00225         }
00226     };
00227 
00229     inline True true_c()
00230     { return True(); }
00231 
00234     struct False
00235     {
00236       template<class _Tp>
00237         bool operator()( _Tp ) const
00238         {
00239           return false;
00240         }
00241     };
00242 
00244     inline False false_c()
00245     { return False(); }
00246 
00249     template<class _Condition>
00250       struct Not
00251       {
00252         Not( _Condition cond_r )
00253         : _cond( cond_r )
00254         {}
00255 
00256         template<class _Tp>
00257           bool operator()( _Tp t ) const
00258           {
00259             return ! _cond( t );
00260           }
00261 
00262         _Condition _cond;
00263       };
00264 
00266     template<class _Condition>
00267       inline Not<_Condition> not_c( _Condition cond_r )
00268       {
00269         return Not<_Condition>( cond_r );
00270       }
00271 
00274     template<class _ACondition, class _BCondition>
00275       struct Or
00276       {
00277         Or( _ACondition conda_r, _BCondition condb_r )
00278         : _conda( conda_r )
00279         , _condb( condb_r )
00280         {}
00281 
00282         template<class _Tp>
00283           bool operator()( _Tp t ) const
00284           {
00285             return _conda( t ) || _condb( t );
00286           }
00287 
00288         _ACondition _conda;
00289         _BCondition _condb;
00290       };
00291 
00295     template<class _ACondition, class _BCondition>
00296       inline Or<_ACondition, _BCondition> or_c( _ACondition conda_r, _BCondition condb_r )
00297       {
00298         return Or<_ACondition, _BCondition>( conda_r, condb_r );
00299       }
00300 
00303     template<class _ACondition, class _BCondition>
00304       struct Chain
00305       {
00306         Chain( _ACondition conda_r, _BCondition condb_r )
00307         : _conda( conda_r )
00308         , _condb( condb_r )
00309         {}
00310 
00311         template<class _Tp>
00312           bool operator()( _Tp t ) const
00313           {
00314             return _conda( t ) && _condb( t );
00315           }
00316 
00317         _ACondition _conda;
00318         _BCondition _condb;
00319       };
00320 
00324     template<class _ACondition, class _BCondition>
00325       inline Chain<_ACondition, _BCondition> chain( _ACondition conda_r, _BCondition condb_r )
00326       {
00327         return Chain<_ACondition, _BCondition>( conda_r, condb_r );
00328       }
00329 
00331 
00332 
00334   } // namespace functor
00337 } // namespace zypp
00339 #endif // ZYPP_BASE_FUNCTIONAL_H

Generated on Tue Sep 25 19:22:58 2007 for libzypp by  doxygen 1.5.3