libzypp  10.5.0
ResPoolProxy.cc
Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <iostream>
00013 #include "zypp/base/LogTools.h"
00014 
00015 #include "zypp/base/Iterator.h"
00016 #include "zypp/base/Algorithm.h"
00017 #include "zypp/base/Functional.h"
00018 
00019 #include "zypp/ResPoolProxy.h"
00020 #include "zypp/pool/PoolImpl.h"
00021 #include "zypp/ui/SelectableImpl.h"
00022 
00023 using std::endl;
00024 
00026 namespace zypp
00027 { 
00028 
00030   struct PoolItemSaver
00031   {
00032     void saveState( ResPool pool_r )
00033     {
00034       std::for_each( pool_r.begin(), pool_r.end(),
00035                      std::mem_fun_ref(&PoolItem::saveState) );
00036     }
00037 
00038     void saveState( ResPool pool_r, const ResKind & kind_r )
00039     {
00040       std::for_each( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
00041                      std::mem_fun_ref(&PoolItem::saveState) );
00042     }
00043 
00044     void restoreState( ResPool pool_r )
00045     {
00046       std::for_each( pool_r.begin(), pool_r.end(),
00047                      std::mem_fun_ref(&PoolItem::restoreState) );
00048     }
00049 
00050     void restoreState( ResPool pool_r, const ResKind & kind_r )
00051     {
00052       std::for_each( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
00053                      std::mem_fun_ref(&PoolItem::restoreState) );
00054     }
00055 
00056     bool diffState( ResPool pool_r ) const
00057     {
00058       // return whether some PoolItem::sameState reported \c false.
00059       return( invokeOnEach( pool_r.begin(), pool_r.end(),
00060                             std::mem_fun_ref(&PoolItem::sameState) ) < 0 );
00061     }
00062 
00063     bool diffState( ResPool pool_r, const ResKind & kind_r ) const
00064     {
00065       // return whether some PoolItem::sameState reported \c false.
00066       return( invokeOnEach( pool_r.byKindBegin(kind_r), pool_r.byKindEnd(kind_r),
00067                             std::mem_fun_ref(&PoolItem::sameState) ) < 0 );
00068     }
00069   };
00070 
00071   namespace
00072   {
00073     ui::Selectable::Ptr makeSelectablePtr( pool::PoolImpl::Id2ItemT::const_iterator begin_r,
00074                                            pool::PoolImpl::Id2ItemT::const_iterator end_r )
00075     {
00076       pool::PoolTraits::byIdent_iterator begin( begin_r, pool::PoolTraits::Id2ItemValueSelector() );
00077       pool::PoolTraits::byIdent_iterator end( end_r, pool::PoolTraits::Id2ItemValueSelector() );
00078       sat::Solvable solv( begin->satSolvable() );
00079 
00080       return new ui::Selectable( ui::Selectable::Impl_Ptr( new ui::Selectable::Impl( solv.kind(), solv.name(), begin, end ) ) );
00081     }
00082   } // namespace
00083 
00085   //
00086   //    CLASS NAME : ResPoolProxy::Impl
00087   //
00091   struct ResPoolProxy::Impl
00092   {
00093     friend std::ostream & operator<<( std::ostream & str, const Impl & obj );
00094     friend std::ostream & dumpOn( std::ostream & str, const Impl & obj );
00095 
00096     typedef std::tr1::unordered_map<sat::detail::IdType,ui::Selectable::Ptr> SelectableIndex;
00097     typedef ResPoolProxy::const_iterator const_iterator;
00098 
00099   public:
00100     Impl()
00101     :_pool( ResPool::instance() )
00102     {}
00103 
00104     Impl( ResPool pool_r, const pool::PoolImpl & poolImpl_r )
00105     : _pool( pool_r )
00106     {
00107       const pool::PoolImpl::Id2ItemT & id2item( poolImpl_r.id2item() );
00108       if ( ! id2item.empty() )
00109       {
00110         // set startpoint
00111         pool::PoolImpl::Id2ItemT::const_iterator cbegin = id2item.begin();
00112 
00113         for_( it, id2item.begin(), id2item.end() )
00114         {
00115           if ( it->first != cbegin->first )
00116           {
00117             // starting a new Selectable, create the previous one
00118             ui::Selectable::Ptr p( makeSelectablePtr( cbegin, it ) );
00119             _selPool.insert( SelectablePool::value_type( p->kind(), p ) );
00120             _selIndex[cbegin->first] = p;
00121             // remember new startpoint
00122             cbegin = it;
00123           }
00124         }
00125         // create the final one
00126         ui::Selectable::Ptr p( makeSelectablePtr( cbegin, id2item.end() ) );
00127         _selPool.insert( SelectablePool::value_type( p->kind(), p ) );
00128         _selIndex[cbegin->first] = p;
00129       }
00130     }
00131 
00132   public:
00133     ui::Selectable::Ptr lookup( const pool::ByIdent & ident_r ) const
00134     {
00135       SelectableIndex::const_iterator it( _selIndex.find( ident_r.get() ) );
00136       if ( it != _selIndex.end() )
00137         return it->second;
00138       return ui::Selectable::Ptr();
00139     }
00140 
00141   public:
00142     bool empty() const
00143     { return _selPool.empty(); }
00144 
00145     size_type size() const
00146     { return _selPool.size(); }
00147 
00148     const_iterator begin() const
00149     { return make_map_value_begin( _selPool ); }
00150 
00151     const_iterator end() const
00152     { return make_map_value_end( _selPool ); }
00153 
00154   public:
00155     bool empty( const ResKind & kind_r ) const
00156     { return( _selPool.count( kind_r ) == 0 );  }
00157 
00158     size_type size( const ResKind & kind_r ) const
00159     { return _selPool.count( kind_r ); }
00160 
00161     const_iterator byKindBegin( const ResKind & kind_r ) const
00162     { return make_map_value_lower_bound( _selPool, kind_r ); }
00163 
00164     const_iterator byKindEnd( const ResKind & kind_r ) const
00165     { return make_map_value_upper_bound( _selPool, kind_r ); }
00166 
00167   public:
00168     size_type knownRepositoriesSize() const
00169     { return _pool.knownRepositoriesSize(); }
00170 
00171     repository_iterator knownRepositoriesBegin() const
00172     { return _pool.knownRepositoriesBegin(); }
00173 
00174     repository_iterator knownRepositoriesEnd() const
00175     { return _pool.knownRepositoriesEnd(); }
00176 
00177   public:
00178 
00179     void saveState() const
00180     { PoolItemSaver().saveState( _pool ); }
00181 
00182     void saveState( const ResKind & kind_r ) const
00183     { PoolItemSaver().saveState( _pool, kind_r ); }
00184 
00185     void restoreState() const
00186     { PoolItemSaver().restoreState( _pool ); }
00187 
00188     void restoreState( const ResKind & kind_r ) const
00189     { PoolItemSaver().restoreState( _pool, kind_r ); }
00190 
00191     bool diffState() const
00192     { return PoolItemSaver().diffState( _pool ); }
00193 
00194     bool diffState( const ResKind & kind_r ) const
00195     { return PoolItemSaver().diffState( _pool, kind_r ); }
00196 
00197   private:
00198     ResPool _pool;
00199     mutable SelectablePool _selPool;
00200     mutable SelectableIndex _selIndex;
00201 
00202   public:
00204     static shared_ptr<Impl> nullimpl()
00205     {
00206       static shared_ptr<Impl> _nullimpl( new Impl );
00207       return _nullimpl;
00208     }
00209   };
00211 
00213   inline std::ostream & operator<<( std::ostream & str, const ResPoolProxy::Impl & obj )
00214   {
00215     return str << "ResPoolProxy (" << obj._pool.serial() << ") [" << obj._pool.size()
00216                << "solv/" << obj.size()<< "sel]";
00217   }
00218 
00219   namespace detail
00220   {
00221     struct DumpFilter
00222     {
00223       bool operator()( const ui::Selectable::Ptr & selp ) const
00224       { return selp->toModify(); }
00225     };
00226   }
00227 
00229   inline std::ostream & dumpOn( std::ostream & str, const ResPoolProxy::Impl & obj )
00230   {
00231     detail::DumpFilter f;
00232     return dumpRange( str << obj << " toModify: ",
00233                       make_filter_begin( f, obj ),
00234                       make_filter_end( f, obj ) );
00235   }
00236 
00238   //
00239   //    CLASS NAME : ResPoolProxy
00240   //
00242 
00244   //
00245   //    METHOD NAME : ResPoolProxy::ResPoolProxy
00246   //    METHOD TYPE : Ctor
00247   //
00248   ResPoolProxy::ResPoolProxy()
00249   : _pimpl( Impl::nullimpl() )
00250   {}
00251 
00253   //
00254   //    METHOD NAME : ResPoolProxy::ResPoolProxy
00255   //    METHOD TYPE : Ctor
00256   //
00257   ResPoolProxy::ResPoolProxy( ResPool pool_r, const pool::PoolImpl & poolImpl_r )
00258   : _pimpl( new Impl( pool_r, poolImpl_r ) )
00259   {}
00260 
00262   //
00263   //    METHOD NAME : ResPoolProxy::~ResPoolProxy
00264   //    METHOD TYPE : Dtor
00265   //
00266   ResPoolProxy::~ResPoolProxy()
00267   {}
00268 
00270   //
00271   // forward to implementation
00272   //
00274 
00275   ui::Selectable::Ptr ResPoolProxy::lookup( const pool::ByIdent & ident_r ) const
00276   { return _pimpl->lookup( ident_r ); }
00277 
00278   bool ResPoolProxy::empty() const
00279   { return _pimpl->empty(); }
00280 
00281   ResPoolProxy::size_type ResPoolProxy::size() const
00282   { return _pimpl->size(); }
00283 
00284   ResPoolProxy::const_iterator ResPoolProxy::begin() const
00285   { return _pimpl->begin(); }
00286 
00287   ResPoolProxy::const_iterator ResPoolProxy::end() const
00288   { return _pimpl->end(); }
00289 
00290   bool ResPoolProxy::empty( const ResKind & kind_r ) const
00291   { return _pimpl->empty( kind_r ); }
00292 
00293   ResPoolProxy::size_type ResPoolProxy::size( const ResKind & kind_r ) const
00294   { return _pimpl->size( kind_r ); }
00295 
00296   ResPoolProxy::const_iterator ResPoolProxy::byKindBegin( const ResKind & kind_r ) const
00297   { return _pimpl->byKindBegin( kind_r ); }
00298 
00299   ResPoolProxy::const_iterator ResPoolProxy::byKindEnd( const ResKind & kind_r ) const
00300   { return _pimpl->byKindEnd( kind_r ); }
00301 
00302   ResPoolProxy::size_type ResPoolProxy::knownRepositoriesSize() const
00303   { return _pimpl->knownRepositoriesSize(); }
00304 
00305   ResPoolProxy::repository_iterator ResPoolProxy::knownRepositoriesBegin() const
00306   { return _pimpl->knownRepositoriesBegin(); }
00307 
00308   ResPoolProxy::repository_iterator ResPoolProxy::knownRepositoriesEnd() const
00309   { return _pimpl->knownRepositoriesEnd(); }
00310 
00311   void ResPoolProxy::saveState() const
00312   { _pimpl->saveState(); }
00313 
00314   void ResPoolProxy::saveState( const ResKind & kind_r ) const
00315   { _pimpl->saveState( kind_r ); }
00316 
00317   void ResPoolProxy::restoreState() const
00318   { _pimpl->restoreState(); }
00319 
00320   void ResPoolProxy::restoreState( const ResKind & kind_r ) const
00321   { _pimpl->restoreState( kind_r ); }
00322 
00323   bool ResPoolProxy::diffState() const
00324   { return _pimpl->diffState(); }
00325 
00326   bool ResPoolProxy::diffState( const ResKind & kind_r ) const
00327   { return _pimpl->diffState( kind_r ); }
00328 
00329   std::ostream & operator<<( std::ostream & str, const ResPoolProxy & obj )
00330   { return str << *obj._pimpl; }
00331 
00332   std::ostream & dumpOn( std::ostream & str, const ResPoolProxy & obj )
00333   { return dumpOn( str, *obj._pimpl ); }
00334 
00336 } // namespace zypp