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 the
00026    C++ Standard Library.
00027   */
00028   using boost::mem_fun;
00029   using boost::mem_fun_ref;
00030 
00031 
00033   namespace functor
00034   { 
00035 
00075 
00076     namespace functor_detail
00077     {
00078       template <class _Functor, class res_type>
00079         struct FunctorRef0
00080         {
00081           FunctorRef0( _Functor & f_r )
00082           : _f( f_r )
00083           {}
00084 
00085           res_type operator()() const
00086           {
00087           return _f();
00088           }
00089 
00090         private:
00091           _Functor & _f;
00092         };
00093 
00094       template <class _Functor, class res_type, class arg1_type>
00095         struct FunctorRef1 : public std::unary_function<arg1_type, res_type>
00096         {
00097           FunctorRef1( _Functor & f_r )
00098           : _f( f_r )
00099           {}
00100 
00101           res_type operator()( arg1_type a1 ) const
00102           {
00103             return _f( a1 );
00104           }
00105 
00106         private:
00107           _Functor & _f;
00108         };
00109 
00110       template <class _Functor, class res_type, class arg1_type, class arg2_type>
00111         struct FunctorRef2 : public std::binary_function<arg1_type, arg2_type, res_type>
00112         {
00113           FunctorRef2( _Functor & f_r )
00114           : _f( f_r )
00115           {}
00116 
00117           res_type operator()( arg1_type a1, arg2_type a2 ) const
00118           {
00119             return _f( a1, a2 );
00120           }
00121 
00122         private:
00123           _Functor & _f;
00124         };
00125 
00126       struct nil
00127       {};
00128     }
00130 
00134     template <class _Functor, class res_type, class arg1_type = functor_detail::nil,
00135                                               class arg2_type = functor_detail::nil>
00136       struct FunctorRef
00137       : public functor_detail::FunctorRef2<_Functor, res_type, arg1_type, arg2_type>
00138       {
00139         FunctorRef( _Functor & f_r )
00140         : functor_detail::FunctorRef2<_Functor, res_type, arg1_type, arg2_type>( f_r )
00141         {}
00142       };
00143 
00147     template <class _Functor, class res_type, class arg1_type>
00148       struct FunctorRef<_Functor, res_type, arg1_type>
00149       : public functor_detail::FunctorRef1<_Functor, res_type, arg1_type>
00150       {
00151         FunctorRef( _Functor & f_r )
00152         : functor_detail::FunctorRef1<_Functor, res_type, arg1_type>( f_r )
00153         {}
00154       };
00155 
00159     template <class _Functor, class res_type>
00160       struct FunctorRef<_Functor, res_type>
00161       : public functor_detail::FunctorRef0<_Functor, res_type>
00162       {
00163         FunctorRef( _Functor & f_r )
00164         : functor_detail::FunctorRef0<_Functor, res_type>( f_r )
00165         {}
00166       };
00167 
00169     template <class res_type, class arg1_type, class arg2_type, class _Functor>
00170       FunctorRef<_Functor, res_type, arg1_type, arg2_type>
00171       functorRef( _Functor & f_r )
00172       { return FunctorRef<_Functor, res_type, arg1_type, arg2_type>( f_r ); }
00174     template <class res_type, class arg1_type, class _Functor>
00175       FunctorRef<_Functor, res_type, arg1_type>
00176       functorRef( _Functor & f_r )
00177       { 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 Thu Sep 14 15:38:33 2006 for zypp by  doxygen 1.4.6