Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / PoolItemBest.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/PoolItemBest.h
10  *
11 */
12 #ifndef ZYPP_POOLITEMBEST_H
13 #define ZYPP_POOLITEMBEST_H
14
15 #include <iosfwd>
16
17 #include "zypp/base/PtrTypes.h"
18 #include "zypp/base/Function.h"
19 #include "zypp/base/Iterator.h"
20 #include "zypp/base/Hash.h"
21
22 #include "zypp/PoolItem.h"
23
24 ///////////////////////////////////////////////////////////////////
25 namespace zypp
26 { /////////////////////////////////////////////////////////////////
27
28   ///////////////////////////////////////////////////////////////////
29   //
30   //    CLASS NAME : PoolItemBest
31   //
32   /** Find the best candidates e.g. in a \ref PoolQuery result.
33    *
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
36    * was added.
37    *
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.
41    *
42    * \code
43    *   PoolQuery q;
44    *   q.addAttribute(sat::SolvAttr::name, "lib*");
45    *   q.setMatchGlob();
46    *
47    *  // get the best matches and tag them for installation:
48    *  PoolItemBest bestMatches( q.begin(), q.end() );
49    *  if ( ! bestMatches.empty() )
50    *  {
51    *    for_( it, bestMatches.begin(), bestMatches.end() )
52    *    {
53    *      ui::asSelectable()( *it )->setOnSystem( *it, ResStatus::USER );
54    *    }
55    *  }
56    * \endcode
57    *
58    * \todo Support arbitrary Predicates.
59    */
60   class PoolItemBest
61   {
62       typedef std::unordered_map<IdString,PoolItem> Container;
63     public:
64       /** Predicate returning \c True if \a lhs is a better choice. */
65       typedef boost::function<bool ( const PoolItem & lhs, const PoolItem & rhs )> Predicate;
66
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;
71
72     public:
73       /** Default ctor. */
74       PoolItemBest()
75       { _ctor_init(); }
76
77       /** Ctor feeding a \ref sat::Solvable. */
78       PoolItemBest( sat::Solvable slv_r )
79       { _ctor_init(); add( slv_r ); }
80
81       /** Ctor feeding a \ref PoolItem. */
82       PoolItemBest( const PoolItem & pi_r )
83       { _ctor_init(); add( pi_r ); }
84
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 ); }
89
90     public:
91       /** Feed one \ref sat::Solvable. */
92       void add( sat::Solvable slv_r )
93       { add( PoolItem( slv_r ) ); }
94
95       /** Feed one \ref PoolItem. */
96       void add( const PoolItem & pi_r );
97
98       /** Feed a range of  \ref sat::Solvable or \ref PoolItem. */
99       template<class TIterator>
100       void add( TIterator begin_r, TIterator end_r )
101       {
102         for_( it, begin_r, end_r )
103           add( *it );
104       }
105
106     public:
107       /** \name Iterate the collected PoolItems. */
108       //@{
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() ); }
117
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() ); }
124       //@}
125
126       /** \name Iterate the collected PoolItems ident strings. */
127       //@{
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() ); }
132       //@}
133
134     private:
135       void _ctor_init();
136       const Container & container() const;
137     private:
138       /** Implementation  */
139       class Impl;
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;
146   };
147   ///////////////////////////////////////////////////////////////////
148
149   /** \relates PoolItemBest Stream output */
150   std::ostream & operator<<( std::ostream & str, const PoolItemBest & obj );
151
152   /////////////////////////////////////////////////////////////////
153 } // namespace zypp
154 ///////////////////////////////////////////////////////////////////
155 #endif // ZYPP_POOLITEMBEST_H