fixup Fix to build with libxml 2.12.x (fixes #505)
[platform/upstream/libzypp.git] / zypp / Filter.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/Filter.h
10  *
11 */
12 #ifndef ZYPP_FILTER_H
13 #define ZYPP_FILTER_H
14
15 #include <iosfwd>
16
17 #include "zypp/base/Functional.h"
18 #include "zypp/base/Function.h"
19 // #include "zypp/ResFilters.h"  included at the end!
20 #include "zypp/sat/Pool.h"
21 #include "zypp/PoolItem.h"
22
23 ///////////////////////////////////////////////////////////////////
24 namespace zypp
25 { /////////////////////////////////////////////////////////////////
26   ///////////////////////////////////////////////////////////////////
27   namespace filter
28   { /////////////////////////////////////////////////////////////////
29     /** \defgroup POOLFILTER Collection solvable filter functors.
30      *
31      * All functors should be able to process \ref Solvable as well
32      * as \ref PoolItem.
33      *
34      * \code
35      *   // The same filter...
36      *   filter::ByLocaleSupport f( Locale("de") );
37      *
38      *   // ...can be used to iterate the sat::Pool...
39      *   sat::Pool satpool( sat::Pool::instance() );
40      *   for_( it, satpool.filterBegin(f), satpool.filterEnd(f) )
41      *   {
42      *     MIL << *it << endl; // prints sat::Solvable
43      *   }
44      *
45      *   // ...as well as the ResPool.
46      *   ResPool   pool   ( ResPool::instance() );
47      *   for_( it, pool.filterBegin(f), pool.filterEnd(f) )
48      *   {
49      *     MIL << *it << endl; // prints PoolItem
50      *   }
51      * \endcode
52      * \ingroup g_Functor
53      */
54     //@{
55
56     ///////////////////////////////////////////////////////////////////
57     //
58     //  CLASS NAME : ByLocaleSupport
59     //
60     /** Filter solvables according to their locale support.
61     */
62     class ByLocaleSupport
63     {
64       private:
65         typedef bool (sat::Solvable::*LS1) (const Locale &) const;
66         typedef bool (sat::Solvable::*LS2) (const LocaleSet &) const;
67
68       public:
69         /** Solvables with locale support. */
70         ByLocaleSupport()
71         : _sel( boost::mem_fun_ref( &sat::Solvable::supportsLocales ) )
72         {}
73
74         /** Solvables supporting \c locale_r. */
75         explicit ByLocaleSupport( const Locale & locale_r )
76         : _sel( boost::bind( boost::mem_fun_ref( (LS1)&sat::Solvable::supportsLocale ), _1, locale_r ) )
77         {}
78
79         /** Solvables supporting at least one locale in \c locales_r. */
80         explicit ByLocaleSupport( const LocaleSet & locales_r )
81         : _sel( boost::bind( boost::mem_fun_ref( (LS2)&sat::Solvable::supportsLocale ), _1, locales_r ) )
82         {}
83
84       public:
85         /** Filter on \ref Solvable. */
86         bool operator()( const sat::Solvable & solv_r ) const
87         { return _sel && _sel( solv_r ); }
88
89         /** Filter fitting PoolItem/ResObject. */
90         template<class TSolv>
91         bool operator()( const TSolv & solv_r ) const
92         { return operator()( solv_r.satSolvable() ); }
93
94       private:
95         function<bool(const sat::Solvable &)> _sel;
96     };
97     ///////////////////////////////////////////////////////////////////
98
99     ///////////////////////////////////////////////////////////////////
100     //
101     //  CLASS NAME : ByKind
102     //
103     /** Filter solvables according to their kind.
104     */
105     class ByKind
106     {
107       public:
108         ByKind()
109         {}
110
111         ByKind( const ResKind & kind_r )
112         : _kind( kind_r )
113         {}
114
115       public:
116         /** Filter on \ref Solvable. */
117         bool operator()( const sat::Solvable & solv_r ) const
118         { return solv_r.isKind( _kind ); }
119
120         /** Filter fitting PoolItem/ResObject. */
121         template<class TSolv>
122         bool operator()( const TSolv & solv_r ) const
123         { return operator()( solv_r.satSolvable() ); }
124
125       private:
126         ResKind _kind;
127     };
128
129     /** \relates ByKind templated convenience ctor. */
130     template<class TRes>
131     inline ByKind byKind()
132     { return ByKind( ResTraits<TRes>::kind ); }
133     ///////////////////////////////////////////////////////////////////
134
135     ///////////////////////////////////////////////////////////////////
136     //
137     //  CLASS NAME : ByStatus
138     //
139     /** Filter solvables according to their status.
140     */
141     class ByStatus
142     {
143       public:
144         typedef bool (ResStatus::*Predicate)() const;
145
146       public:
147         ByStatus( Predicate pred_r = 0 )
148         : _pred( pred_r )
149         {}
150
151       public:
152         /** Filter on \ref PoolItem. */
153         bool operator()( const PoolItem & pi_r ) const
154         { return _pred && (pi_r.status().*_pred)(); }
155
156         /** Filter fitting sat::Solvable/ResObject. */
157         template<class TSolv>
158         bool operator()( const TSolv & solv_r ) const
159         { return operator()( PoolItem(solv_r) ); }
160
161       private:
162         Predicate _pred;
163     };
164     ///////////////////////////////////////////////////////////////////
165
166
167     ///////////////////////////////////////////////////////////////////
168     //
169     //  CLASS NAME : SameItemAs
170     //
171     /** Filter items with at least same NVRA, vendor.
172      * This is usually used to find available packages
173      * that matche an insytalled one.
174     */
175     class SameItemAs
176     {
177       public:
178         SameItemAs( const sat::Solvable & solv_r )
179         : _item( solv_r )
180         {}
181
182         /** Fitting PoolItem/ResObject. */
183         template<class TSolv>
184         SameItemAs( const TSolv & solv_r )
185         : _item( solv_r.satSolvable() )
186         {}
187
188       public:
189         /** Filter on \ref Solvable. */
190         bool operator()( const sat::Solvable & solv_r ) const
191         {
192           return solv_r.name()    == _item.name()
193               && solv_r.edition() == _item.edition()
194               && solv_r.arch()    == _item.arch()
195               && solv_r.vendor()  == _item.vendor();
196         }
197
198         /** Filter fitting PoolItem/ResObject. */
199         template<class TSolv>
200         bool operator()( const TSolv & solv_r ) const
201         { return operator()( solv_r.satSolvable() ); }
202
203       private:
204         sat::Solvable _item;
205     };
206     ///////////////////////////////////////////////////////////////////
207     //@}
208
209   /////////////////////////////////////////////////////////////////
210   } // namespace filter
211   ///////////////////////////////////////////////////////////////////
212   /////////////////////////////////////////////////////////////////
213 } // namespace zypp
214 ///////////////////////////////////////////////////////////////////
215
216 #include "zypp/ResFilters.h"
217
218 #endif // ZYPP_FILTER_H