- Create the cache directly from the schema (installed) file.
[platform/upstream/libzypp.git] / zypp / NameKindProxy.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/NameKindProxy.h
10  *
11 */
12 #ifndef ZYPP_NAMEKINDPROXY_H
13 #define ZYPP_NAMEKINDPROXY_H
14
15 #include <iosfwd>
16 #include <set>
17
18 #include "zypp/ResPool.h"
19
20 ///////////////////////////////////////////////////////////////////
21 namespace zypp
22 { /////////////////////////////////////////////////////////////////
23
24   namespace name_kind_proxy_details
25   {
26     struct IOrder : public std::binary_function<PoolItem,PoolItem,bool>
27     {
28       // NOTE: operator() provides LESS semantics to order the set.
29       // So LESS means 'prior in set'. We want the last item installed
30       // first.
31       /** \todo let ResObject provide installtime */
32       bool operator()( const PoolItem & lhs, const PoolItem & rhs ) const
33       {
34         // top should be installtime!
35
36         int res = lhs->arch().compare( rhs->arch() );
37         if ( res )
38           return res > 0;
39         res = lhs->edition().compare( rhs->edition() );
40         if ( res )
41           return res > 0;
42
43         // no more criteria, still equal:
44         // use the ResObject::constPtr (the poiner value)
45         // (here it's arbitrary whether < or > )
46         return lhs.resolvable() < rhs.resolvable();
47       }
48     };
49
50     struct AOrder : public std::binary_function<PoolItem,PoolItem,bool>
51     {
52       // NOTE: operator() provides LESS semantics to order the set.
53       // So LESS means 'prior in set'. We want 'better' archs and
54       // 'better' editions at the beginning of the set. So we return
55       // TRUE if (lhs > rhs)!
56       //
57       bool operator()( const PoolItem & lhs, const PoolItem & rhs ) const
58       {
59         int res = lhs->arch().compare( rhs->arch() );
60         if ( res )
61           return res > 0;
62         res = lhs->edition().compare( rhs->edition() );
63         if ( res )
64           return res > 0;
65
66         // no more criteria, still equal:
67         // use the ResObject::constPtr (the poiner value)
68         // (here it's arbitrary whether < or > )
69         return lhs.resolvable() < rhs.resolvable();
70       }
71     };
72   }
73
74   ///////////////////////////////////////////////////////////////////
75   //
76   //    CLASS NAME : NameKindProxy
77   //
78   /** Retrieve and maintain PoolItem of a certain name and kind.
79    *
80    * Installed PoolItems are sorted according to their installtime
81    * (last installed first).
82    *
83    * Available PoolItems are sorted 'best first'.
84    *
85    * \todo provide status query and manipulation methods
86   */
87   class NameKindProxy
88   {
89     typedef name_kind_proxy_details::IOrder IOrder;
90     typedef name_kind_proxy_details::AOrder AOrder;
91   public:
92     typedef std::set<PoolItem,IOrder>    InstalledSet;
93     typedef InstalledSet::iterator       Installed_iterator;
94     typedef InstalledSet::const_iterator Installed_const_iterator;
95     typedef InstalledSet::size_type      Installed_size_type;
96
97     typedef std::set<PoolItem,AOrder>    AvailableSet;
98     typedef AvailableSet::iterator       Available_iterator;
99     typedef AvailableSet::const_iterator Available_const_iterator;
100     typedef AvailableSet::size_type      Available_size_type;
101
102   public:
103     NameKindProxy( ResPool pool_r,
104                    const std::string & name_r, Resolvable::Kind kind_r );
105
106   public:
107     ResObject::Kind kind() const
108     { return _kind; }
109
110     const std::string & name() const
111     { return _name; }
112
113   public:
114     Installed_size_type installedSize() const
115     { return _installed.size(); }
116
117     bool installedEmpty() const
118     { return _installed.empty(); }
119
120     Installed_const_iterator installedBegin() const
121     { return _installed.begin(); }
122
123     Installed_const_iterator installedEnd() const
124     { return _installed.end(); }
125
126   public:
127     Available_size_type availableSize() const
128     { return _available.size(); }
129
130     bool availableEmpty() const
131     { return _available.empty(); }
132
133     Available_const_iterator availableBegin() const
134     { return _available.begin(); }
135
136     Available_const_iterator availableEnd() const
137     { return _available.end(); }
138
139   public:
140
141     // status query and manip stuff...
142
143   private:
144     ResObject::Kind _kind;
145     std::string     _name;
146     InstalledSet    _installed;
147     AvailableSet    _available;
148   };
149   ///////////////////////////////////////////////////////////////////
150
151   /** \relates NameKindProxy Stream output. */
152   std::ostream & operator<<( std::ostream & str, const NameKindProxy & obj );
153
154   /** \relates NameKindProxy Convenience construction. */
155   template<class _Res>
156     inline NameKindProxy nameKindProxy( ResPool pool_r, const std::string & name_r )
157     { return NameKindProxy( pool_r, name_r, ResTraits<_Res>::kind ); }
158
159   /////////////////////////////////////////////////////////////////
160 } // namespace zypp
161 ///////////////////////////////////////////////////////////////////
162 #endif // ZYPP_NAMEKINDPROXY_H