- ResPoolProxy save/restore
[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   /**
32    * \todo Make it a _Ref.
33   */
34   class ResPoolProxy
35   {
36     friend std::ostream & operator<<( std::ostream & str, const ResPoolProxy & obj );
37
38     typedef std::set<ui::Selectable::Ptr>             SelectableIndex;
39     typedef std::map<ResObject::Kind,SelectableIndex> SelectablePool;
40
41   public:
42     /** Implementation  */
43     class Impl;
44
45     typedef SelectableIndex::iterator       iterator;
46     typedef SelectableIndex::const_iterator const_iterator;
47     typedef SelectableIndex::size_type      size_type;
48
49   public:
50     /** Default ctor: no pool */
51     ResPoolProxy();
52     /** Ctor */
53     ResPoolProxy( ResPool_Ref pool_r );
54     /** Dtor */
55     ~ResPoolProxy();
56
57   public:
58
59     /** True if there are items of a certain kind. */
60     bool empty( const ResObject::Kind & kind_r ) const;
61
62     template<class _Res>
63       bool empty() const
64       { return empty( ResTraits<_Res>::kind ); }
65
66     /** Number of Items of a certain kind.  */
67     size_type size( const ResObject::Kind & kind_r ) const;
68
69     template<class _Res>
70       size_type size() const
71       { return size( ResTraits<_Res>::kind ); }
72
73     /** \name Iterate through all Selectables of a certain kind. */
74     //@{
75     const_iterator byKindBegin( const ResObject::Kind & kind_r ) const;
76
77     template<class _Res>
78       const_iterator byKindBegin() const
79       { return byKindBegin( ResTraits<_Res>::kind ); }
80
81
82     const_iterator byKindEnd( const ResObject::Kind & kind_r ) const;
83
84     template<class _Res>
85       const_iterator byKindEnd() const
86       { return byKindEnd( ResTraits<_Res>::kind ); }
87     //@}
88
89   public:
90     /** Test whether there is at least one ui::Selectable with
91      * an installed object.
92     */
93     bool hasInstalledObj( const ResObject::Kind & kind_r ) const
94     {
95       return(    make_begin<ui::selfilter::ByHasInstalledObj>( kind_r )
96               != make_end<ui::selfilter::ByHasInstalledObj>( kind_r ) );
97     }
98
99     template<class _Res>
100       bool hasInstalledObj() const
101       { return hasInstalledObj( ResTraits<_Res>::kind ); }
102
103   public:
104     /** \name Save and restore state per kind of resolvable.
105      * Simple version, no savety net.
106     */
107     //@{
108     void saveState( const ResObject::Kind & kind_r ) const;
109
110     template<class _Res>
111       void saveState() const
112       { return saveState( ResTraits<_Res>::kind ); }
113
114     void restoreState( const ResObject::Kind & kind_r ) const;
115
116     template<class _Res>
117       void restoreState() const
118       { return restoreState( ResTraits<_Res>::kind ); }
119     //@}
120
121   private:
122     template<class _Filter>
123       filter_iterator<_Filter,const_iterator>
124       make_begin( _Filter filter_r, const ResObject::Kind & kind_r ) const
125       {
126         return make_filter_iterator( filter_r,
127                                      byKindBegin(kind_r),
128                                      byKindEnd(kind_r) );
129       }
130     template<class _Filter>
131       filter_iterator<_Filter,const_iterator>
132       make_begin( const ResObject::Kind & kind_r ) const
133       {
134         return make_begin( _Filter(), kind_r );
135       }
136
137
138     template<class _Filter>
139       filter_iterator<_Filter,const_iterator>
140       make_end( _Filter filter_r, const ResObject::Kind & kind_r ) const
141       {
142         return make_filter_iterator( filter_r,
143                                      byKindEnd(kind_r),
144                                      byKindEnd(kind_r) );
145       }
146     template<class _Filter>
147       filter_iterator<_Filter,const_iterator>
148       make_end( const ResObject::Kind & kind_r ) const
149       {
150         return make_end( _Filter(), kind_r );
151       }
152
153
154     /** Pointer to implementation */
155     RW_pointer<Impl> _pimpl;
156   };
157   ///////////////////////////////////////////////////////////////////
158
159   /** \relates ResPoolProxy Stream output */
160   std::ostream & operator<<( std::ostream & str, const ResPoolProxy & obj );
161
162   /////////////////////////////////////////////////////////////////
163 } // namespace zypp
164 ///////////////////////////////////////////////////////////////////
165 #endif // ZYPP_RESPOOLPROXY_H