#ifndef ZYPP_RESFILTERS_H
#define ZYPP_RESFILTERS_H
-#include <functional>
+#include <boost/function.hpp>
-#include "zypp/base/FunctorRef.h"
-#include "zypp/base/LogicalFilters.h"
+#include "zypp/base/Functional.h"
+#include "zypp/Filter.h"
#include "zypp/Resolvable.h"
+#include "zypp/PoolItem.h"
+#include "zypp/Repository.h"
+
///////////////////////////////////////////////////////////////////
namespace zypp
{ /////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
- namespace functor
+ namespace resfilter
{ /////////////////////////////////////////////////////////////////
- /** \defgroup RESFILTERS Filter functors operating on Resolvables.
+
+ /** \defgroup RESFILTERS Filter functors operating on ResObjects.
* \ingroup g_Functor
*
* A simple filter is a function or functor matching the signature:
* \code
- * bool simplefilter( Resolvable::Ptr );
+ * bool simplefilter( ResObject::Ptr );
* \endcode
*
* \note It's not neccessary that your function or functor actually
* will do;
*
* Besides basic filter functors which actually evaluate the
- * \c Resolvable (e.g. \ref ByKind, \ref ByName) you may
+ * \c ResObject (e.g. \ref ByKind, \ref ByName) you may
* use \ref LOGICALFILTERS to build more complex filters.
*
* \code
* // some 'action' functor, printing and counting
- * // Resolvables.
+ * // ResObjects.
* struct PrintAndCount
* {
* PrintAndCount( unsigned & counter_r )
* : _counter( counter_r )
* {}
*
- * bool operator()( Resolvable::Ptr p ) const
+ * bool operator()( ResObject::Ptr p ) const
* {
* DBG << *p << endl;
* ++_counter;
* return true;
* }
*
- * unsigned & _counter;
+ * unsigned _counter;
* };
*
* ResStore store;
*
* // print and count all Packages named "kernel"
* counter = 0;
- * store.forEach( chain( ByKind(ResTraits<Package>::kind),
+ * store.forEach( chain( ByKind(ResKind::package),
* ByName("kernel") ),
* PrintAndCount(counter) );
*
* // print and count all Packages not named "kernel"
* counter = 0;
- * store.forEach( chain( ByKind(ResTraits<Package>::kind),
+ * store.forEach( chain( ByKind(ResKind::package),
* not_c(ByName("kernel")) ),
* PrintAndCount(counter) );
*
* // same as above ;)
* counter = 0;
- * store.forEach( chain( ByKind(ResTraits<Package>::kind),
+ * store.forEach( chain( ByKind(ResKind::package),
* chain( not_c(ByName("kernel")),
* PrintAndCount(counter) ) ),
* true_c() );
*
* \c PrintAndCount is an example how a functor can return data collected
* during the query. You ca easily write a collector, that takes a
- * <tt>std:list\<Resolvable::Ptr\>\&</tt> and fills it with the matches
+ * <tt>std:list\<ResObject::Ptr\>\&</tt> and fills it with the matches
* found.
*
* But as a rule of thumb, a functor should be lightweight. If you
* If you look at a functor, you'll see that it contains both, the function
* to call (it's <tt>operator()</tt> ) and the data you'd otherwise pass as
* <tt>void * data</tt>. That's nice and safe.
+ *
+ * \todo migrate to namespace filter and enhance to support Solvables as well.
*/
//@{
///////////////////////////////////////////////////////////////////
//
- // Now some Resolvable attributes
+ // Some ResObject attributes
//
///////////////////////////////////////////////////////////////////
- struct ByKind
+ /** */
+ typedef std::unary_function<ResObject::constPtr, bool> ResObjectFilterFunctor;
+ typedef boost::function<bool ( ResObject::constPtr )> ResFilter;
+
+ /** */
+ template<class TRes>
+ inline filter::ByKind byKind()
+ { return filter::ByKind( ResTraits<TRes>::kind ); }
+
+ /** Select ResObject by name. */
+ struct ByName : public ResObjectFilterFunctor
{
- ByKind( const Resolvable::Kind & kind_r )
- : _kind( kind_r )
+ ByName()
{}
- bool operator()( Resolvable::constPtr p ) const
+ ByName( const std::string & name_r )
+ : _name( name_r )
+ {}
+
+ bool operator()( ResObject::constPtr p ) const
{
- return p->kind() == _kind;
+ return p->name() == _name;
}
- Resolvable::Kind _kind;
+ std::string _name;
};
- template<class _Res>
- ByKind byKind()
- { return ByKind( ResTraits<_Res>::kind ); }
-
- struct ByName
+ /** Select ResObject by repository or repository alias. */
+ struct ByRepository : public ResObjectFilterFunctor
{
- ByName( const std::string & name_r )
- : _name( name_r )
+ ByRepository( Repository repository_r )
+ : _alias( repository_r.info().alias() )
{}
- bool operator()( Resolvable::constPtr p ) const
+ ByRepository( const std::string & alias_r )
+ : _alias( alias_r )
+ {}
+
+ bool operator()( ResObject::constPtr p ) const
{
- return p->name() == _name;
+ return p->repoInfo().alias() == _alias;
}
- std::string _name;
+ std::string _alias;
};
+ /** Select ResObject by Edition using \a TCompare functor.
+ *
+ * Selects ResObject if <tt>TCompare( ResObject->edition(), _edition )</tt>
+ * is \c true.
+ * \code
+ * // use the convenience funktions to create ByEdition:
+ *
+ * byEdition( someedition ); // selects ResObjects with edition == someedition
+ *
+ * byEdition( someedition, CompareByGT<Edition>() ) // edition > someedition
+ * \endcode
+ */
+ template<class TCompare = CompareByEQ<Edition> >
+ struct ByEdition : public ResObjectFilterFunctor
+ {
+ ByEdition( const Edition & edition_r, TCompare cmp_r )
+ : _edition( edition_r )
+ , _cmp( cmp_r )
+ {}
+
+ bool operator()( ResObject::constPtr p ) const
+ {
+ return _cmp( p->edition(), _edition );
+ }
+
+ Edition _edition;
+ TCompare _cmp;
+ };
+
+ /** */
+ template<class TCompare>
+ ByEdition<TCompare> byEdition( const Edition & edition_r, TCompare cmp_r )
+ { return ByEdition<TCompare>( edition_r, cmp_r ); }
+
+ /** */
+ template<class TCompare>
+ ByEdition<TCompare> byEdition( const Edition & edition_r )
+ { return byEdition( edition_r, TCompare() ); }
+
+
+ /** Select ResObject by Arch using \a TCompare functor.
+ *
+ * Selects ResObject if <tt>TCompare( ResObject->arch(), _arch )</tt>
+ * is \c true.
+ * \code
+ * // use the convenience funktions to create ByArch:
+ *
+ * byArch( somearch ); // selects ResObjects with arch == somearch
+ *
+ * byArch( somearch, CompareByGT<Arch>() ) // arch > somearch
+ * \endcode
+ */
+ template<class TCompare = CompareByEQ<Arch> >
+ struct ByArch : public ResObjectFilterFunctor
+ {
+ ByArch( const Arch & arch_r, TCompare cmp_r )
+ : _arch( arch_r )
+ , _cmp( cmp_r )
+ {}
+
+ bool operator()( ResObject::constPtr p ) const
+ {
+ return _cmp( p->arch(), _arch );
+ }
+
+ Arch _arch;
+ TCompare _cmp;
+ };
+
+ /** */
+ template<class TCompare>
+ ByArch<TCompare> byArch( const Arch & arch_r, TCompare cmp_r )
+ { return ByArch<TCompare>( arch_r, cmp_r ); }
+
+ /** */
+ template<class TCompare>
+ ByArch<TCompare> byArch( const Arch & arch_r )
+ { return byArch( arch_r, TCompare() ); }
+
+
+ ///////////////////////////////////////////////////////////////////
+
+ ///////////////////////////////////////////////////////////////////
+ //
+ // Some PoolItem attributes
+ //
///////////////////////////////////////////////////////////////////
+ /** */
+ typedef std::unary_function<PoolItem, bool> PoolItemFilterFunctor;
+
+ /** Select PoolItem by installed. */
+ struct ByInstalled : public PoolItemFilterFunctor
+ {
+ bool operator()( const PoolItem & p ) const
+ {
+ return p.status().isInstalled();
+ }
+ };
+
+ /** Select PoolItem by uninstalled. */
+ struct ByUninstalled : public PoolItemFilterFunctor
+ {
+ bool operator()( const PoolItem & p ) const
+ {
+ return p.status().isUninstalled();
+ }
+ };
+
+ /** Select PoolItem by transact. */
+ struct ByTransact : public PoolItemFilterFunctor
+ {
+ bool operator()( const PoolItem & p ) const
+ {
+ return p.status().transacts();
+ }
+ };
+
+ /** Select PoolItem by lock. */
+ struct ByLock : public PoolItemFilterFunctor
+ {
+ bool operator()( const PoolItem & p ) const
+ {
+ return p.status().isLocked();
+ }
+ };
+
+ /** Select PoolItem by keep. */
+ struct ByKeep : public PoolItemFilterFunctor
+ {
+ bool operator()( const PoolItem & p ) const
+ {
+ return p.status().isKept();
+ }
+ };
+
+ /** PoolItem which is recommended. */
+ struct ByRecommended : public PoolItemFilterFunctor
+ {
+ bool operator()( const PoolItem & p ) const
+ {
+ return p.status().isRecommended();
+ }
+ };
+
+ /** PoolItem which is suggested. */
+ struct BySuggested : public PoolItemFilterFunctor
+ {
+ bool operator()( const PoolItem & p ) const
+ {
+ return p.status().isSuggested();
+ }
+ };
+
//@}
/////////////////////////////////////////////////////////////////
- } // namespace functor
+ } // namespace resfilter
///////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
} // namespace zypp