1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ResPoolProxy.cc
13 #include "zypp/base/LogTools.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/pool/PoolImpl.h"
21 #include "zypp/ui/SelectableImpl.h"
25 ///////////////////////////////////////////////////////////////////
27 { /////////////////////////////////////////////////////////////////
29 /** Tem. friend of PoolItem */
32 void saveState( ResPool pool_r )
34 std::for_each( pool_r.begin(), pool_r.end(),
35 std::mem_fun_ref(&PoolItem::saveState) );
38 void saveState( ResPool pool_r, const ResKind & kind_r )
40 std::for_each( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
41 std::mem_fun_ref(&PoolItem::saveState) );
44 void restoreState( ResPool pool_r )
46 std::for_each( pool_r.begin(), pool_r.end(),
47 std::mem_fun_ref(&PoolItem::restoreState) );
50 void restoreState( ResPool pool_r, const ResKind & kind_r )
52 std::for_each( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
53 std::mem_fun_ref(&PoolItem::restoreState) );
56 bool diffState( ResPool pool_r ) const
58 // return whether some PoolItem::sameState reported \c false.
59 return( invokeOnEach( pool_r.begin(), pool_r.end(),
60 std::mem_fun_ref(&PoolItem::sameState) ) < 0 );
63 bool diffState( ResPool pool_r, const ResKind & kind_r ) const
65 // return whether some PoolItem::sameState reported \c false.
66 return( invokeOnEach( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
67 std::mem_fun_ref(&PoolItem::sameState) ) < 0 );
73 ui::Selectable::Ptr makeSelectablePtr( pool::PoolImpl::Id2ItemT::const_iterator begin_r,
74 pool::PoolImpl::Id2ItemT::const_iterator end_r )
76 pool::PoolTraits::byIdent_iterator begin( begin_r, pool::PoolTraits::Id2ItemValueSelector() );
77 pool::PoolTraits::byIdent_iterator end( end_r, pool::PoolTraits::Id2ItemValueSelector() );
78 sat::Solvable solv( begin->satSolvable() );
80 return new ui::Selectable( ui::Selectable::Impl_Ptr( new ui::Selectable::Impl( solv.kind(), solv.name(), begin, end ) ) );
84 ///////////////////////////////////////////////////////////////////
86 // CLASS NAME : ResPoolProxy::Impl
88 /** ResPoolProxy implementation.
89 * \todo Seedup as it is still using old index
91 struct ResPoolProxy::Impl
93 friend std::ostream & operator<<( std::ostream & str, const Impl & obj );
94 friend std::ostream & dumpOn( std::ostream & str, const Impl & obj );
96 typedef std::unordered_map<sat::detail::IdType,ui::Selectable::Ptr> SelectableIndex;
97 typedef ResPoolProxy::const_iterator const_iterator;
101 :_pool( ResPool::instance() )
104 Impl( ResPool pool_r, const pool::PoolImpl & poolImpl_r )
107 const pool::PoolImpl::Id2ItemT & id2item( poolImpl_r.id2item() );
108 if ( ! id2item.empty() )
111 pool::PoolImpl::Id2ItemT::const_iterator cbegin = id2item.begin();
113 for_( it, id2item.begin(), id2item.end() )
115 if ( it->first != cbegin->first )
117 // starting a new Selectable, create the previous one
118 ui::Selectable::Ptr p( makeSelectablePtr( cbegin, it ) );
119 _selPool.insert( SelectablePool::value_type( p->kind(), p ) );
120 _selIndex[cbegin->first] = p;
121 // remember new startpoint
125 // create the final one
126 ui::Selectable::Ptr p( makeSelectablePtr( cbegin, id2item.end() ) );
127 _selPool.insert( SelectablePool::value_type( p->kind(), p ) );
128 _selIndex[cbegin->first] = p;
133 ui::Selectable::Ptr lookup( const pool::ByIdent & ident_r ) const
135 SelectableIndex::const_iterator it( _selIndex.find( ident_r.get() ) );
136 if ( it != _selIndex.end() )
138 return ui::Selectable::Ptr();
143 { return _selPool.empty(); }
145 size_type size() const
146 { return _selPool.size(); }
148 const_iterator begin() const
149 { return make_map_value_begin( _selPool ); }
151 const_iterator end() const
152 { return make_map_value_end( _selPool ); }
155 bool empty( const ResKind & kind_r ) const
156 { return( _selPool.count( kind_r ) == 0 ); }
158 size_type size( const ResKind & kind_r ) const
159 { return _selPool.count( kind_r ); }
161 const_iterator byKindBegin( const ResKind & kind_r ) const
162 { return make_map_value_lower_bound( _selPool, kind_r ); }
164 const_iterator byKindEnd( const ResKind & kind_r ) const
165 { return make_map_value_upper_bound( _selPool, kind_r ); }
168 size_type knownRepositoriesSize() const
169 { return _pool.knownRepositoriesSize(); }
171 repository_iterator knownRepositoriesBegin() const
172 { return _pool.knownRepositoriesBegin(); }
174 repository_iterator knownRepositoriesEnd() const
175 { return _pool.knownRepositoriesEnd(); }
179 void saveState() const
180 { PoolItemSaver().saveState( _pool ); }
182 void saveState( const ResKind & kind_r ) const
183 { PoolItemSaver().saveState( _pool, kind_r ); }
185 void restoreState() const
186 { PoolItemSaver().restoreState( _pool ); }
188 void restoreState( const ResKind & kind_r ) const
189 { PoolItemSaver().restoreState( _pool, kind_r ); }
191 bool diffState() const
192 { return PoolItemSaver().diffState( _pool ); }
194 bool diffState( const ResKind & kind_r ) const
195 { return PoolItemSaver().diffState( _pool, kind_r ); }
199 mutable SelectablePool _selPool;
200 mutable SelectableIndex _selIndex;
203 /** Offer default Impl. */
204 static shared_ptr<Impl> nullimpl()
206 static shared_ptr<Impl> _nullimpl( new Impl );
210 ///////////////////////////////////////////////////////////////////
212 /** \relates ResPoolProxy::Impl Stream output */
213 inline std::ostream & operator<<( std::ostream & str, const ResPoolProxy::Impl & obj )
215 return str << "ResPoolProxy (" << obj._pool.serial() << ") [" << obj._pool.size()
216 << "solv/" << obj.size()<< "sel]";
223 bool operator()( const ui::Selectable::Ptr & selp ) const
224 { return selp->toModify(); }
228 /** \relates ResPoolProxy::Impl Verbose stream output */
229 inline std::ostream & dumpOn( std::ostream & str, const ResPoolProxy::Impl & obj )
231 detail::DumpFilter f;
232 return dumpRange( str << obj << " toModify: ",
233 make_filter_begin( f, obj ),
234 make_filter_end( f, obj ) );
237 ///////////////////////////////////////////////////////////////////
239 // CLASS NAME : ResPoolProxy
241 ///////////////////////////////////////////////////////////////////
243 ///////////////////////////////////////////////////////////////////
245 // METHOD NAME : ResPoolProxy::ResPoolProxy
246 // METHOD TYPE : Ctor
248 ResPoolProxy::ResPoolProxy()
249 : _pimpl( Impl::nullimpl() )
252 ///////////////////////////////////////////////////////////////////
254 // METHOD NAME : ResPoolProxy::ResPoolProxy
255 // METHOD TYPE : Ctor
257 ResPoolProxy::ResPoolProxy( ResPool pool_r, const pool::PoolImpl & poolImpl_r )
258 : _pimpl( new Impl( pool_r, poolImpl_r ) )
261 ///////////////////////////////////////////////////////////////////
263 // METHOD NAME : ResPoolProxy::~ResPoolProxy
264 // METHOD TYPE : Dtor
266 ResPoolProxy::~ResPoolProxy()
269 ///////////////////////////////////////////////////////////////////
271 // forward to implementation
273 ///////////////////////////////////////////////////////////////////
275 ui::Selectable::Ptr ResPoolProxy::lookup( const pool::ByIdent & ident_r ) const
276 { return _pimpl->lookup( ident_r ); }
278 bool ResPoolProxy::empty() const
279 { return _pimpl->empty(); }
281 ResPoolProxy::size_type ResPoolProxy::size() const
282 { return _pimpl->size(); }
284 ResPoolProxy::const_iterator ResPoolProxy::begin() const
285 { return _pimpl->begin(); }
287 ResPoolProxy::const_iterator ResPoolProxy::end() const
288 { return _pimpl->end(); }
290 bool ResPoolProxy::empty( const ResKind & kind_r ) const
291 { return _pimpl->empty( kind_r ); }
293 ResPoolProxy::size_type ResPoolProxy::size( const ResKind & kind_r ) const
294 { return _pimpl->size( kind_r ); }
296 ResPoolProxy::const_iterator ResPoolProxy::byKindBegin( const ResKind & kind_r ) const
297 { return _pimpl->byKindBegin( kind_r ); }
299 ResPoolProxy::const_iterator ResPoolProxy::byKindEnd( const ResKind & kind_r ) const
300 { return _pimpl->byKindEnd( kind_r ); }
302 ResPoolProxy::size_type ResPoolProxy::knownRepositoriesSize() const
303 { return _pimpl->knownRepositoriesSize(); }
305 ResPoolProxy::repository_iterator ResPoolProxy::knownRepositoriesBegin() const
306 { return _pimpl->knownRepositoriesBegin(); }
308 ResPoolProxy::repository_iterator ResPoolProxy::knownRepositoriesEnd() const
309 { return _pimpl->knownRepositoriesEnd(); }
311 void ResPoolProxy::saveState() const
312 { _pimpl->saveState(); }
314 void ResPoolProxy::saveState( const ResKind & kind_r ) const
315 { _pimpl->saveState( kind_r ); }
317 void ResPoolProxy::restoreState() const
318 { _pimpl->restoreState(); }
320 void ResPoolProxy::restoreState( const ResKind & kind_r ) const
321 { _pimpl->restoreState( kind_r ); }
323 bool ResPoolProxy::diffState() const
324 { return _pimpl->diffState(); }
326 bool ResPoolProxy::diffState( const ResKind & kind_r ) const
327 { return _pimpl->diffState( kind_r ); }
329 std::ostream & operator<<( std::ostream & str, const ResPoolProxy & obj )
330 { return str << *obj._pimpl; }
332 std::ostream & dumpOn( std::ostream & str, const ResPoolProxy & obj )
333 { return dumpOn( str, *obj._pimpl ); }
335 /////////////////////////////////////////////////////////////////
337 ///////////////////////////////////////////////////////////////////