Imported Upstream version 15.0.0
[platform/upstream/libzypp.git] / zypp / PoolItem.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/PoolItem.cc
10  *
11 */
12 #include <iostream>
13 #include "zypp/base/Logger.h"
14 #include "zypp/base/DefaultIntegral.h"
15
16 #include "zypp/PoolItem.h"
17 #include "zypp/ResPool.h"
18 #include "zypp/Package.h"
19 #include "zypp/VendorAttr.h"
20
21 using std::endl;
22
23 ///////////////////////////////////////////////////////////////////
24 namespace zypp
25 { /////////////////////////////////////////////////////////////////
26
27   ///////////////////////////////////////////////////////////////////
28   //
29   //    CLASS NAME : PoolItem::Impl
30   //
31   /** PoolItem implementation.
32    * \c _buddy handling:
33    * \li \c ==0 no buddy
34    * \li \c >0 this uses \c _buddy status
35    * \li \c <0 this status used by \c -_buddy
36    */
37   struct PoolItem::Impl
38   {
39     public:
40       Impl() {}
41
42       Impl( ResObject::constPtr res_r,
43             const ResStatus & status_r )
44       : _status( status_r )
45       , _resolvable( res_r )
46       {}
47
48       ResStatus & status() const
49       { return _buddy > 0 ? PoolItem(buddy()).status() : _status; }
50
51       sat::Solvable buddy() const
52       {
53         if ( !_buddy )
54           return sat::Solvable::noSolvable;
55         if ( _buddy < 0 )
56           return sat::Solvable( -_buddy );
57         return sat::Solvable( _buddy );
58       }
59
60       void setBuddy( sat::Solvable solv_r );
61
62       ResObject::constPtr resolvable() const
63       { return _resolvable; }
64
65       ResStatus & statusReset() const
66       {
67         _status.setLock( false, zypp::ResStatus::USER );
68         _status.resetTransact( zypp::ResStatus::USER );
69         return _status;
70       }
71
72     public:
73       bool isUndetermined() const
74       {
75           return status().isUndetermined();
76       }
77
78       bool isRelevant() const
79       {
80           return !status().isNonRelevant();
81       }
82
83       bool isSatisfied() const
84       {
85           return status().isSatisfied();
86       }
87
88       bool isBroken() const
89       {
90           return status().isBroken();
91       }
92
93       bool isNeeded() const
94       {
95         return status().isToBeInstalled() || ( isBroken() && ! status().isLocked() );
96       }
97
98       bool isUnwanted() const
99       {
100         return isBroken() && status().isLocked();
101       }
102
103     private:
104       mutable ResStatus     _status;
105       ResObject::constPtr   _resolvable;
106       DefaultIntegral<sat::detail::IdType,sat::detail::noId> _buddy;
107
108     /** \name Poor man's save/restore state.
109        * \todo There may be better save/restore state strategies.
110      */
111     //@{
112     public:
113       void saveState() const
114       { _savedStatus = status(); }
115       void restoreState() const
116       { status() = _savedStatus; }
117       bool sameState() const
118       {
119         if ( status() == _savedStatus )
120           return true;
121         // some bits changed...
122         if ( status().getTransactValue() != _savedStatus.getTransactValue()
123              && ( ! status().isBySolver() // ignore solver state changes
124                   // removing a user lock also goes to bySolver
125                   || _savedStatus.getTransactValue() == ResStatus::LOCKED ) )
126           return false;
127         if ( status().isLicenceConfirmed() != _savedStatus.isLicenceConfirmed() )
128           return false;
129         return true;
130       }
131     private:
132       mutable ResStatus _savedStatus;
133     //@}
134
135     public:
136       /** Offer default Impl. */
137       static shared_ptr<Impl> nullimpl()
138       {
139         static shared_ptr<Impl> _nullimpl( new Impl );
140         return _nullimpl;
141       }
142   };
143   ///////////////////////////////////////////////////////////////////
144
145   /** \relates PoolItem::Impl Stream output */
146   inline std::ostream & operator<<( std::ostream & str, const PoolItem::Impl & obj )
147   {
148     str << obj.status();
149     if (obj.resolvable())
150         str << *obj.resolvable();
151     else
152         str << "(NULL)";
153     return str;
154   }
155
156   inline void PoolItem::Impl::setBuddy( sat::Solvable solv_r )
157   {
158     PoolItem myBuddy( solv_r );
159     if ( myBuddy )
160     {
161       myBuddy._pimpl->_buddy = -resolvable()->satSolvable().id();
162       _buddy = myBuddy.satSolvable().id();
163       DBG << *this << " has buddy " << myBuddy << endl;
164     }
165   }
166
167   ///////////////////////////////////////////////////////////////////
168   //
169   //    CLASS NAME : PoolItem
170   //
171   ///////////////////////////////////////////////////////////////////
172
173   ///////////////////////////////////////////////////////////////////
174   //
175   //    METHOD NAME : PoolItem::PoolItem
176   //    METHOD TYPE : Ctor
177   //
178   PoolItem::PoolItem()
179   : _pimpl( Impl::nullimpl() )
180   {}
181
182   ///////////////////////////////////////////////////////////////////
183   //
184   //    METHOD NAME : PoolItem::PoolItem
185   //    METHOD TYPE : Ctor
186   //
187   PoolItem::PoolItem( const sat::Solvable & solvable_r )
188   : _pimpl( ResPool::instance().find( solvable_r )._pimpl )
189   {}
190
191   ///////////////////////////////////////////////////////////////////
192   //
193   //    METHOD NAME : PoolItem::PoolItem
194   //    METHOD TYPE : Ctor
195   //
196   PoolItem::PoolItem( const ResObject::constPtr & resolvable_r )
197   : _pimpl( ResPool::instance().find( resolvable_r )._pimpl )
198   {}
199
200   ///////////////////////////////////////////////////////////////////
201   //
202   //    METHOD NAME : PoolItem::PoolItem
203   //    METHOD TYPE : Ctor
204   //
205   PoolItem::PoolItem( Impl * implptr_r )
206   : _pimpl( implptr_r )
207   {}
208
209   ///////////////////////////////////////////////////////////////////
210   //
211   //    METHOD NAME : PoolItem::makePoolItem
212   //    METHOD TYPE : PoolItem
213   //
214   PoolItem PoolItem::makePoolItem( const sat::Solvable & solvable_r )
215   {
216     return PoolItem( new Impl( makeResObject( solvable_r ), solvable_r.isSystem() ) );
217   }
218
219   ///////////////////////////////////////////////////////////////////
220   //
221   //    METHOD NAME : PoolItem::~PoolItem
222   //    METHOD TYPE : Dtor
223   //
224   PoolItem::~PoolItem()
225   {}
226
227   ///////////////////////////////////////////////////////////////////
228   //
229   //    METHOD NAME : PoolItem::pool
230   //    METHOD TYPE : ResPool
231   //
232   ResPool PoolItem::pool() const
233   { return ResPool::instance(); }
234
235   ///////////////////////////////////////////////////////////////////
236   //
237   //    Forward to Impl:
238   //
239   ///////////////////////////////////////////////////////////////////
240
241   ResStatus & PoolItem::status() const
242   { return _pimpl->status(); }
243
244   ResStatus & PoolItem::statusReset() const
245   { return _pimpl->statusReset(); }
246
247   sat::Solvable PoolItem::buddy() const
248   { return _pimpl->buddy(); }
249
250   void PoolItem::setBuddy( sat::Solvable solv_r )
251   { _pimpl->setBuddy( solv_r ); }
252
253   bool PoolItem::isUndetermined() const
254   { return _pimpl->isUndetermined(); }
255
256   bool PoolItem::isRelevant() const
257   { return _pimpl->isRelevant(); }
258
259   bool PoolItem::isSatisfied() const
260   { return _pimpl->isSatisfied(); }
261
262   bool PoolItem::isBroken() const
263   { return _pimpl->isBroken(); }
264
265   bool PoolItem::isNeeded() const
266   { return _pimpl->isNeeded(); }
267
268   bool PoolItem::isUnwanted() const
269   { return _pimpl->isUnwanted(); }
270
271   void PoolItem::saveState() const
272   { _pimpl->saveState(); }
273
274   void PoolItem::restoreState() const
275   { _pimpl->restoreState(); }
276
277   bool PoolItem::sameState() const
278   { return _pimpl->sameState(); }
279
280   ResObject::constPtr PoolItem::resolvable() const
281   { return _pimpl->resolvable(); }
282
283   /******************************************************************
284    **
285    **   FUNCTION NAME : operator<<
286    **   FUNCTION TYPE : std::ostream &
287   */
288   std::ostream & operator<<( std::ostream & str, const PoolItem & obj )
289   {
290     return str << *obj._pimpl;
291   }
292
293   /////////////////////////////////////////////////////////////////
294 } // namespace zypp
295 ///////////////////////////////////////////////////////////////////