Imported Upstream version 17.14.0
[platform/upstream/libzypp.git] / zypp / ResPoolProxy.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/ResPoolProxy.h
10  *
11 */
12 #ifndef ZYPP_RESPOOLPROXY_H
13 #define ZYPP_RESPOOLPROXY_H
14
15 #include <iosfwd>
16
17 #include "zypp/base/PtrTypes.h"
18
19 #include "zypp/ResPool.h"
20 #include "zypp/ui/Selectable.h"
21 #include "zypp/ui/SelFilters.h"
22
23 ///////////////////////////////////////////////////////////////////
24 namespace zypp
25 { /////////////////////////////////////////////////////////////////
26
27   ///////////////////////////////////////////////////////////////////
28   //
29   //    CLASS NAME : ResPoolProxy
30   //
31   /** ResPool::instance().proxy();
32    * \todo integrate it into ResPool
33   */
34   class ResPoolProxy
35   {
36     friend std::ostream & operator<<( std::ostream & str, const ResPoolProxy & obj );
37     friend std::ostream & dumpOn( std::ostream & str, const ResPoolProxy & obj );
38     typedef std::multimap<ResKind,ui::Selectable::Ptr> SelectablePool;
39
40   public:
41     /** Implementation  */
42     struct Impl;
43
44     typedef MapKVIteratorTraits<SelectablePool>::Value_const_iterator const_iterator;
45     typedef SelectablePool::size_type size_type;
46
47     typedef ResPool::repository_iterator    repository_iterator;
48
49   public:
50
51     /** Default ctor: no pool
52      * Nonempty proxies are provided by \ref ResPool.
53      * \see \ref ResPool::proxy
54      * \code
55      * ResPoolProxy p( ResPool::instance().proxy() );
56      * \endcode
57      */
58     ResPoolProxy();
59
60     /** Dtor */
61     ~ResPoolProxy();
62
63   public:
64     /** \name Lookup individual Selectables. */
65     //@{
66     ui::Selectable::Ptr lookup( const pool::ByIdent & ident_r ) const;
67
68     ui::Selectable::Ptr lookup( IdString ident_r ) const
69     { return lookup( pool::ByIdent( ident_r ) ); }
70
71     ui::Selectable::Ptr lookup( ResKind kind_r, const std::string & name_r ) const
72     { return lookup( pool::ByIdent( kind_r, name_r ) ); }
73
74     ui::Selectable::Ptr lookup( const sat::Solvable & solv_r ) const
75     { return lookup( pool::ByIdent( solv_r ) ); }
76
77     ui::Selectable::Ptr lookup( const ResObject::constPtr & resolvable_r ) const
78     { return resolvable_r ? lookup( resolvable_r->satSolvable() ) : ui::Selectable::Ptr(); }
79
80     ui::Selectable::Ptr lookup( const PoolItem & pi_r ) const
81     { return lookup( pi_r.satSolvable() ); }
82     //@}
83
84   public:
85     /** \name Iterate through all Selectables of a all kind. */
86     //@{
87     bool empty() const;
88     size_type size() const;
89     const_iterator begin() const;
90     const_iterator end() const;
91     //@}
92
93     /** \name Iterate through all Selectables of a certain kind. */
94     //@{
95     /** True if there are items of a certain kind. */
96     bool empty( const ResKind & kind_r ) const;
97
98     template<class TRes>
99       bool empty() const
100       { return empty( ResTraits<TRes>::kind ); }
101
102     /** Number of Items of a certain kind.  */
103     size_type size( const ResKind & kind_r ) const;
104
105     template<class TRes>
106       size_type size() const
107       { return size( ResTraits<TRes>::kind ); }
108
109     const_iterator byKindBegin( const ResKind & kind_r ) const;
110
111     template<class TRes>
112       const_iterator byKindBegin() const
113       { return byKindBegin( ResTraits<TRes>::kind ); }
114
115
116     const_iterator byKindEnd( const ResKind & kind_r ) const;
117
118     template<class TRes>
119       const_iterator byKindEnd() const
120       { return byKindEnd( ResTraits<TRes>::kind ); }
121
122
123     Iterable<const_iterator> byKind( const ResKind & kind_r ) const
124       { return makeIterable( byKindBegin( kind_r ), byKindEnd( kind_r ) ); }
125
126     template<class TRes>
127       Iterable<const_iterator> byKind() const
128       { return makeIterable( byKindBegin<TRes>(), byKindEnd<TRes>() ); }
129
130     //@}
131
132  public:
133    /** \name Iterate through all Repositories that contribute ResObjects.
134    */
135    //@{
136    size_type knownRepositoriesSize() const;
137
138    repository_iterator knownRepositoriesBegin() const;
139
140    repository_iterator knownRepositoriesEnd() const;
141
142    Iterable<repository_iterator> knownRepositories() const
143    { return makeIterable( knownRepositoriesBegin(), knownRepositoriesEnd() ); }
144    //@}
145
146   public:
147     /** Test whether there is at least one ui::Selectable with
148      * an installed object.
149     */
150     bool hasInstalledObj( const ResKind & kind_r ) const
151     {
152       return(    make_begin<ui::selfilter::ByHasInstalledObj>( kind_r )
153               != make_end<ui::selfilter::ByHasInstalledObj>( kind_r ) );
154     }
155
156     template<class TRes>
157       bool hasInstalledObj() const
158       { return hasInstalledObj( ResTraits<TRes>::kind ); }
159
160   public:
161     /** \name Save and restore state per kind of resolvable.
162      * Simple version, no safety net. So don't restore or diff,
163      * if you didn't save before.
164      *
165      * Diff returns true, if current stat differs from the saved
166      * state.
167      *
168      * Use \ref scopedSaveState for exception safe scoped save/restore
169      */
170     //@{
171     void saveState() const;
172
173     void saveState( const ResKind & kind_r ) const;
174
175     template<class TRes>
176       void saveState() const
177       { return saveState( ResTraits<TRes>::kind ); }
178
179     void restoreState() const;
180
181     void restoreState( const ResKind & kind_r ) const;
182
183     template<class TRes>
184       void restoreState() const
185       { return restoreState( ResTraits<TRes>::kind ); }
186
187     bool diffState() const;
188
189     bool diffState( const ResKind & kind_r ) const;
190
191     template<class TRes>
192       bool diffState() const
193       { return diffState( ResTraits<TRes>::kind ); }
194
195     /**
196      * \class ScopedSaveState
197      * \brief Exception safe scoped save/restore state.
198      * Call \ref acceptState to prevent the class from restoring
199      * the remembered state.
200      * \ingroup g_RAII
201      */
202     struct ScopedSaveState;
203
204     ScopedSaveState scopedSaveState() const;
205
206     ScopedSaveState scopedSaveState( const ResKind & kind_r ) const;
207
208     template<class TRes>
209       ScopedSaveState && scopedSaveState() const
210       { return scopedSaveState( ResTraits<TRes>::kind ); }
211
212     //@}
213
214   private:
215     template<class TFilter>
216       filter_iterator<TFilter,const_iterator>
217       make_begin( TFilter filter_r, const ResKind & kind_r ) const
218       {
219         return make_filter_iterator( filter_r,
220                                      byKindBegin(kind_r),
221                                      byKindEnd(kind_r) );
222       }
223     template<class TFilter>
224       filter_iterator<TFilter,const_iterator>
225       make_begin( const ResKind & kind_r ) const
226       {
227         return make_begin( TFilter(), kind_r );
228       }
229
230
231     template<class TFilter>
232       filter_iterator<TFilter,const_iterator>
233       make_end( TFilter filter_r, const ResKind & kind_r ) const
234       {
235         return make_filter_iterator( filter_r,
236                                      byKindEnd(kind_r),
237                                      byKindEnd(kind_r) );
238       }
239     template<class TFilter>
240       filter_iterator<TFilter,const_iterator>
241       make_end( const ResKind & kind_r ) const
242       {
243         return make_end( TFilter(), kind_r );
244       }
245
246   private:
247     friend class pool::PoolImpl;
248     /** Ctor */
249     ResPoolProxy( ResPool pool_r, const pool::PoolImpl & poolImpl_r );
250     /** Pointer to implementation */
251     RW_pointer<Impl> _pimpl;
252   };
253   ///////////////////////////////////////////////////////////////////
254
255   /** \relates ResPoolProxy Stream output */
256   std::ostream & operator<<( std::ostream & str, const ResPoolProxy & obj );
257
258   /** \relates ResPoolProxy Verbose stream output */
259   std::ostream & dumpOn( std::ostream & str, const ResPoolProxy & obj );
260
261   ///////////////////////////////////////////////////////////////////
262
263   struct ResPoolProxy::ScopedSaveState
264   {
265     NON_COPYABLE_BUT_MOVE( ScopedSaveState );
266
267     ScopedSaveState( const ResPoolProxy & pool_r )
268     : _pimpl( new Impl( pool_r ) )
269     { _pimpl->saveState(); }
270
271     ScopedSaveState( const ResPoolProxy & pool_r, const ResKind & kind_r )
272     : _pimpl( new Impl( pool_r, kind_r ) )
273     { _pimpl->saveState(); }
274
275     ~ScopedSaveState()
276     { if ( _pimpl ) _pimpl->restoreState(); }
277
278     void acceptState()
279     { _pimpl.reset(); }
280
281   private:
282     struct Impl
283     {
284       Impl( const ResPoolProxy & pool_r )
285       : _pool( pool_r )
286       {}
287       Impl( const ResPoolProxy & pool_r, const ResKind & kind_r )
288       : _pool( pool_r ), _kind( new ResKind( kind_r ) )
289       {}
290       void saveState()
291       { if ( _kind ) _pool.saveState( *_kind ); else _pool.saveState(); }
292       void restoreState()
293       { if ( _kind ) _pool.restoreState( *_kind ); else _pool.restoreState(); }
294       ResPoolProxy _pool;
295       scoped_ptr<ResKind> _kind;
296
297     };
298     std::unique_ptr<Impl> _pimpl;
299   };
300
301   inline ResPoolProxy::ScopedSaveState ResPoolProxy::scopedSaveState() const
302   { return ScopedSaveState( *this ); }
303
304   inline ResPoolProxy::ScopedSaveState ResPoolProxy::scopedSaveState( const ResKind & kind_r ) const
305   { return ScopedSaveState( *this, kind_r ); }
306
307   /////////////////////////////////////////////////////////////////
308 } // namespace zypp
309 ///////////////////////////////////////////////////////////////////
310 #endif // ZYPP_RESPOOLPROXY_H