backup
[platform/upstream/libzypp.git] / zypp / PoolItem.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/PoolItem.h
10  *
11 */
12 #ifndef ZYPP_POOLITEM_H
13 #define ZYPP_POOLITEM_H
14
15 #include <iosfwd>
16 #include <functional>
17
18 #include "zypp/base/PtrTypes.h"
19 #include "zypp/base/SafeBool.h"
20 #include "zypp/ResObject.h"
21 #include "zypp/ResStatus.h"
22
23 ///////////////////////////////////////////////////////////////////
24 namespace zypp
25 { /////////////////////////////////////////////////////////////////
26
27   class ResPool;
28
29   namespace pool
30   {
31     class PoolImpl;
32   }
33
34   ///////////////////////////////////////////////////////////////////
35   //
36   //    CLASS NAME : PoolItem
37   //
38   /** Reference to a PoolItem connecting ResObject and ResStatus.
39    *
40    * The "real" PoolItem is usg. somewhere in the ResPool. This is
41    * a reference to it. All copies made will reference (and modify)
42    * the same PoolItem. All changes via a PoolItem are immediately
43    * visible in all copies (now COW).
44    *
45    * \note Constness: Like pointer types, a <tt>const PoolItem</tt>
46    * does \b not refer to a <tt>const PoolItem</tt>. The reference is
47    * \c const, i.e. you can't change the refered PoolItem. The PoolItem
48    * (i.e. the status) is always mutable.
49    *
50   */
51   class PoolItem : protected base::SafeBool<PoolItem>
52   {
53     friend std::ostream & operator<<( std::ostream & str, const PoolItem & obj );
54
55     public:
56       /** Implementation */
57       class Impl;
58
59     public:
60       /** Default ctor for use in std::container. */
61       PoolItem();
62
63       /** Ctor looking up the \ref sat::Solvable in the \ref ResPool. */
64       explicit PoolItem( const sat::Solvable & solvable_r );
65
66       /** Ctor looking up the \ref ResObject in the \ref ResPool. */
67       explicit PoolItem( const ResObject::constPtr & resolvable_r );
68
69       /** Dtor */
70       ~PoolItem();
71
72     public:
73       /** \name Status related methods. */
74       //@{
75       /** Returns the current status. */
76       ResStatus & status() const;
77
78       /** Reset status. */
79       ResStatus & statusReset() const;
80
81
82       /** \name Status validation.
83        * Performed for non-packages.
84       */
85       //@{
86       /** No validation is performed for packages. */
87       bool isUndetermined() const;
88
89       /** Returns true if the solvable is relevant which means e.g. for patches
90        *  that at least one package of the patch is installed.
91        */
92       bool isRelevant() const;
93
94       /** Whether a relevant items requirements are met. */
95       bool isSatisfied() const;
96
97       /** Whether a relevant items requirements are broken. */
98       bool isBroken() const;
99
100       /** This includes \c unlocked broken patches, as well as those already
101        * selected to be installed (otherwise classified as \c satisfied).
102        */
103       bool isNeeded() const;
104
105       /** Broken (needed) but locked patches. */
106       bool isUnwanted() const;
107       //@}
108
109       //@}
110     public:
111       /** Return the \ref ResPool the item belongs to. */
112       ResPool pool() const;
113
114       /** Return the corresponding \ref sat::Solvable. */
115       sat::Solvable satSolvable() const
116       { return resolvable() ? resolvable()->satSolvable() : sat::Solvable::noSolvable; }
117
118       /** Return the buddy we share our status object with.
119        * A \ref Product e.g. may share it's status with an associated reference \ref Package.
120       */
121       sat::Solvable buddy() const;
122
123     public:
124       /** Returns the ResObject::constPtr.
125        * \see \ref operator->
126        */
127       ResObject::constPtr resolvable() const;
128
129       /** Implicit conversion into ResObject::constPtr to
130        *  support query filters operating on ResObject.
131        */
132       operator ResObject::constPtr() const
133       { return resolvable(); }
134
135       /** Forward \c -> access to ResObject. */
136       ResObject::constPtr operator->() const
137       { return resolvable(); }
138
139       /** Conversion to bool to allow pointer style tests
140        *  for nonNULL \ref resolvable. */
141       using base::SafeBool<PoolItem>::operator bool_type;
142
143     private:
144       friend class Impl;
145       friend class pool::PoolImpl;
146       /** \ref PoolItem generator for \ref pool::PoolImpl. */
147       static PoolItem makePoolItem( const sat::Solvable & solvable_r );
148       /** Buddies are set by \ref pool::PoolImpl.*/
149       void setBuddy( sat::Solvable solv_r );
150       /** internal ctor */
151       explicit PoolItem( Impl * implptr_r );
152       /** Pointer to implementation */
153       RW_pointer<Impl> _pimpl;
154
155   private:
156     friend SafeBool<PoolItem>::operator bool_type() const;
157     bool boolTest() const
158     { return !!resolvable(); }
159
160     private:
161       /** \name tmp hack for save/restore state. */
162       /** \todo get rid of it. */
163       //@{
164       friend class PoolItemSaver;
165       void saveState() const;
166       void restoreState() const;
167       bool sameState() const;
168       //@}
169   };
170   ///////////////////////////////////////////////////////////////////
171
172   /** \relates PoolItem Stream output */
173   std::ostream & operator<<( std::ostream & str, const PoolItem & obj );
174
175   /** \relates PoolItem */
176   inline bool operator==( const PoolItem & lhs, const PoolItem & rhs )
177   { return lhs.resolvable() == rhs.resolvable(); }
178
179   /** \relates PoolItem */
180   inline bool operator==( const PoolItem & lhs, const ResObject::constPtr & rhs )
181   { return lhs.resolvable() == rhs; }
182
183   /** \relates PoolItem */
184   inline bool operator==( const ResObject::constPtr & lhs, const PoolItem & rhs )
185   { return lhs == rhs.resolvable(); }
186
187
188   /** \relates PoolItem */
189   inline bool operator!=( const PoolItem & lhs, const PoolItem & rhs )
190   { return ! (lhs==rhs); }
191
192   /** \relates PoolItem */
193   inline bool operator!=( const PoolItem & lhs, const ResObject::constPtr & rhs )
194   { return ! (lhs==rhs); }
195
196   /** \relates PoolItem */
197   inline bool operator!=( const ResObject::constPtr & lhs, const PoolItem & rhs )
198   { return ! (lhs==rhs); }
199
200
201   /** \relates PoolItem Test for same content. */
202   inline bool identical( const PoolItem & lhs, const PoolItem & rhs )
203   { return lhs == rhs || lhs.satSolvable().identical( rhs.satSolvable() ); }
204
205   /** \relates PoolItem Test for same content. */
206   inline bool identical( const PoolItem & lhs, sat::Solvable rhs )
207   { return lhs.satSolvable().identical( rhs ); }
208
209   /** \relates PoolItem Test for same content. */
210   inline bool identical( sat::Solvable lhs, const PoolItem & rhs )
211   { return lhs.identical( rhs.satSolvable() ); }
212
213
214   /** \relates PoolItem Test for same name version release and arch. */
215   inline bool sameNVRA( const PoolItem & lhs, const PoolItem & rhs )
216   { return lhs == rhs || lhs.satSolvable().sameNVRA( rhs.satSolvable() ); }
217
218   /** \relates PoolItem Test for same name version release and arch. */
219   inline bool sameNVRA( const PoolItem & lhs, sat::Solvable rhs )
220   { return lhs.satSolvable().sameNVRA( rhs ); }
221
222   /** \relates PoolItem Test for same name version release and arch. */
223   inline bool sameNVRA( sat::Solvable lhs, const PoolItem & rhs )
224   { return lhs.sameNVRA( rhs.satSolvable() ); }
225
226   /** Solvable to PoolItem transform functor.
227    * \relates PoolItem
228    * \relates sat::SolvIterMixin
229    */
230   struct asPoolItem
231   {
232     typedef PoolItem result_type;
233
234     PoolItem operator()( const sat::Solvable & solv_r ) const
235     { return PoolItem( solv_r ); }
236   };
237
238   /////////////////////////////////////////////////////////////////
239 } // namespace zypp
240 ///////////////////////////////////////////////////////////////////
241 ///////////////////////////////////////////////////////////////////
242 namespace std
243 { /////////////////////////////////////////////////////////////////
244
245   /** \relates zypp::PoolItem Order in std::container follows ResObject::constPtr.*/
246   template<>
247     inline bool less<zypp::PoolItem>::operator()( const zypp::PoolItem & lhs, const zypp::PoolItem & rhs ) const
248     { return lhs.resolvable() < rhs.resolvable(); }
249
250   /////////////////////////////////////////////////////////////////
251 } // namespace zypp
252 ///////////////////////////////////////////////////////////////////
253 #endif // ZYPP_POOLITEM_H