1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PoolQueryResult.h
12 #ifndef ZYPP_POOLQUERYRESULT_H
13 #define ZYPP_POOLQUERYRESULT_H
17 #include "zypp/base/Hash.h"
18 #include "zypp/base/Exception.h"
19 #include "zypp/sat/SolvIterMixin.h"
21 #include "zypp/PoolItem.h"
22 #include "zypp/PoolQuery.h"
24 ///////////////////////////////////////////////////////////////////
26 { /////////////////////////////////////////////////////////////////
28 ///////////////////////////////////////////////////////////////////
30 // CLASS NAME : PoolQueryResult
32 /** Helper class to collect (not only) \ref PoolQuery results.
34 * \note Unfortunately \ref PoolQuery::begin might throw. Exceptions
35 * are caught and the query is treated as empty.
37 * \ref PoolQueryResult maintains a set of \ref sat::Solvable. You can
38 * add/remove solvables to/from the set defined by:
40 * \li a single \ref sat::Solvable
41 * \li a single \ref PoolItem
42 * \li a \ref PoolQuery
43 * \li an other \ref PoolQueryResult
44 * \li any iterator pair with \c value_type \ref sat::Solvable
45 * or \ref PoolItem or \ref PoolQuery or any type that fits
48 * The class is a \ref sat::SolvIterMixin, so you can iterate the result
49 * not just as \ref sat::Solvable, but also as \ref PoolItem or
50 * \ref ui::Selectable.
53 * // Constructed from PoolItem iterator pair
54 * PoolQueryResult result( pool.byKindBegin<Package>(), pool.byKindEnd<Package>() );
55 * MIL << result.size() << endl;
58 * // Removing a PoolQuery result
60 * q.addAttribute( sat::SolvAttr::name, "[a-zA-Z]*" );
63 * MIL << result.size() << endl;
65 * MIL << result << endl;
67 * // Removing a range of sat::Solvables
68 * sat::WhatProvides poviders( Capability("3ddiag") );
69 * result -= PoolQueryResult( poviders.begin(), poviders.end() );
71 * // packages not starting with a letter, except 3ddiag
72 * MIL << result << endl;
75 class PoolQueryResult : public sat::SolvIterMixin<PoolQueryResult,std::unordered_set<sat::Solvable>::const_iterator>
78 typedef std::unordered_set<sat::Solvable> ResultSet;
79 typedef ResultSet::size_type size_type;
80 typedef ResultSet::const_iterator const_iterator;
83 /** Default ctor (empty result) */
87 /** Ctor adding one \ref sat::Solvable. */
88 explicit PoolQueryResult( sat::Solvable result_r )
89 { operator+=( result_r ); }
91 /** Ctor adding one \ref PoolItem. */
92 explicit PoolQueryResult( const PoolItem & result_r )
93 { operator+=( result_r ); }
95 /** Ctor adding one \ref PoolQuery result. */
96 explicit PoolQueryResult( const PoolQuery & query_r )
97 { operator+=( query_r ); }
99 /** Ctor adding a range of items for which \ref operator+= is defined. */
100 template<class TQueryResultIter>
101 PoolQueryResult( TQueryResultIter begin_r, TQueryResultIter end_r )
103 for_( it, begin_r, end_r )
110 /** Whether the result is empty. */
112 { return _result.empty(); }
113 /** The number of \ref sat::Solvables. */
114 size_type size() const
115 { return _result.size(); }
117 const_iterator begin() const
118 { return _result.begin(); }
120 const_iterator end() const
121 { return _result.end(); }
123 /** Test whether some item is in the result set. */
124 bool contains(sat::Solvable result_r ) const
125 { return( _result.find( result_r ) != _result.end() ); }
127 bool contains( const PoolItem & result_r ) const
128 { return contains( result_r.satSolvable() ); }
131 /** Clear the result. */
135 /** Add items to the result. */
136 PoolQueryResult & operator+=( const PoolQueryResult & query_r )
138 if ( ! query_r.empty() )
139 _result.insert( query_r.begin(), query_r.end() );
143 PoolQueryResult & operator+=( const PoolQuery & query_r )
147 for_( it, query_r.begin(), query_r.end() )
148 _result.insert( *it );
150 catch ( const Exception & )
155 PoolQueryResult & operator+=( sat::Solvable result_r )
157 _result.insert( result_r );
161 PoolQueryResult & operator+=( const PoolItem & result_r )
163 _result.insert( result_r.satSolvable() );
167 /** Remove Items from the result. */
168 PoolQueryResult & operator-=( const PoolQueryResult & query_r )
170 if ( &query_r == this ) // catch self removal!
173 for_( it, query_r.begin(), query_r.end() )
174 _result.erase( *it );
178 PoolQueryResult & operator-=( const PoolQuery & query_r )
182 for_( it, query_r.begin(), query_r.end() )
183 _result.erase( *it );
185 catch ( const Exception & )
190 PoolQueryResult & operator-=( sat::Solvable result_r )
192 _result.erase( result_r );
196 PoolQueryResult & operator-=( const PoolItem & result_r )
198 _result.erase( result_r.satSolvable() );
203 /** Combine results. */
204 PoolQueryResult operator+( const PoolQueryResult & query_r ) const
205 { return PoolQueryResult(*this) += query_r; }
207 PoolQueryResult operator+( const PoolQuery & query_r ) const
208 { return PoolQueryResult(*this) += query_r; }
210 PoolQueryResult operator+( sat::Solvable result_r ) const
211 { return PoolQueryResult(*this) += result_r; }
213 /** Intersect results. */
214 PoolQueryResult operator-( const PoolQueryResult & query_r ) const
215 { return PoolQueryResult(*this) -= query_r; }
217 PoolQueryResult operator-( const PoolQuery & query_r ) const
218 { return PoolQueryResult(*this) -= query_r; }
220 PoolQueryResult operator-( sat::Solvable result_r ) const
221 { return PoolQueryResult(*this) -= result_r; }
226 ///////////////////////////////////////////////////////////////////
228 /** \relates PoolQueryResult Stream output */
229 std::ostream & operator<<( std::ostream & str, const PoolQueryResult & obj );
231 /////////////////////////////////////////////////////////////////
233 ///////////////////////////////////////////////////////////////////
234 #endif // ZYPP_POOLQUERYRESULT_H