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 };
00150 }
00151
00155 inline bool hasMatches( const CapSet & lhs_r, const CapSet & rhs_r )
00156 {
00157 return( forEachMatchIn( lhs_r, rhs_r, capmatch_detail::AlwaysFalse() ) < 0 );
00158 }
00159
00161
00162
00195 class ForEachMatchInPool
00196 {
00197 public:
00198 typedef function<bool(const CapAndItem &)> Action;
00199
00200 public:
00201 ForEachMatchInPool( const ResPool & pool_r,
00202 const Dep & dep_r,
00203 const Action & action_r )
00204 : _pool ( pool_r )
00205 , _dep ( dep_r )
00206 , _action( action_r )
00207 {}
00208
00209 bool operator()( const Capability & cap_r ) const
00210 {
00211 return( forEachMatchIn( _pool, _dep, cap_r, _action )
00212 >= 0 );
00213 }
00214
00215 private:
00216 ResPool _pool;
00217 Dep _dep;
00218 Action _action;
00219 };
00220
00243 inline int forEachPoolItemMatchedBy( const ResPool & pool_r,
00244 const PoolItem & poolitem_r,
00245 const Dep & poolitemdep_r,
00246 function<bool(const CapAndItem &)> action_r )
00247 {
00248 return invokeOnEach( poolitem_r->dep(poolitemdep_r).begin(),
00249 poolitem_r->dep(poolitemdep_r).end(),
00250 ForEachMatchInPool( pool_r, Dep::PROVIDES, action_r ) );
00251 }
00252
00274 inline int forEachPoolItemMatching( const ResPool & pool_r,
00275 const Dep & pooldep_r,
00276 const PoolItem & poolitem_r,
00277 function<bool(const CapAndItem &)> action_r )
00278 {
00279 return invokeOnEach( poolitem_r->dep(Dep::PROVIDES).begin(),
00280 poolitem_r->dep(Dep::PROVIDES).end(),
00281 ForEachMatchInPool( pool_r, pooldep_r, action_r ) );
00282 }
00283
00322 struct OncePerPoolItem
00323 {
00324 public:
00325 typedef function<bool(const PoolItem &)> Action;
00326
00327 public:
00328 explicit
00329 OncePerPoolItem( const PoolItem & self_r = PoolItem() )
00330 : _self ( self_r )
00331 , _uset ( new std::set<PoolItem> )
00332 {}
00333
00334 OncePerPoolItem( const Action & action_r,
00335 const PoolItem & self_r = PoolItem() )
00336 : _action( action_r )
00337 , _self ( self_r )
00338 , _uset ( new std::set<PoolItem> )
00339 {}
00340
00341 bool operator()( const CapAndItem & cai_r ) const
00342 {
00343 if ( cai_r.item == _self )
00344 return true;
00345
00346
00347 if ( _uset->insert( cai_r.item ).second
00348 && _action )
00349 return _action( cai_r.item );
00350
00351 return true;
00352 }
00353
00354 const std::set<PoolItem> & collectedItems() const
00355 { return *_uset; }
00356
00357 private:
00358 Action _action;
00359 PoolItem _self;
00360 shared_ptr<std::set<PoolItem> > _uset;
00361 };
00362
00363
00365
00367 }
00369 #endif // ZYPP_CAPMATCHHELPER_H