1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PoolItemBest.h
12 #ifndef ZYPP_POOLITEMBEST_H
13 #define ZYPP_POOLITEMBEST_H
17 #include "zypp/base/PtrTypes.h"
18 #include "zypp/base/Function.h"
19 #include "zypp/base/Iterator.h"
20 #include "zypp/base/Hash.h"
22 #include "zypp/PoolItem.h"
24 ///////////////////////////////////////////////////////////////////
26 { /////////////////////////////////////////////////////////////////
28 ///////////////////////////////////////////////////////////////////
30 // CLASS NAME : PoolItemBest
32 /** Find the best candidates e.g. in a \ref PoolQuery result.
34 * The class basically maintains a \c map<IdString,PoolItem> and remembers
35 * for each \c ident (\ref sat::Solvable::ident) the best \ref PoolItem that
38 * The default \ref Predicate to determine the best choice is the same that
39 * sorts the \ref ui::Selectable list of available objects, thus follows the
40 * same rules the \ref resolver will apply.
44 * q.addAttribute(sat::SolvAttr::name, "lib*");
47 * // get the best matches and tag them for installation:
48 * PoolItemBest bestMatches( q.begin(), q.end() );
49 * if ( ! bestMatches.empty() )
51 * for_( it, bestMatches.begin(), bestMatches.end() )
53 * ui::asSelectable()( *it )->setOnSystem( *it, ResStatus::USER );
58 * \todo Support arbitrary Predicates.
62 typedef std::unordered_map<IdString,PoolItem> Container;
64 /** Predicate returning \c True if \a lhs is a better choice. */
65 typedef boost::function<bool ( const PoolItem & lhs, const PoolItem & rhs )> Predicate;
67 typedef Container::size_type size_type;
68 typedef Container::value_type value_type;
69 typedef MapKVIteratorTraits<Container>::Value_const_iterator iterator;
70 typedef MapKVIteratorTraits<Container>::Key_const_iterator ident_iterator;
77 /** Ctor feeding a \ref sat::Solvable. */
78 PoolItemBest( sat::Solvable slv_r )
79 { _ctor_init(); add( slv_r ); }
81 /** Ctor feeding a \ref PoolItem. */
82 PoolItemBest( const PoolItem & pi_r )
83 { _ctor_init(); add( pi_r ); }
85 /** Ctor feeding a range of \ref sat::Solvable or \ref PoolItem. */
86 template<class TIterator>
87 PoolItemBest( TIterator begin_r, TIterator end_r )
88 { _ctor_init(); add( begin_r, end_r ); }
91 /** Feed one \ref sat::Solvable. */
92 void add( sat::Solvable slv_r )
93 { add( PoolItem( slv_r ) ); }
95 /** Feed one \ref PoolItem. */
96 void add( const PoolItem & pi_r );
98 /** Feed a range of \ref sat::Solvable or \ref PoolItem. */
99 template<class TIterator>
100 void add( TIterator begin_r, TIterator end_r )
102 for_( it, begin_r, end_r )
107 /** \name Iterate the collected PoolItems. */
109 /** Whether PoolItems were collected. */
110 bool empty() const { return container().empty(); }
111 /** Number of PoolItems collected. */
112 size_type size() const { return container().size(); }
113 /** Pointer to the first PoolItem. */
114 iterator begin() const { return make_map_value_begin( container() ); }
115 /** Pointer behind the last PoolItem. */
116 iterator end() const { return make_map_value_end( container() ); }
118 /** Return the collected \ref PoolItem with \ref sat::Solvable::ident \a ident_r. */
119 PoolItem find( IdString ident_r ) const;
120 /** \overload Use Solvables ident string. */
121 PoolItem find( sat::Solvable slv_r ) const { return find( slv_r.ident() ); }
122 /** \overload Use PoolItems ident string. */
123 PoolItem find( const PoolItem & pi_r ) const { return find( pi_r.satSolvable().ident() ); }
126 /** \name Iterate the collected PoolItems ident strings. */
128 /** Pointer to the first item. */
129 ident_iterator identBegin() const { return make_map_key_begin( container() ); }
130 /** Pointer behind the last item. */
131 ident_iterator identEnd() const { return make_map_key_end( container() ); }
136 const Container & container() const;
138 /** Implementation */
140 /** Pointer to implementation */
141 RWCOW_pointer<Impl> & pimpl() { return *(reinterpret_cast<RWCOW_pointer<Impl>*>( _dont_use_this_use_pimpl.get() )); }
142 /** Pointer to implementation */
143 const RWCOW_pointer<Impl> & pimpl() const { return *(reinterpret_cast<RWCOW_pointer<Impl>*>( _dont_use_this_use_pimpl.get() )); }
144 /** Avoid need to include Impl definition when inlined ctors (due to tepmlate) are provided. */
145 shared_ptr<void> _dont_use_this_use_pimpl;
147 ///////////////////////////////////////////////////////////////////
149 /** \relates PoolItemBest Stream output */
150 std::ostream & operator<<( std::ostream & str, const PoolItemBest & obj );
152 /////////////////////////////////////////////////////////////////
154 ///////////////////////////////////////////////////////////////////
155 #endif // ZYPP_POOLITEMBEST_H