CapMatchHelper.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #ifndef ZYPP_CAPMATCHHELPER_H
00013 #define ZYPP_CAPMATCHHELPER_H
00014 
00015 #include "zypp/base/Algorithm.h"
00016 #include "zypp/base/Function.h"
00017 #include "zypp/ResPool.h"
00018 
00020 namespace zypp
00021 { 
00022 
00027   class MatchesCapability
00028   {
00029   public:
00030     MatchesCapability( const Capability & lhs_r )
00031     : _lhs( lhs_r )
00032     {}
00033 
00034     bool operator()( const CapAndItem & capitem_r ) const
00035     { return operator()( capitem_r.cap ); }
00036 
00037     bool operator()( const Capability & rhs_r ) const
00038     { return( _lhs.matches( rhs_r ) == CapMatch::yes ); }
00039 
00040   private:
00041     const Capability & _lhs;
00042   };
00043 
00048 
00052   inline int forEachMatchIn( CapSet::const_iterator begin_r,
00053                              CapSet::const_iterator end_r,
00054                              const Capability & lhs_r,
00055                              function<bool(const Capability &)> action_r )
00056   {
00057     std::string index( lhs_r.index() );
00058     return invokeOnEach( begin_r, end_r,
00059                          MatchesCapability( lhs_r ), // filter
00060                          action_r );
00061   }
00062 
00066   inline int forEachMatchIn( const CapSet & capset_r,
00067                              const Capability & lhs_r,
00068                              function<bool(const Capability &)> action_r )
00069   {
00070     return invokeOnEach( capset_r.begin(), capset_r.end(),
00071                          MatchesCapability( lhs_r ), // filter
00072                          action_r );
00073   }
00074 
00091   inline int forEachMatchIn( const ResPool & pool_r, const Dep & dep_r,
00092                              const Capability & lhs_r,
00093                              function<bool(const CapAndItem &)> action_r )
00094   {
00095     std::string index( lhs_r.index() );
00096     return invokeOnEach( pool_r.byCapabilityIndexBegin( index, dep_r ),
00097                          pool_r.byCapabilityIndexEnd( index, dep_r ),
00098                          MatchesCapability( lhs_r ), // filter
00099                          action_r );
00100   }
00101 
00134   class ForEachMatchInPool
00135   {
00136   public:
00137     typedef function<bool(const CapAndItem &)> Action;
00138 
00139   public:
00140     ForEachMatchInPool( const ResPool & pool_r,
00141                         const Dep &     dep_r,
00142                         const Action &  action_r )
00143     : _pool  ( pool_r )
00144     , _dep   ( dep_r )
00145     , _action( action_r )
00146     {}
00147 
00148     bool operator()( const Capability & cap_r ) const
00149     {
00150       return( forEachMatchIn( _pool, _dep, cap_r, _action )
00151               >= 0 ); // i.e. _action did not return false
00152     }
00153 
00154   private:
00155     ResPool _pool;
00156     Dep     _dep;
00157     Action  _action;
00158   };
00159 
00182   inline void forEachPoolItemMatchedBy( const ResPool &  pool_r,
00183                                         const PoolItem & poolitem_r,
00184                                         const Dep &      poolitemdep_r,
00185                                         function<bool(const CapAndItem &)> action_r )
00186   {
00187     for_each( poolitem_r->dep(poolitemdep_r).begin(),
00188               poolitem_r->dep(poolitemdep_r).end(),
00189               ForEachMatchInPool( pool_r, Dep::PROVIDES, action_r ) );
00190   }
00191 
00213   inline void forEachPoolItemMatching( const ResPool &  pool_r,
00214                                        const Dep &      pooldep_r,
00215                                        const PoolItem & poolitem_r,
00216                                        function<bool(const CapAndItem &)> action_r )
00217   {
00218     for_each( poolitem_r->dep(Dep::PROVIDES).begin(),
00219               poolitem_r->dep(Dep::PROVIDES).end(),
00220               ForEachMatchInPool( pool_r, pooldep_r, action_r ) );
00221   }
00222 
00261   struct OncePerPoolItem
00262   {
00263   public:
00264     typedef function<bool(const PoolItem &)> Action;
00265 
00266   public:
00267     explicit
00268     OncePerPoolItem( const PoolItem & self_r = PoolItem() )
00269     : _self  ( self_r )
00270     , _uset  ( new std::set<PoolItem> )
00271     {}
00272 
00273     OncePerPoolItem( const Action & action_r,
00274                      const PoolItem & self_r = PoolItem() )
00275     : _action( action_r )
00276     , _self  ( self_r )
00277     , _uset  ( new std::set<PoolItem> )
00278     {}
00279 
00280     bool operator()( const CapAndItem & cai_r ) const
00281     {
00282       if ( cai_r.item == _self ) // omit _self
00283         return true;
00284       // intentionally collect items in _uset even
00285       // if no _action specified.
00286       if ( _uset->insert( cai_r.item ).second
00287            && _action )
00288         return _action( cai_r.item );
00289 
00290       return true;
00291     }
00292 
00293     const std::set<PoolItem> & collectedItems() const
00294     { return *_uset; }
00295 
00296   private:
00297     Action   _action;
00298     PoolItem _self;
00299     shared_ptr<std::set<PoolItem> > _uset;
00300   };
00301 
00302 
00304 
00306 } // namespace zypp
00308 #endif // ZYPP_CAPMATCHHELPER_H

Generated on Tue Nov 28 16:49:29 2006 for zypp by  doxygen 1.5.0