1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ResFilters.h
12 #ifndef ZYPP_RESFILTERS_H
13 #define ZYPP_RESFILTERS_H
15 #include <boost/function.hpp>
17 #include "zypp/base/Functional.h"
18 #include "zypp/Filter.h"
19 #include "zypp/Resolvable.h"
21 #include "zypp/PoolItem.h"
22 #include "zypp/Repository.h"
24 ///////////////////////////////////////////////////////////////////
26 { /////////////////////////////////////////////////////////////////
27 ///////////////////////////////////////////////////////////////////
29 { /////////////////////////////////////////////////////////////////
31 /** \defgroup RESFILTERS Filter functors operating on ResObjects.
34 * A simple filter is a function or functor matching the signature:
36 * bool simplefilter( ResObject::Ptr );
39 * \note It's not neccessary that your function or functor actually
40 * returns \c bool. Anything which is convertible into a \c bool
43 * Besides basic filter functors which actually evaluate the
44 * \c ResObject (e.g. \ref ByKind, \ref ByName) you may
45 * use \ref LOGICALFILTERS to build more complex filters.
48 * // some 'action' functor, printing and counting
50 * struct PrintAndCount
52 * PrintAndCount( unsigned & counter_r )
53 * : _counter( counter_r )
56 * bool operator()( ResObject::Ptr p ) const
67 * unsigned counter = 0;
69 * // print and count all resolvables
70 * store.forEach( PrintAndCount(counter) );
72 * // print and count all resolvables named "kernel"
74 * store.forEach( ByName("kernel"), PrintAndCount(counter) );
76 * // print and count all Packages named "kernel"
78 * store.forEach( chain( ByKind(ResKind::package),
80 * PrintAndCount(counter) );
82 * // print and count all Packages not named "kernel"
84 * store.forEach( chain( ByKind(ResKind::package),
85 * not_c(ByName("kernel")) ),
86 * PrintAndCount(counter) );
90 * store.forEach( chain( ByKind(ResKind::package),
91 * chain( not_c(ByName("kernel")),
92 * PrintAndCount(counter) ) ),
96 * As you can see in the last example there is no difference in using
97 * a filter or an action functor, as both have the same signature.
98 * A difference of course is the way forEach interprets the returned
101 * Consequently you can netgate and chain actions as well. Thus
102 * <tt>PrintAndCount(counter)</tt> could be
103 * <tt>chain(Print(),Count(counter))</tt>, if these functors are
106 * \note These functors are not limited to be used with ResStore::forEach.
107 * You can use them with std::algorithms as well.
109 * \note In case you already have functions or methods which do what you
110 * want, but thet don't perfectly match the required signature: Make yourself
111 * familiar with <tt>std::ptr_fun, mem_fun, bind1st, bind2nd and compose</tt>.
112 * They are sometimes quite helpfull.
114 * \c PrintAndCount is an example how a functor can return data collected
115 * during the query. You ca easily write a collector, that takes a
116 * <tt>std:list\<ResObject::Ptr\>\&</tt> and fills it with the matches
119 * But as a rule of thumb, a functor should be lightweight. If you
120 * want to get data out, pass references to variables in (and assert
121 * these variables live as long as the query lasts). Or use \ref FunctorRef.
123 * Internally all functors are passed by value. Thus it would not help
124 * you to create an instance of some collecting functor, and pass it
125 * to the query. The query will then fill a copy of your functor, you
126 * won't get the data back. (Well, you probabely could, by using
129 * Why functors and not plain functions?
131 * You can use plain functions if they don't have to deliver data back to
133 * The \c C-style approach is having functions that take a <tt>void * data</tt>
134 * as last argument. This \c data pointer is then passed arround and casted
137 * If you look at a functor, you'll see that it contains both, the function
138 * to call (it's <tt>operator()</tt> ) and the data you'd otherwise pass as
139 * <tt>void * data</tt>. That's nice and safe.
141 * \todo migrate to namespace filter and enhance to support Solvables as well.
144 ///////////////////////////////////////////////////////////////////
146 // Some ResObject attributes
148 ///////////////////////////////////////////////////////////////////
151 typedef std::unary_function<ResObject::constPtr, bool> ResObjectFilterFunctor;
152 typedef boost::function<bool ( ResObject::constPtr )> ResFilter;
156 inline filter::ByKind byKind()
157 { return filter::ByKind( ResTraits<_Res>::kind ); }
159 /** Select ResObject by name. */
160 struct ByName : public ResObjectFilterFunctor
162 ByName( const std::string & name_r )
166 bool operator()( ResObject::constPtr p ) const
168 return p->name() == _name;
174 /** Select ResObject by repository or repository alias. */
175 struct ByRepository : public ResObjectFilterFunctor
177 ByRepository( Repository repository_r )
178 : _alias( repository_r.info().alias() )
181 ByRepository( const std::string & alias_r )
185 bool operator()( ResObject::constPtr p ) const
187 return p->repoInfo().alias() == _alias;
193 /** Select ResObject by Edition using \a _Compare functor.
195 * Selects ResObject if <tt>_Compare( ResObject->edition(), _edition )</tt>
198 * // use the convenience funktions to create ByEdition:
200 * byEdition( someedition ); // selects ResObjects with edition == someedition
202 * byEdition( someedition, CompareByGT<Edition>() ) // edition > someedition
205 template<class _Compare = CompareByEQ<Edition> >
206 struct ByEdition : public ResObjectFilterFunctor
208 ByEdition( const Edition & edition_r,
210 : _edition( edition_r )
214 bool operator()( ResObject::constPtr p ) const
216 return _cmp( p->edition(), _edition );
224 template<class _Compare>
225 ByEdition<_Compare> byEdition( const Edition & edition_r, _Compare cmp_r )
226 { return ByEdition<_Compare>( edition_r, cmp_r ); }
229 template<class _Compare>
230 ByEdition<_Compare> byEdition( const Edition & edition_r )
231 { return byEdition( edition_r, _Compare() ); }
234 /** Select ResObject by Arch using \a _Compare functor.
236 * Selects ResObject if <tt>_Compare( ResObject->arch(), _arch )</tt>
239 * // use the convenience funktions to create ByArch:
241 * byArch( somearch ); // selects ResObjects with arch == somearch
243 * byArch( somearch, CompareByGT<Arch>() ) // arch > somearch
246 template<class _Compare = CompareByEQ<Arch> >
247 struct ByArch : public ResObjectFilterFunctor
249 ByArch( const Arch & arch_r,
255 bool operator()( ResObject::constPtr p ) const
257 return _cmp( p->arch(), _arch );
265 template<class _Compare>
266 ByArch<_Compare> byArch( const Arch & arch_r, _Compare cmp_r )
267 { return ByArch<_Compare>( arch_r, cmp_r ); }
270 template<class _Compare>
271 ByArch<_Compare> byArch( const Arch & arch_r )
272 { return byArch( arch_r, _Compare() ); }
275 ///////////////////////////////////////////////////////////////////
277 ///////////////////////////////////////////////////////////////////
279 // Some PoolItem attributes
281 ///////////////////////////////////////////////////////////////////
284 typedef std::unary_function<PoolItem, bool> PoolItemFilterFunctor;
286 /** Select PoolItem by installed. */
287 struct ByInstalled : public PoolItemFilterFunctor
289 bool operator()( const PoolItem & p ) const
291 return p.status().isInstalled();
296 /** Select PoolItem by uninstalled. */
297 struct ByUninstalled : public PoolItemFilterFunctor
299 bool operator()( const PoolItem & p ) const
301 return p.status().isUninstalled();
305 /** Select PoolItem by transact. */
306 struct ByTransact : public PoolItemFilterFunctor
308 bool operator()( const PoolItem & p ) const
310 return p.status().transacts();
314 /** Select PoolItem by lock. */
315 struct ByLock : public PoolItemFilterFunctor
317 bool operator()( const PoolItem & p ) const
319 return p.status().isLocked();
323 /** Select PoolItem by keep. */
324 struct ByKeep : public PoolItemFilterFunctor
326 bool operator()( const PoolItem & p ) const
328 return p.status().isKept();
332 /** PoolItem which is recommended. */
333 struct ByRecommended : public PoolItemFilterFunctor
335 bool operator()( const PoolItem & p ) const
337 return p.status().isRecommended();
341 /** PoolItem which is suggested. */
342 struct BySuggested : public PoolItemFilterFunctor
344 bool operator()( const PoolItem & p ) const
346 return p.status().isSuggested();
351 /////////////////////////////////////////////////////////////////
352 } // namespace resfilter
353 ///////////////////////////////////////////////////////////////////
354 /////////////////////////////////////////////////////////////////
356 ///////////////////////////////////////////////////////////////////
357 #endif // ZYPP_RESFILTERS_H