5014dbf4e716d03beb7208dab2eed38a6e718fd0
[platform/upstream/libzypp.git] / zypp / ui / SelectableTraits.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/ui/SelectableTraits.h
10  *
11 */
12 #ifndef ZYPP_UI_SELECTABLETRAITS_H
13 #define ZYPP_UI_SELECTABLETRAITS_H
14
15 #include <set>
16 #include <vector>
17
18 #include "zypp/base/Iterator.h"
19 #include "zypp/PoolItem.h"
20 #include "zypp/pool/ByIdent.h"
21
22 ///////////////////////////////////////////////////////////////////
23 namespace zypp
24 { /////////////////////////////////////////////////////////////////
25   ///////////////////////////////////////////////////////////////////
26   namespace ui
27   { /////////////////////////////////////////////////////////////////
28
29     ///////////////////////////////////////////////////////////////////
30     //
31     //  CLASS NAME : SelectableTraits
32     //
33     /** */
34     struct SelectableTraits
35     {
36       /** Oder on AvailableItemSet.
37        * \li repository priority
38        * \li best Arch (arch/noarch changes are ok)
39        * \li best Edition
40        * \li newer buildtime
41        * \li ResObject::constPtr as fallback.
42       */
43       struct AVOrder : public std::binary_function<PoolItem,PoolItem,bool>
44       {
45         // NOTE: operator() provides LESS semantics to order the set.
46         // So LESS means 'prior in set'. We want 'better' archs and
47         // 'better' editions at the beginning of the set. So we return
48         // TRUE if (lhs > rhs)!
49         //
50         bool operator()( const PoolItem & lhs, const PoolItem & rhs ) const
51         {
52           int lprio = lhs->satSolvable().repository().satInternalPriority();
53           int rprio = rhs->satSolvable().repository().satInternalPriority();
54           if ( lprio != rprio )
55             return( lprio > rprio );
56
57           // arch/noarch changes are ok.
58           if ( lhs->arch() != Arch_noarch && rhs->arch() != Arch_noarch )
59           {
60             int res = lhs->arch().compare( rhs->arch() );
61             if ( res )
62               return res > 0;
63           }
64
65           int res = lhs->edition().compare( rhs->edition() );
66           if ( res )
67             return res > 0;
68
69           lprio = lhs->buildtime();
70           rprio = rhs->buildtime();
71           if ( lprio != rprio )
72             return( lprio > rprio );
73
74           lprio = lhs->satSolvable().repository().satInternalSubPriority();
75           rprio = rhs->satSolvable().repository().satInternalSubPriority();
76           if ( lprio != rprio )
77             return( lprio > rprio );
78
79           // no more criteria, still equal: sort by id
80           return lhs.satSolvable().id() < rhs.satSolvable().id();
81         }
82       };
83
84       /** Oder on InstalledItemSet.
85        * \li best Arch
86        * \li best Edition
87        * \li newer install time
88        * \li ResObject::constPtr as fallback.
89       */
90       struct IOrder : public std::binary_function<PoolItem,PoolItem,bool>
91       {
92         // NOTE: operator() provides LESS semantics to order the set.
93         // So LESS means 'prior in set'. We want 'newer' install time
94         // at the beginning of the set.
95         //
96         bool operator()( const PoolItem & lhs, const PoolItem & rhs ) const
97         {
98           int res = lhs->arch().compare( rhs->arch() );
99           if ( res )
100             return res > 0;
101           res = lhs->edition().compare( rhs->edition() );
102           if ( res )
103             return res > 0;
104           Date ldate = lhs->installtime();
105           Date rdate = rhs->installtime();
106           if ( ldate != rdate )
107             return( ldate > rdate );
108
109           // no more criteria, still equal: sort by id
110           return lhs.satSolvable().id() < rhs.satSolvable().id();
111         }
112       };
113
114       typedef std::set<PoolItem,AVOrder>       AvailableItemSet;
115       typedef AvailableItemSet::iterator       available_iterator;
116       typedef AvailableItemSet::const_iterator available_const_iterator;
117       typedef AvailableItemSet::size_type      available_size_type;
118
119       typedef std::set<PoolItem,IOrder>        InstalledItemSet;
120       typedef AvailableItemSet::iterator       installed_iterator;
121       typedef AvailableItemSet::const_iterator installed_const_iterator;
122       typedef AvailableItemSet::size_type      installed_size_type;
123
124       typedef std::vector<PoolItem>             PickList;
125       typedef PickList::const_iterator          picklist_iterator;
126       typedef PickList::size_type               picklist_size_type;
127     };
128     ///////////////////////////////////////////////////////////////////
129
130     /////////////////////////////////////////////////////////////////
131   } // namespace ui
132   ///////////////////////////////////////////////////////////////////
133   /////////////////////////////////////////////////////////////////
134 } // namespace zypp
135 ///////////////////////////////////////////////////////////////////
136 #endif // ZYPP_UI_SELECTABLETRAITS_H