1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ResPoolProxy.cc
13 #include "zypp/base/Logger.h"
15 #include "zypp/base/Iterator.h"
16 #include "zypp/base/Algorithm.h"
17 #include "zypp/base/Functional.h"
19 #include "zypp/ResPoolProxy.h"
20 #include "zypp/ui/SelectableImpl.h"
24 ///////////////////////////////////////////////////////////////////
26 { /////////////////////////////////////////////////////////////////
28 /** Tem. friend of PoolItem */
31 void saveState( ResPool pool_r )
33 std::for_each( pool_r.begin(), pool_r.end(),
34 std::mem_fun_ref(&PoolItem::saveState) );
37 void saveState( ResPool pool_r, const ResObject::Kind & kind_r )
39 std::for_each( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
40 std::mem_fun_ref(&PoolItem::saveState) );
43 void restoreState( ResPool pool_r )
45 std::for_each( pool_r.begin(), pool_r.end(),
46 std::mem_fun_ref(&PoolItem::restoreState) );
49 void restoreState( ResPool pool_r, const ResObject::Kind & kind_r )
51 std::for_each( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
52 std::mem_fun_ref(&PoolItem::restoreState) );
55 bool diffState( ResPool pool_r ) const
57 // return whether some PoolItem::sameState reported \c false.
58 return( invokeOnEach( pool_r.begin(), pool_r.end(),
59 std::mem_fun_ref(&PoolItem::sameState) ) < 0 );
62 bool diffState( ResPool pool_r, const ResObject::Kind & kind_r ) const
64 // return whether some PoolItem::sameState reported \c false.
65 return( invokeOnEach( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
66 std::mem_fun_ref(&PoolItem::sameState) ) < 0 );
72 typedef std::set<PoolItem> ItemC;
75 void add( PoolItem it )
77 if ( it.status().isInstalled() )
78 installed.insert( it );
80 available.insert( it );
85 typedef std::map<std::string,SelC> NameC;
86 typedef std::map<ResObject::Kind,NameC> KindC;
90 /** collect from a pool */
91 void operator()( PoolItem it )
93 _kinds[it->kind()][it->name()].add( it );
97 ui::Selectable::Ptr buildSelectable( const ResObject::Kind & kind_r,
98 const std::string & name_r,
99 const ItemC & installed,
100 const ItemC & available )
102 return ui::Selectable::Ptr( new ui::Selectable(
103 ui::Selectable::Impl_Ptr( new ui::Selectable::Impl( kind_r, name_r,
111 /** Build Selectable::Ptr and feed them to some container.
112 * \todo Cleanup typedefs
114 typedef std::set<ui::Selectable::Ptr> SelectableIndex;
115 typedef std::map<ResObject::Kind,SelectableIndex> SelectablePool;
117 void feed( SelectablePool & _selPool )
119 for ( KindC::const_iterator kindIt = _kinds.begin(); kindIt != _kinds.end(); ++kindIt )
121 for ( NameC::const_iterator nameIt = kindIt->second.begin(); nameIt != kindIt->second.end(); ++nameIt )
123 const ItemC & installed( nameIt->second.installed );
124 const ItemC & available( nameIt->second.available );
126 if ( installed.empty() && available.empty() )
129 _selPool[kindIt->first].insert( buildSelectable( kindIt->first, nameIt->first, installed, available ) );
135 ///////////////////////////////////////////////////////////////////
137 // CLASS NAME : ResPoolProxy::Impl
139 /** ResPoolProxy implementation. */
140 struct ResPoolProxy::Impl
144 :_pool( ResPool::instance() )
147 Impl( ResPool pool_r )
150 SelPoolHelper collect;
151 std::for_each( _pool.begin(), _pool.end(),
152 functor::functorRef<void,PoolItem>( collect ) );
153 collect.feed( _selPool );
158 bool empty( const ResObject::Kind & kind_r ) const
159 { return _selPool[kind_r].empty(); }
161 size_type size( const ResObject::Kind & kind_r ) const
162 { return _selPool[kind_r].size(); }
164 const_iterator byKindBegin( const ResObject::Kind & kind_r ) const
165 { return _selPool[kind_r].begin(); }
167 const_iterator byKindEnd( const ResObject::Kind & kind_r ) const
168 { return _selPool[kind_r].end(); }
171 size_type knownRepositoriesSize() const
172 { return _pool.knownRepositoriesSize(); }
174 repository_iterator knownRepositoriesBegin() const
175 { return _pool.knownRepositoriesBegin(); }
177 repository_iterator knownRepositoriesEnd() const
178 { return _pool.knownRepositoriesEnd(); }
182 void saveState() const
183 { PoolItemSaver().saveState( _pool ); }
185 void saveState( const ResObject::Kind & kind_r ) const
186 { PoolItemSaver().saveState( _pool, kind_r ); }
188 void restoreState() const
189 { PoolItemSaver().restoreState( _pool ); }
191 void restoreState( const ResObject::Kind & kind_r ) const
192 { PoolItemSaver().restoreState( _pool, kind_r ); }
194 bool diffState() const
195 { return PoolItemSaver().diffState( _pool ); }
197 bool diffState( const ResObject::Kind & kind_r ) const
198 { return PoolItemSaver().diffState( _pool, kind_r ); }
202 mutable SelectablePool _selPool;
205 /** Offer default Impl. */
206 static shared_ptr<Impl> nullimpl()
208 static shared_ptr<Impl> _nullimpl( new Impl );
212 ///////////////////////////////////////////////////////////////////
214 /** \relates ResPoolProxy::Impl Stream output */
215 inline std::ostream & operator<<( std::ostream & str, const ResPoolProxy::Impl & obj )
217 return str << "ResPoolProxy::Impl";
220 ///////////////////////////////////////////////////////////////////
222 // CLASS NAME : ResPoolProxy
224 ///////////////////////////////////////////////////////////////////
226 ///////////////////////////////////////////////////////////////////
228 // METHOD NAME : ResPoolProxy::ResPoolProxy
229 // METHOD TYPE : Ctor
231 ResPoolProxy::ResPoolProxy()
232 : _pimpl( Impl::nullimpl() )
235 ///////////////////////////////////////////////////////////////////
237 // METHOD NAME : ResPoolProxy::ResPoolProxy
238 // METHOD TYPE : Ctor
240 ResPoolProxy::ResPoolProxy( ResPool pool_r )
241 : _pimpl( new Impl( pool_r ) )
244 ///////////////////////////////////////////////////////////////////
246 // METHOD NAME : ResPoolProxy::~ResPoolProxy
247 // METHOD TYPE : Dtor
249 ResPoolProxy::~ResPoolProxy()
252 ///////////////////////////////////////////////////////////////////
254 // forward to implementation
256 ///////////////////////////////////////////////////////////////////
258 bool ResPoolProxy::empty( const ResObject::Kind & kind_r ) const
259 { return _pimpl->empty( kind_r ); }
261 ResPoolProxy::size_type ResPoolProxy::size( const ResObject::Kind & kind_r ) const
262 { return _pimpl->size( kind_r ); }
264 ResPoolProxy::const_iterator ResPoolProxy::byKindBegin( const ResObject::Kind & kind_r ) const
265 { return _pimpl->byKindBegin( kind_r ); }
267 ResPoolProxy::const_iterator ResPoolProxy::byKindEnd( const ResObject::Kind & kind_r ) const
268 { return _pimpl->byKindEnd( kind_r ); }
270 ResPoolProxy::size_type ResPoolProxy::knownRepositoriesSize() const
271 { return _pimpl->knownRepositoriesSize(); }
273 ResPoolProxy::repository_iterator ResPoolProxy::knownRepositoriesBegin() const
274 { return _pimpl->knownRepositoriesBegin(); }
276 ResPoolProxy::repository_iterator ResPoolProxy::knownRepositoriesEnd() const
277 { return _pimpl->knownRepositoriesEnd(); }
279 void ResPoolProxy::saveState() const
280 { _pimpl->saveState(); }
282 void ResPoolProxy::saveState( const ResObject::Kind & kind_r ) const
283 { _pimpl->saveState( kind_r ); }
285 void ResPoolProxy::restoreState() const
286 { _pimpl->restoreState(); }
288 void ResPoolProxy::restoreState( const ResObject::Kind & kind_r ) const
289 { _pimpl->restoreState( kind_r ); }
291 bool ResPoolProxy::diffState() const
292 { return _pimpl->diffState(); }
294 bool ResPoolProxy::diffState( const ResObject::Kind & kind_r ) const
295 { return _pimpl->diffState( kind_r ); }
297 /******************************************************************
299 ** FUNCTION NAME : operator<<
300 ** FUNCTION TYPE : std::ostream &
302 std::ostream & operator<<( std::ostream & str, const ResPoolProxy & obj )
304 return str << *obj._pimpl;
307 /////////////////////////////////////////////////////////////////
309 ///////////////////////////////////////////////////////////////////