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