Add class PoolQueryResult: Helper class to collect (not only) PoolQuery results.
authorMichael Andres <ma@suse.de>
Tue, 29 Apr 2008 20:28:57 +0000 (20:28 +0000)
committerMichael Andres <ma@suse.de>
Tue, 29 Apr 2008 20:28:57 +0000 (20:28 +0000)
devel/devel.ma/NewPool.cc
zypp/CMakeLists.txt
zypp/PoolQueryResult.cc [new file with mode: 0644]
zypp/PoolQueryResult.h [new file with mode: 0644]

index fb28bfeebdbdeb7e5ceee7ca0f50ad4862c364c5..2ca50950eae7841af42ed12b63333e4fadd9d8b0 100644 (file)
@@ -1,5 +1,7 @@
 #include "Tools.h"
 
+#include "zypp/PoolQueryResult.h"
+
 #include <zypp/base/PtrTypes.h>
 #include <zypp/base/Exception.h>
 #include <zypp/base/Gettext.h>
@@ -454,112 +456,6 @@ void testCMP( const L & lhs, const R & rhs )
 
 namespace zypp
 {
-  /** Helper class to collect (not only) \ref PoolQuery results.
-   *
-   * \note Unfortunately \ref PoolQuery::begin might throw. Exceptions
-   * are caught and the query is treated as empty.
-   *
-   * \ref PoolQueryResult maintains a set of \ref sat::Solvable. You can
-   * add/remove solvables to/from the set defined by:
-   *
-   * \li a single \ref sat::Solvable
-   * \li a \ref PoolQuery
-   * \li an other \ref PoolQueryResult
-   * \li any iterator pair with \c value_type \ref sat::Solvable or \ref PoolQuery
-   *     or \ref PoolQueryResult (any type that fits \c operator+=)
-  */
-  class PoolQueryResult : public sat::SolvIterMixin<PoolQueryResult,std::tr1::unordered_set<sat::Solvable>::const_iterator>
-  {
-    public:
-      typedef std::tr1::unordered_set<sat::Solvable>   ResultSet;
-      typedef ResultSet::size_type                      size_type;
-      typedef ResultSet::const_iterator                 const_iterator;
-
-    public:
-      PoolQueryResult()
-      {}
-
-      explicit PoolQueryResult( sat::Solvable result_r )
-      { _result.insert( result_r ); }
-
-      explicit PoolQueryResult( const PoolQuery & query_r )
-      { operator+=( query_r ); }
-
-      template<class _QueryResultIter>
-      PoolQueryResult( _QueryResultIter begin_r, _QueryResultIter end_r )
-      {
-        for_( it, begin_r, end_r )
-        {
-          operator+=( *it );
-        }
-      }
-
-    public:
-      /***/
-      bool empty() const
-      { return _result.empty(); }
-      /***/
-      size_type size() const
-      { return _result.size(); }
-      /***/
-      const_iterator begin() const
-      { return _result.begin(); }
-      /***/
-      const_iterator end() const
-      { return _result.end(); }
-
-    public:
-      /**
-      */
-      PoolQueryResult & operator+=( const PoolQueryResult & query_r )
-      {
-        if ( ! query_r.empty() )
-          _result.insert( query_r.begin(), query_r.end() );
-        return *this;
-      }
-      /** \overload */
-      PoolQueryResult & operator+=( const PoolQuery & query_r )
-      { return operator+=( PoolQueryResult( query_r ) ); }
-      /** \overload */
-      PoolQueryResult & operator+=( sat::Solvable result_r )
-      { _result.insert( result_r ); return *this; }
-
-      /**
-      */
-      PoolQueryResult & operator-=( const PoolQueryResult & query_r );
-      /** \overload */
-      PoolQueryResult & operator-=( const PoolQuery & query_r )
-      { return operator-=( PoolQueryResult( query_r ) ); }
-      /** \overload */
-      PoolQueryResult & operator-=( sat::Solvable result_r )
-      { return operator+=( PoolQueryResult( result_r ) ); }
-
-    public:
-      /**
-      */
-      PoolQueryResult operator+( const PoolQueryResult & query_r ) const
-      { return PoolQueryResult(*this) += query_r; }
-      /** \overload */
-      PoolQueryResult operator+( const PoolQuery & query_r ) const
-      { return PoolQueryResult(*this) += query_r; }
-      /** \overload */
-      PoolQueryResult operator+( sat::Solvable result_r ) const
-      { return PoolQueryResult(*this) += result_r; }
-
-      /**
-      */
-      PoolQueryResult operator-( const PoolQueryResult & query_r ) const
-      { return PoolQueryResult(*this) -= query_r; }
-      /** \overload */
-      PoolQueryResult operator-( const PoolQuery & query_r ) const
-      { return PoolQueryResult(*this) -= query_r; }
-      /** \overload */
-      PoolQueryResult operator-( sat::Solvable result_r ) const
-      { return PoolQueryResult(*this) -= result_r; }
-
-    private:
-      ResultSet _result;
-  };
 }
 
 void tt( const std::string & name_r, ResKind kind_r = ResKind::package )
@@ -719,7 +615,28 @@ try {
   ///////////////////////////////////////////////////////////////////
   ///////////////////////////////////////////////////////////////////
 
+  //ResPool pool( ResPool::instance() );
+
+  //
+  PoolQueryResult result( pool.byKindBegin<Package>(), pool.byKindEnd<Package>() );
+  MIL << result.size() << endl;
+
+  {
+    PoolQuery q;
+    q.addAttribute( sat::SolvAttr::name, "[a-zA-Z]*" );
+    q.setMatchGlob();
+    result -= q;
+    MIL << result.size() << endl;
+  }
+  MIL << result << endl;
+
+  sat::WhatProvides poviders( Capability("3ddiag") );
+  result -= PoolQueryResult( poviders.begin(), poviders.end() );
+  MIL << result << endl;
+
 
+  result -= result;
+  MIL << result << endl;
 
   ///////////////////////////////////////////////////////////////////
   INT << "===[END]============================================" << endl << endl;
index 016942bf1067bae48ea297436b9516d67c120438..13c1f44d2433ae5fd30d16f9721f6267eba7a409 100644 (file)
@@ -44,6 +44,7 @@ SET( zypp_SRCS
   Pattern.cc
   PoolItem.cc
   PoolQuery.cc
+  PoolQueryResult.cc
   ProblemSolution.cc
   Product.cc
   ProgressData.cc
@@ -135,6 +136,7 @@ SET( zypp_HEADERS
   PoolItem.h
   PoolQuery.h
   PoolQueryUtil.tcc
+  PoolQueryResult.h
   ProblemSolution.h
   ProblemTypes.h
   Product.h
diff --git a/zypp/PoolQueryResult.cc b/zypp/PoolQueryResult.cc
new file mode 100644 (file)
index 0000000..c69f69c
--- /dev/null
@@ -0,0 +1,35 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file      zypp/PoolQueryResult.cc
+ *
+*/
+#include <iostream>
+#include "zypp/base/LogTools.h"
+
+#include "zypp/PoolQueryResult.h"
+
+using std::endl;
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+
+  /******************************************************************
+  **
+  **   FUNCTION NAME : operator<<
+  **   FUNCTION TYPE : std::ostream &
+  */
+  std::ostream & operator<<( std::ostream & str, const PoolQueryResult & obj )
+  {
+    return dumpRange( str, obj.begin(), obj.end() );
+  }
+
+  /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
diff --git a/zypp/PoolQueryResult.h b/zypp/PoolQueryResult.h
new file mode 100644 (file)
index 0000000..18f455e
--- /dev/null
@@ -0,0 +1,223 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file      zypp/PoolQueryResult.h
+ *
+*/
+#ifndef ZYPP_POOLQUERYRESULT_H
+#define ZYPP_POOLQUERYRESULT_H
+
+#include <iosfwd>
+
+#include "zypp/base/Tr1hash.h"
+#include "zypp/base/Exception.h"
+#include "zypp/sat/SolvIterMixin.h"
+
+#include "zypp/PoolItem.h"
+#include "zypp/PoolQuery.h"
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+
+  ///////////////////////////////////////////////////////////////////
+  //
+  //   CLASS NAME : PoolQueryResult
+  //
+  /** Helper class to collect (not only) \ref PoolQuery results.
+   *
+   * \note Unfortunately \ref PoolQuery::begin might throw. Exceptions
+   * are caught and the query is treated as empty.
+   *
+   * \ref PoolQueryResult maintains a set of \ref sat::Solvable. You can
+   * add/remove solvables to/from the set defined by:
+   *
+   * \li a single \ref sat::Solvable
+   * \li a single \ref PoolItem
+   * \li a \ref PoolQuery
+   * \li an other \ref PoolQueryResult
+   * \li any iterator pair with \c value_type \ref sat::Solvable
+   *     or \ref PoolItem or \ref PoolQuery or any type that fits
+   *     \c operator+=.
+   *
+   * \code
+   *   // Constructed from PoolItem iterator pair
+   *   PoolQueryResult result( pool.byKindBegin<Package>(), pool.byKindEnd<Package>() );
+   *   MIL << result.size() << endl;
+   *
+   *   {
+   *     // Removing a PoolQuery result
+   *     PoolQuery q;
+   *     q.addAttribute( sat::SolvAttr::name, "[a-zA-Z]*" );
+   *     q.setMatchGlob();
+   *     result -= q;
+   *     MIL << result.size() << endl;
+   *   }
+   *   MIL << result << endl;
+   *
+   *   // Removing a range of sat::Solvables
+   *   sat::WhatProvides poviders( Capability("3ddiag") );
+   *   result -= PoolQueryResult( poviders.begin(), poviders.end() );
+   *
+   *   // packages not starting with a letter, except 3ddiag
+   *   MIL << result << endl;
+   * \endcode
+   */
+  class PoolQueryResult : public sat::SolvIterMixin<PoolQueryResult,std::tr1::unordered_set<sat::Solvable>::const_iterator>
+  {
+    public:
+      typedef std::tr1::unordered_set<sat::Solvable>   ResultSet;
+      typedef ResultSet::size_type                      size_type;
+      typedef ResultSet::const_iterator                 const_iterator;
+
+    public:
+      /** Default ctor (empty result) */
+      PoolQueryResult()
+      {}
+
+      /** Ctor adding one \ref sat::Solvable. */
+      explicit PoolQueryResult( sat::Solvable result_r )
+      { operator+=( result_r ); }
+
+      /** Ctor adding one \ref PoolItem. */
+      explicit PoolQueryResult( const PoolItem & result_r )
+      { operator+=( result_r ); }
+
+      /** Ctor adding one \ref PoolQuery result. */
+      explicit PoolQueryResult( const PoolQuery & query_r )
+      { operator+=( query_r ); }
+
+      /** Ctor adding a range of items for which \ref operator+= is defined. */
+      template<class _QueryResultIter>
+      PoolQueryResult( _QueryResultIter begin_r, _QueryResultIter end_r )
+      {
+        for_( it, begin_r, end_r )
+        {
+          operator+=( *it );
+        }
+      }
+
+    public:
+      /** Whether the result is empty. */
+      bool empty() const
+      { return _result.empty(); }
+      /** The number of \ref sat::Solvables. */
+      size_type size() const
+      { return _result.size(); }
+      /** */
+      const_iterator begin() const
+      { return _result.begin(); }
+      /** */
+      const_iterator end() const
+      { return _result.end(); }
+
+    public:
+      /** Clear the result. */
+      void clear()
+      { _result.clear(); }
+
+      /** Add items to the result. */
+      PoolQueryResult & operator+=( const PoolQueryResult & query_r )
+      {
+        if ( ! query_r.empty() )
+          _result.insert( query_r.begin(), query_r.end() );
+        return *this;
+      }
+      /** \overload */
+      PoolQueryResult & operator+=( const PoolQuery & query_r )
+      {
+        try
+        {
+          for_( it, query_r.begin(), query_r.end() )
+            _result.insert( *it );
+        }
+        catch ( const Exception & )
+        {}
+        return *this;
+      }
+      /** \overload */
+      PoolQueryResult & operator+=( sat::Solvable result_r )
+      {
+        _result.insert( result_r );
+        return *this;
+      }
+      /** \overload */
+      PoolQueryResult & operator+=( const PoolItem & result_r )
+      {
+        _result.insert( result_r.satSolvable() );
+        return *this;
+      }
+
+      /** Remove Items from the result. */
+      PoolQueryResult & operator-=( const PoolQueryResult & query_r )
+      {
+        if ( &query_r == this ) // catch self intersection!
+          clear();
+        else
+          for_( it, query_r.begin(), query_r.end() )
+            _result.erase( *it );
+        return *this;
+      }
+      /** \overload */
+      PoolQueryResult & operator-=( const PoolQuery & query_r )
+      {
+        try
+        {
+          for_( it, query_r.begin(), query_r.end() )
+            _result.erase( *it );
+        }
+        catch ( const Exception & )
+        {}
+        return *this;
+      }
+      /** \overload */
+      PoolQueryResult & operator-=( sat::Solvable result_r )
+      {
+        _result.erase( result_r );
+        return *this;
+      }
+      /** \overload */
+      PoolQueryResult & operator-=( const PoolItem & result_r )
+      {
+        _result.erase( result_r.satSolvable() );
+        return *this;
+      }
+
+    public:
+      /** Combine results. */
+      PoolQueryResult operator+( const PoolQueryResult & query_r ) const
+      { return PoolQueryResult(*this) += query_r; }
+      /** \overload */
+      PoolQueryResult operator+( const PoolQuery & query_r ) const
+      { return PoolQueryResult(*this) += query_r; }
+      /** \overload */
+      PoolQueryResult operator+( sat::Solvable result_r ) const
+      { return PoolQueryResult(*this) += result_r; }
+
+      /** Intersect results. */
+      PoolQueryResult operator-( const PoolQueryResult & query_r ) const
+      { return PoolQueryResult(*this) -= query_r; }
+      /** \overload */
+      PoolQueryResult operator-( const PoolQuery & query_r ) const
+      { return PoolQueryResult(*this) -= query_r; }
+      /** \overload */
+      PoolQueryResult operator-( sat::Solvable result_r ) const
+      { return PoolQueryResult(*this) -= result_r; }
+
+    private:
+      ResultSet _result;
+  };
+  ///////////////////////////////////////////////////////////////////
+
+  /** \relates PoolQueryResult Stream output */
+  std::ostream & operator<<( std::ostream & str, const PoolQueryResult & obj );
+
+  /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
+#endif // ZYPP_POOLQUERYRESULT_H