Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / ResFilters.h
index cda692e..1b4ffd0 100644 (file)
 #ifndef ZYPP_RESFILTERS_H
 #define ZYPP_RESFILTERS_H
 
+#include <boost/function.hpp>
+
 #include "zypp/base/Functional.h"
+#include "zypp/Filter.h"
 #include "zypp/Resolvable.h"
-#include "zypp/CapFilters.h"
-
-#include "zypp/Source.h"
-#include "zypp/source/SourceImpl.h"
 
 #include "zypp/PoolItem.h"
+#include "zypp/Repository.h"
 
 ///////////////////////////////////////////////////////////////////
 namespace zypp
@@ -60,7 +60,7 @@ namespace zypp
      *     return true;
      *   }
      *
-     *   unsigned _counter;
+     *   unsigned _counter;
      * };
      *
      * ResStore store;
@@ -75,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() );
@@ -137,6 +137,8 @@ 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.
     */
     //@{
     ///////////////////////////////////////////////////////////////////
@@ -147,30 +149,19 @@ namespace zypp
 
     /** */
     typedef std::unary_function<ResObject::constPtr, bool> ResObjectFilterFunctor;
-
-    /** Select ResObject by kind. */
-    struct ByKind : public ResObjectFilterFunctor
-    {
-      ByKind( const ResObject::Kind & kind_r )
-      : _kind( kind_r )
-      {}
-
-      bool operator()( ResObject::constPtr p ) const
-      {
-        return p->kind() == _kind;
-      }
-
-      ResObject::Kind _kind;
-    };
+    typedef boost::function<bool ( ResObject::constPtr )> ResFilter;
 
     /** */
-    template<class _Res>
-      inline ByKind byKind()
-      { return ByKind( ResTraits<_Res>::kind ); }
+    template<class TRes>
+      inline filter::ByKind byKind()
+      { return filter::ByKind( ResTraits<TRes>::kind ); }
 
     /** Select ResObject by name. */
     struct ByName : public ResObjectFilterFunctor
     {
+      ByName()
+      {}
+
       ByName( const std::string & name_r )
       : _name( name_r )
       {}
@@ -183,26 +174,28 @@ namespace zypp
       std::string _name;
     };
 
-
-    /** Select ResObject by source. */
-    struct BySource : public ResObjectFilterFunctor
+    /** Select ResObject by repository or repository alias. */
+    struct ByRepository : public ResObjectFilterFunctor
     {
-      BySource( Source_Ref source_r )
-      : _source( source_r )
+      ByRepository( Repository repository_r )
+      : _alias( repository_r.info().alias() )
+      {}
+
+      ByRepository( const std::string & alias_r )
+      : _alias( alias_r )
       {}
 
       bool operator()( ResObject::constPtr p ) const
       {
-        return p->source() == _source;
+        return p->repoInfo().alias() == _alias;
       }
 
-      Source_Ref _source;
+      std::string _alias;
     };
 
-
-    /** Select ResObject by Edition using \a _Compare functor.
+    /** Select ResObject by Edition using \a TCompare functor.
      *
-     * Selects ResObject if <tt>_Compare( ResObject->edition(), _edition )<\tt>
+     * Selects ResObject if <tt>TCompare( ResObject->edition(), _edition )</tt>
      * is \c true.
      * \code
      * // use the convenience funktions to create ByEdition:
@@ -212,11 +205,10 @@ namespace zypp
      * byEdition( someedition, CompareByGT<Edition>() ) //  edition >  someedition
      * \endcode
     */
-    template<class _Compare = CompareByEQ<Edition> >
+    template<class TCompare = CompareByEQ<Edition> >
       struct ByEdition : public ResObjectFilterFunctor
       {
-        ByEdition( const Edition & edition_r,
-                   _Compare cmp_r )
+        ByEdition( const Edition & edition_r, TCompare cmp_r )
         : _edition( edition_r )
         , _cmp( cmp_r )
         {}
@@ -227,45 +219,58 @@ namespace zypp
         }
 
         Edition  _edition;
-        _Compare _cmp;
+        TCompare _cmp;
       };
 
     /** */
-    template<class _Compare>
-      ByEdition<_Compare> byEdition( const Edition & edition_r, _Compare cmp_r )
-      { return ByEdition<_Compare>( edition_r, cmp_r ); }
+    template<class TCompare>
+      ByEdition<TCompare> byEdition( const Edition & edition_r, TCompare cmp_r )
+      { return ByEdition<TCompare>( edition_r, cmp_r ); }
 
     /** */
-    template<class _Compare>
-      ByEdition<_Compare> byEdition( const Edition & edition_r )
-      { return byEdition( edition_r, _Compare() ); }
+    template<class TCompare>
+      ByEdition<TCompare> byEdition( const Edition & edition_r )
+      { return byEdition( edition_r, TCompare() ); }
 
 
-    /** Select ResObject if at least one Capability with
-     *  index \a index_r was found in dependency \a depType_r.
+    /** 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
     */
-    struct ByCapabilityIndex : public ResObjectFilterFunctor
-    {
-      ByCapabilityIndex( const std::string & index_r, Dep depType_r )
-      : _dep( depType_r )
-      , _index( index_r )
-      {}
-      ByCapabilityIndex( const Capability & cap_r, Dep depType_r )
-      : _dep( depType_r )
-      , _index( cap_r.index() )
-      {}
-
-      bool operator()( ResObject::constPtr p ) const
+    template<class TCompare = CompareByEQ<Arch> >
+      struct ByArch : public ResObjectFilterFunctor
       {
-        using capfilter::ByIndex;
-        return(    make_filter_begin( ByIndex(_index), p->dep( _dep ) )
-                != make_filter_end( ByIndex(_index), p->dep( _dep ) ) );
-      }
+        ByArch( const Arch & arch_r, TCompare cmp_r )
+        : _arch( arch_r )
+        , _cmp( cmp_r )
+        {}
 
-      Dep         _dep;
-      std::string _index;
-    };
+        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() ); }
 
 
     ///////////////////////////////////////////////////////////////////
@@ -284,9 +289,8 @@ namespace zypp
     {
       bool operator()( const PoolItem & p ) const
       {
-       return p.status().staysInstalled();
+       return p.status().isInstalled();
       }
-
     };
 
     /** Select PoolItem by uninstalled. */
@@ -294,7 +298,7 @@ namespace zypp
     {
       bool operator()( const PoolItem & p ) const
       {
-       return p.status().staysUninstalled();
+       return p.status().isUninstalled();
       }
     };
 
@@ -307,65 +311,41 @@ namespace zypp
       }
     };
 
-    ///////////////////////////////////////////////////////////////////
-
-    typedef std::binary_function<PoolItem,Capability,
-                                 bool> OnCapMatchCallbackFunctor;
-
-    /** Find matching Capabilities in a ResObjects dependency and invoke a
-     *  callback on matches.
-     *
-     * Iterates through the PoolItem (in fact the ResObject it holds)
-     * CapSet denoted by \a dep_r. For each Capability matching the
-     * provided \a cap_r the callback functor \a fnc_r is called with
-     * the PoolItem and the PoolItem's matching Capability.
-     *
-     * \returns \c true, unless an invokation of the callback functor
-     * returned \c false.
-     *
-     * \todo Unfortunately a pure PoolItem Filter, but woud be usefull with
-     * plain ResObjects too. But the Solver urgently needs the PoolItem in
-     * the OnCapMatchCallback.
-    */
-    template<class _OnCapMatchCallback>
-      struct CallOnCapMatchIn
+    /** Select PoolItem by lock. */
+    struct ByLock : public PoolItemFilterFunctor
+    {
+      bool operator()( const PoolItem & p ) const
       {
-        bool operator()( const PoolItem & p ) const
-        {
-          const CapSet & depSet( p->dep( _dep ) ); // dependency set in p to iterate
-          capfilter::ByCapMatch matching( _cap );  // predicate: true if match with _cap
-
-          int res
-          = invokeOnEach( depSet.begin(), depSet.end(), // iterate this set
-                          matching,                     // Filter: if match
-                          std::bind1st(_fnc,p) );       // Action: invoke _fnc(p,match)
-          // Maybe worth to note: Filter and Action are invoked with the same
-          // iterator, thus Action will use the same capability that cause
-          // the match in Filter.
-          return ( res >= 0 );
-        }
+       return p.status().isLocked();
+      }
+    };
 
-        CallOnCapMatchIn( Dep dep_r, const Capability & cap_r,
-                          _OnCapMatchCallback fnc_r )
-        : _dep( dep_r )
-        , _cap( cap_r )
-        , _fnc( fnc_r )
-        {}
-        Dep                 _dep;
-        const Capability &  _cap;
-        _OnCapMatchCallback _fnc;
-      };
+    /** Select PoolItem by keep. */
+    struct ByKeep : public PoolItemFilterFunctor
+    {
+      bool operator()( const PoolItem & p ) const
+      {
+       return p.status().isKept();
+      }
+    };
 
-    /** */
-    template<class _OnCapMatchCallback>
-      inline CallOnCapMatchIn<_OnCapMatchCallback>
-      callOnCapMatchIn( Dep dep_r, const Capability & cap_r,
-                        _OnCapMatchCallback fnc_r )
+    /** PoolItem which is recommended. */
+    struct ByRecommended : public PoolItemFilterFunctor
+    {
+      bool operator()( const PoolItem & p ) const
       {
-        return CallOnCapMatchIn<_OnCapMatchCallback>( dep_r, cap_r, fnc_r );
+       return p.status().isRecommended();
       }
+    };
 
-    ///////////////////////////////////////////////////////////////////
+    /** PoolItem which is suggested. */
+    struct BySuggested : public PoolItemFilterFunctor
+    {
+      bool operator()( const PoolItem & p ) const
+      {
+       return p.status().isSuggested();
+      }
+    };
 
     //@}
     /////////////////////////////////////////////////////////////////