00001
00002
00003
00004
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 ),
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 ),
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 ),
00099 action_r );
00100 }
00101
00102
00104
00111 class ForEachMatchInCapSet
00112 {
00113 public:
00114 typedef function<bool(const Capability &, const Capability &)> Action;
00115
00116 public:
00117 ForEachMatchInCapSet( const CapSet & set_r, const Action & action_r )
00118 : _set( set_r )
00119 , _action( action_r )
00120 {}
00121
00122 bool operator()( const Capability & cap_r ) const
00123 {
00124 return( forEachMatchIn( _set, cap_r, bind( _action, _1, cap_r ) )
00125 >= 0 );
00126 }
00127
00128 private:
00129 const CapSet & _set;
00130 Action _action;
00131 };
00132
00135 inline int forEachMatchIn( const CapSet & lhs_r,
00136 const CapSet & rhs_r,
00137 function<bool(const Capability &, const Capability &)> action_r )
00138 {
00139 return invokeOnEach( lhs_r.begin(), lhs_r.end(),
00140 ForEachMatchInCapSet( rhs_r, action_r ) );
00141 }
00143
00144 namespace capmatch_detail {
00145 struct AlwaysFalse
00146 {
00147 bool operator()( const Capability &, const Capability & ) const
00148 { return false; }
00149 bool operator()( const Capability & ) const
00150 { return false; }
00151 };
00152 }
00153
00157 inline bool hasMatches( const CapSet & lhs_r, const Capability & rhs_r )
00158 {
00159 return( forEachMatchIn( lhs_r, rhs_r, capmatch_detail::AlwaysFalse() ) < 0 );
00160 }
00161
00165 inline bool hasMatches( const CapSet & lhs_r, const CapSet & rhs_r )
00166 {
00167 return( forEachMatchIn( lhs_r, rhs_r, capmatch_detail::AlwaysFalse() ) < 0 );
00168 }
00169
00171
00172
00205 class ForEachMatchInPool
00206 {
00207 public:
00208 typedef function<bool(const CapAndItem &)> Action;
00209
00210 public:
00211 ForEachMatchInPool( const ResPool & pool_r,
00212 const Dep & dep_r,
00213 const Action & action_r )
00214 : _pool ( pool_r )
00215 , _dep ( dep_r )
00216 , _action( action_r )
00217 {}
00218
00219 bool operator()( const Capability & cap_r ) const
00220 {
00221 return( forEachMatchIn( _pool, _dep, cap_r, _action )
00222 >= 0 );
00223 }
00224
00225 private:
00226 ResPool _pool;
00227 Dep _dep;
00228 Action _action;
00229 };
00230
00253 inline int forEachPoolItemMatchedBy( const ResPool & pool_r,
00254 const PoolItem & poolitem_r,
00255 const Dep & poolitemdep_r,
00256 function<bool(const CapAndItem &)> action_r )
00257 {
00258 return invokeOnEach( poolitem_r->dep(poolitemdep_r).begin(),
00259 poolitem_r->dep(poolitemdep_r).end(),
00260 ForEachMatchInPool( pool_r, Dep::PROVIDES, action_r ) );
00261 }
00262
00284 inline int forEachPoolItemMatching( const ResPool & pool_r,
00285 const Dep & pooldep_r,
00286 const PoolItem & poolitem_r,
00287 function<bool(const CapAndItem &)> action_r )
00288 {
00289 return invokeOnEach( poolitem_r->dep(Dep::PROVIDES).begin(),
00290 poolitem_r->dep(Dep::PROVIDES).end(),
00291 ForEachMatchInPool( pool_r, pooldep_r, action_r ) );
00292 }
00293
00332 struct OncePerPoolItem
00333 {
00334 public:
00335 typedef function<bool(const PoolItem &)> Action;
00336
00337 public:
00338 explicit
00339 OncePerPoolItem( const PoolItem & self_r = PoolItem() )
00340 : _self ( self_r )
00341 , _uset ( new std::set<PoolItem> )
00342 {}
00343
00344 OncePerPoolItem( const Action & action_r,
00345 const PoolItem & self_r = PoolItem() )
00346 : _action( action_r )
00347 , _self ( self_r )
00348 , _uset ( new std::set<PoolItem> )
00349 {}
00350
00351 bool operator()( const CapAndItem & cai_r ) const
00352 {
00353 if ( cai_r.item == _self )
00354 return true;
00355
00356
00357 if ( _uset->insert( cai_r.item ).second
00358 && _action )
00359 return _action( cai_r.item );
00360
00361 return true;
00362 }
00363
00364 const std::set<PoolItem> & collectedItems() const
00365 { return *_uset; }
00366
00367 private:
00368 Action _action;
00369 PoolItem _self;
00370 shared_ptr<std::set<PoolItem> > _uset;
00371 };
00372
00373
00375
00377 }
00379 #endif // ZYPP_CAPMATCHHELPER_H