Imported Upstream version 17.23.0
[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
21 #include "zypp/sat/SolvableType.h"
22 #include "zypp/ResStatus.h"
23
24 ///////////////////////////////////////////////////////////////////
25 namespace zypp
26 {
27   class ResPool;
28   namespace pool
29   {
30     class PoolImpl;
31   }
32   ///////////////////////////////////////////////////////////////////
33   /// \class PoolItem
34   /// \brief Combining \ref sat::Solvable and \ref ResStatus.
35   ///
36   /// The "real" PoolItem is usually somewhere in the ResPool. This is
37   /// a reference to it. All copies made will reference (and modify)
38   /// the same PoolItem. All changes via a PoolItem are immediately
39   /// visible in all copies (now COW).
40   ///
41   /// \note PoolItem is a SolvableType, which provides direct access to
42   /// many of the underlying sat::Solvables properties.
43   /// \see \ref sat::SolvableType
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   class PoolItem : public sat::SolvableType<PoolItem>
51   {
52     friend std::ostream & operator<<( std::ostream & str, const PoolItem & obj );
53     public:
54       /** Default ctor for use in std::container. */
55       PoolItem();
56
57       /** Ctor looking up the \ref sat::Solvable in the \ref ResPool. */
58       explicit PoolItem( const sat::Solvable & solvable_r );
59
60       /** Ctor looking up the \ref sat::Solvable in the \ref ResPool. */
61       template <class Derived>
62       explicit PoolItem( const SolvableType<Derived> & solvable_r )
63       : PoolItem( solvable_r.satSolvable() )
64       {}
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
83       /** \name Status validation.
84        * Performed for non-packages.
85       */
86       //@{
87       /** No validation is performed for packages. */
88       bool isUndetermined() const;
89
90       /** Returns true if the solvable is relevant which means e.g. for patches
91        *  that at least one package of the patch is installed.
92        */
93       bool isRelevant() const;
94
95       /** Whether a relevant items requirements are met. */
96       bool isSatisfied() const;
97
98       /** Whether a relevant items requirements are broken. */
99       bool isBroken() const;
100
101       /** This includes \c unlocked broken patches, as well as those already
102        * selected to be installed (otherwise classified as \c satisfied).
103        */
104       bool isNeeded() const;
105
106       /** Broken (needed) but locked patches. */
107       bool isUnwanted() const;
108       //@}
109
110     public:
111       /** Return the \ref ResPool the item belongs to. */
112       ResPool pool() const;
113
114       /** This is a \ref sat::SolvableType. */
115       explicit operator sat::Solvable() 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     private:
140       friend class pool::PoolImpl;
141       /** \ref PoolItem generator for \ref pool::PoolImpl. */
142       static PoolItem makePoolItem( const sat::Solvable & solvable_r );
143       /** Buddies are set by \ref pool::PoolImpl.*/
144       void setBuddy( const sat::Solvable & solv_r );
145       /** internal ctor */
146     public:
147       struct Impl;      ///< Expose type only
148     private:
149       explicit PoolItem( Impl * implptr_r );
150       /** Pointer to implementation */
151       RW_pointer<Impl> _pimpl;
152
153     private:
154       /** \name tmp hack for save/restore state. */
155       /** \todo get rid of it. */
156       //@{
157       friend struct PoolItemSaver;
158       void saveState() const;
159       void restoreState() const;
160       bool sameState() const;
161       //@}
162   };
163   ///////////////////////////////////////////////////////////////////
164
165   /** \relates PoolItem Stream output */
166   std::ostream & operator<<( std::ostream & str, const PoolItem & obj );
167
168
169   /** \relates PoolItem Required to disambiguate vs. (PoolItem,ResObject::constPtr) due to implicit PoolItem::operator ResObject::constPtr  */
170   inline bool operator==( const PoolItem & lhs, const PoolItem & rhs )
171   { return lhs.resolvable() == rhs.resolvable(); }
172
173   /** \relates PoolItem Convenience compare */
174   inline bool operator==( const PoolItem & lhs, const ResObject::constPtr & rhs )
175   { return lhs.resolvable() == rhs; }
176
177   /** \relates PoolItem Convenience compare */
178   inline bool operator==( const ResObject::constPtr & lhs, const PoolItem & rhs )
179   { return lhs == rhs.resolvable(); }
180
181
182   /** \relates PoolItem Required to disambiguate vs. (PoolItem,ResObject::constPtr) due to implicit PoolItem::operator ResObject::constPtr */
183   inline bool operator!=( const PoolItem & lhs, const PoolItem & rhs )
184   { return ! (lhs==rhs); }
185
186   /** \relates PoolItem Convenience compare */
187   inline bool operator!=( const PoolItem & lhs, const ResObject::constPtr & rhs )
188   { return ! (lhs==rhs); }
189
190   /** \relates PoolItem Convenience compare */
191   inline bool operator!=( const ResObject::constPtr & lhs, const PoolItem & rhs )
192   { return ! (lhs==rhs); }
193
194   /** Solvable to PoolItem transform functor.
195    * \relates PoolItem
196    * \relates sat::SolvIterMixin
197    */
198   struct asPoolItem
199   {
200     typedef PoolItem result_type;
201
202     PoolItem operator()( const sat::Solvable & solv_r ) const
203     { return PoolItem( solv_r ); }
204   };
205
206 } // namespace zypp
207 ///////////////////////////////////////////////////////////////////
208 #endif // ZYPP_POOLITEM_H