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/Resolvable.h"
20 #include "zypp/PoolItem.h"
21 #include "zypp/Repository.h"
23 ///////////////////////////////////////////////////////////////////
25 { /////////////////////////////////////////////////////////////////
26 ///////////////////////////////////////////////////////////////////
28 { /////////////////////////////////////////////////////////////////
30 /** \defgroup RESFILTERS Filter functors operating on ResObjects.
33 * A simple filter is a function or functor matching the signature:
35 * bool simplefilter( ResObject::Ptr );
38 * \note It's not neccessary that your function or functor actually
39 * returns \c bool. Anything which is convertible into a \c bool
42 * Besides basic filter functors which actually evaluate the
43 * \c ResObject (e.g. \ref ByKind, \ref ByName) you may
44 * use \ref LOGICALFILTERS to build more complex filters.
47 * // some 'action' functor, printing and counting
49 * struct PrintAndCount
51 * PrintAndCount( unsigned & counter_r )
52 * : _counter( counter_r )
55 * bool operator()( ResObject::Ptr p ) const
66 * unsigned counter = 0;
68 * // print and count all resolvables
69 * store.forEach( PrintAndCount(counter) );
71 * // print and count all resolvables named "kernel"
73 * store.forEach( ByName("kernel"), PrintAndCount(counter) );
75 * // print and count all Packages named "kernel"
77 * store.forEach( chain( ByKind(ResTraits<Package>::kind),
79 * PrintAndCount(counter) );
81 * // print and count all Packages not named "kernel"
83 * store.forEach( chain( ByKind(ResTraits<Package>::kind),
84 * not_c(ByName("kernel")) ),
85 * PrintAndCount(counter) );
89 * store.forEach( chain( ByKind(ResTraits<Package>::kind),
90 * chain( not_c(ByName("kernel")),
91 * PrintAndCount(counter) ) ),
95 * As you can see in the last example there is no difference in using
96 * a filter or an action functor, as both have the same signature.
97 * A difference of course is the way forEach interprets the returned
100 * Consequently you can netgate and chain actions as well. Thus
101 * <tt>PrintAndCount(counter)</tt> could be
102 * <tt>chain(Print(),Count(counter))</tt>, if these functors are
105 * \note These functors are not limited to be used with ResStore::forEach.
106 * You can use them with std::algorithms as well.
108 * \note In case you already have functions or methods which do what you
109 * want, but thet don't perfectly match the required signature: Make yourself
110 * familiar with <tt>std::ptr_fun, mem_fun, bind1st, bind2nd and compose</tt>.
111 * They are sometimes quite helpfull.
113 * \c PrintAndCount is an example how a functor can return data collected
114 * during the query. You ca easily write a collector, that takes a
115 * <tt>std:list\<ResObject::Ptr\>\&</tt> and fills it with the matches
118 * But as a rule of thumb, a functor should be lightweight. If you
119 * want to get data out, pass references to variables in (and assert
120 * these variables live as long as the query lasts). Or use \ref FunctorRef.
122 * Internally all functors are passed by value. Thus it would not help
123 * you to create an instance of some collecting functor, and pass it
124 * to the query. The query will then fill a copy of your functor, you
125 * won't get the data back. (Well, you probabely could, by using
128 * Why functors and not plain functions?
130 * You can use plain functions if they don't have to deliver data back to
132 * The \c C-style approach is having functions that take a <tt>void * data</tt>
133 * as last argument. This \c data pointer is then passed arround and casted
136 * If you look at a functor, you'll see that it contains both, the function
137 * to call (it's <tt>operator()</tt> ) and the data you'd otherwise pass as
138 * <tt>void * data</tt>. That's nice and safe.
141 ///////////////////////////////////////////////////////////////////
143 // Some ResObject attributes
145 ///////////////////////////////////////////////////////////////////
148 typedef std::unary_function<ResObject::constPtr, bool> ResObjectFilterFunctor;
149 typedef boost::function<bool ( ResObject::constPtr )> ResFilter;
151 /** Select ResObject by kind. */
152 struct ByKind : public ResObjectFilterFunctor
154 ByKind( const ResObject::Kind & kind_r )
158 bool operator()( ResObject::constPtr p ) const
160 return p->kind() == _kind;
163 ResObject::Kind _kind;
168 inline ByKind byKind()
169 { return ByKind( ResTraits<_Res>::kind ); }
171 /** Select ResObject by name. */
172 struct ByName : public ResObjectFilterFunctor
174 ByName( const std::string & name_r )
178 bool operator()( ResObject::constPtr p ) const
180 return p->name() == _name;
186 /** Select ResObject by repository or repository alias. */
187 struct ByRepository : public ResObjectFilterFunctor
189 ByRepository( Repository repository_r )
190 : _alias( repository_r.info().alias() )
193 ByRepository( const std::string & alias_r )
197 bool operator()( ResObject::constPtr p ) const
199 return p->repoInfo().alias() == _alias;
205 /** Select ResObject by Edition using \a _Compare functor.
207 * Selects ResObject if <tt>_Compare( ResObject->edition(), _edition )</tt>
210 * // use the convenience funktions to create ByEdition:
212 * byEdition( someedition ); // selects ResObjects with edition == someedition
214 * byEdition( someedition, CompareByGT<Edition>() ) // edition > someedition
217 template<class _Compare = CompareByEQ<Edition> >
218 struct ByEdition : public ResObjectFilterFunctor
220 ByEdition( const Edition & edition_r,
222 : _edition( edition_r )
226 bool operator()( ResObject::constPtr p ) const
228 return _cmp( p->edition(), _edition );
236 template<class _Compare>
237 ByEdition<_Compare> byEdition( const Edition & edition_r, _Compare cmp_r )
238 { return ByEdition<_Compare>( edition_r, cmp_r ); }
241 template<class _Compare>
242 ByEdition<_Compare> byEdition( const Edition & edition_r )
243 { return byEdition( edition_r, _Compare() ); }
246 /** Select ResObject by Arch using \a _Compare functor.
248 * Selects ResObject if <tt>_Compare( ResObject->arch(), _arch )</tt>
251 * // use the convenience funktions to create ByArch:
253 * byArch( somearch ); // selects ResObjects with arch == somearch
255 * byArch( somearch, CompareByGT<Arch>() ) // arch > somearch
258 template<class _Compare = CompareByEQ<Arch> >
259 struct ByArch : public ResObjectFilterFunctor
261 ByArch( const Arch & arch_r,
267 bool operator()( ResObject::constPtr p ) const
269 return _cmp( p->arch(), _arch );
277 template<class _Compare>
278 ByArch<_Compare> byArch( const Arch & arch_r, _Compare cmp_r )
279 { return ByArch<_Compare>( arch_r, cmp_r ); }
282 template<class _Compare>
283 ByArch<_Compare> byArch( const Arch & arch_r )
284 { return byArch( arch_r, _Compare() ); }
287 ///////////////////////////////////////////////////////////////////
289 ///////////////////////////////////////////////////////////////////
291 // Some PoolItem attributes
293 ///////////////////////////////////////////////////////////////////
296 typedef std::unary_function<PoolItem, bool> PoolItemFilterFunctor;
298 /** Select PoolItem by installed. */
299 struct ByInstalled : public PoolItemFilterFunctor
301 bool operator()( const PoolItem & p ) const
303 return p.status().isInstalled();
308 /** Select PoolItem by uninstalled. */
309 struct ByUninstalled : public PoolItemFilterFunctor
311 bool operator()( const PoolItem & p ) const
313 return p.status().isUninstalled();
317 /** Select PoolItem by transact. */
318 struct ByTransact : public PoolItemFilterFunctor
320 bool operator()( const PoolItem & p ) const
322 return p.status().transacts();
326 /** Select PoolItem by lock. */
327 struct ByLock : public PoolItemFilterFunctor
329 bool operator()( const PoolItem & p ) const
331 return p.status().isLocked();
335 /** Select PoolItem by keep. */
336 struct ByKeep : public PoolItemFilterFunctor
338 bool operator()( const PoolItem & p ) const
340 return p.status().isKept();
345 /////////////////////////////////////////////////////////////////
346 } // namespace resfilter
347 ///////////////////////////////////////////////////////////////////
348 /////////////////////////////////////////////////////////////////
350 ///////////////////////////////////////////////////////////////////
351 #endif // ZYPP_RESFILTERS_H