Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / ResFilters.h
index a48bd07..1b4ffd0 100644 (file)
 #ifndef ZYPP_RESFILTERS_H
 #define ZYPP_RESFILTERS_H
 
-#include <iosfwd>
+#include <boost/function.hpp>
 
+#include "zypp/base/Functional.h"
+#include "zypp/Filter.h"
 #include "zypp/Resolvable.h"
 
+#include "zypp/PoolItem.h"
+#include "zypp/Repository.h"
+
 ///////////////////////////////////////////////////////////////////
 namespace zypp
 { /////////////////////////////////////////////////////////////////
   ///////////////////////////////////////////////////////////////////
   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
@@ -34,46 +41,28 @@ namespace zypp
      * will do;
      *
      * Besides basic filter functors which actually evaluate the
-     * \c Resolvable (e.g. \ref ByKind, \ref ByName), there are some
-     * special functors to build more complex queries:
-     *
-     * \li \ref True and \ref False. No supprise, they always return
-     *     \c true or \c false.
-     * \li \ref Not\<_Condition\>. _Condition is a filter functor, and
-     *     it's result is inverted.
-     * \li \ref Chain\<_ACondition,_BCondition\>. \c _ACondition and \c _BCondition
-     *     are filter functors, and Chain evaluates
-     *     <tt>_ACondition && _BCondition</tt>
-     *
-     * As it's no fun to get and write the correct template arguments,
-     * convenience functions creating the correct functor are provided.
-     *
-     * \li \c true_c and \c false_c. (provided just to match the schema)
-     * \li \c not_c. Takes a functor as argument and returns the appropriate
-     *     \ref Not functor.
-     * \li \c chain. Takes two functors and returns the appropriate
-     *     \ref Cain functor.
+     * \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;
      * unsigned counter = 0;
      *
@@ -86,19 +75,19 @@ namespace zypp
      *
      * // 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() );
@@ -124,12 +113,12 @@ namespace zypp
      *
      * \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
      * want to get data out, pass references to variables in (and assert
-     * these variables live as long as the quiery lasts).
+     * these variables live as long as the query lasts). Or use \ref FunctorRef.
      *
      * Internally all functors are passed by value. Thus it would not help
      * you to create an instance of some collecting functor, and pass it
@@ -148,113 +137,215 @@ namespace zypp
      * 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.
     */
     //@{
     ///////////////////////////////////////////////////////////////////
     //
-    // Predefined filters
+    // Some ResObject attributes
     //
     ///////////////////////////////////////////////////////////////////
 
-    struct True
+    /** */
+    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
     {
-      bool operator()( Resolvable::Ptr ) const
+      ByName()
+      {}
+
+      ByName( const std::string & name_r )
+      : _name( name_r )
+      {}
+
+      bool operator()( ResObject::constPtr p ) const
       {
-        return true;
+        return p->name() == _name;
       }
+
+      std::string _name;
     };
 
-    True true_c()
-    { return True(); }
+    /** Select ResObject by repository or repository alias. */
+    struct ByRepository : public ResObjectFilterFunctor
+    {
+      ByRepository( Repository repository_r )
+      : _alias( repository_r.info().alias() )
+      {}
 
-    ///////////////////////////////////////////////////////////////////
+      ByRepository( const std::string & alias_r )
+      : _alias( alias_r )
+      {}
 
-    struct False
-    {
-      bool operator()( Resolvable::Ptr ) const
+      bool operator()( ResObject::constPtr p ) const
       {
-        return false;
+        return p->repoInfo().alias() == _alias;
       }
-    };
-
-    False false_c()
-    { return False(); }
 
-    ///////////////////////////////////////////////////////////////////
+      std::string _alias;
+    };
 
-    template<class _Condition>
-      struct Not
+    /** 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
       {
-        Not( _Condition cond_r )
-        : _cond( cond_r )
+        ByEdition( const Edition & edition_r, TCompare cmp_r )
+        : _edition( edition_r )
+        , _cmp( cmp_r )
         {}
-        bool operator()( Resolvable::Ptr p ) const
+
+        bool operator()( ResObject::constPtr p ) const
         {
-          return ! _cond( p );
+          return _cmp( p->edition(), _edition );
         }
-        _Condition _cond;
+
+        Edition  _edition;
+        TCompare _cmp;
       };
 
-    template<class _Condition>
-      Not<_Condition> not_c( _Condition cond_r )
-      {
-        return Not<_Condition>( cond_r );
-      }
+    /** */
+    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() ); }
 
-    template<class _ACondition, class _BCondition>
-      struct Chain
+
+    /** 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
       {
-        Chain( _ACondition conda_r, _BCondition condb_r )
-        : _conda( conda_r )
-        , _condb( condb_r )
+        ByArch( const Arch & arch_r, TCompare cmp_r )
+        : _arch( arch_r )
+        , _cmp( cmp_r )
         {}
-        bool operator()( Resolvable::Ptr p ) const
+
+        bool operator()( ResObject::constPtr p ) const
         {
-          return _conda( p ) && _condb( p );
+          return _cmp( p->arch(), _arch );
         }
-        _ACondition _conda;
-        _BCondition _condb;
+
+        Arch  _arch;
+        TCompare _cmp;
       };
 
-    template<class _ACondition, class _BCondition>
-      Chain<_ACondition, _BCondition> chain( _ACondition conda_r, _BCondition condb_r )
-      {
-        return Chain<_ACondition, _BCondition>( conda_r, condb_r );
-      }
+    /** */
+    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() ); }
+
+
+    ///////////////////////////////////////////////////////////////////
 
     ///////////////////////////////////////////////////////////////////
     //
-    // Now some Resolvable attributes
+    // Some PoolItem attributes
     //
     ///////////////////////////////////////////////////////////////////
 
-    struct ByKind
+    /** */
+    typedef std::unary_function<PoolItem, bool> PoolItemFilterFunctor;
+
+    /** Select PoolItem by installed. */
+    struct ByInstalled : public PoolItemFilterFunctor
     {
-      ByKind( const Resolvable::Kind & kind_r )
-      : _kind( kind_r )
-      {}
-      bool operator()( Resolvable::Ptr p ) const
+      bool operator()( const PoolItem & p ) const
       {
-        return p->kind() == _kind;
+       return p.status().isInstalled();
       }
-      Resolvable::Kind _kind;
     };
 
-    struct ByName
+    /** Select PoolItem by uninstalled. */
+    struct ByUninstalled : public PoolItemFilterFunctor
     {
-      ByName( const std::string & name_r )
-      : _name( name_r )
-      {}
-      bool operator()( Resolvable::Ptr p ) const
+      bool operator()( const PoolItem & p ) const
       {
-        return p->name() == _name;
+       return p.status().isUninstalled();
       }
-      std::string _name;
     };
 
-    ///////////////////////////////////////////////////////////////////
+    /** 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();
+      }
+    };
 
     //@}
     /////////////////////////////////////////////////////////////////