1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ResStatus.h
12 #ifndef ZYPP_RESSTATUS_H
13 #define ZYPP_RESSTATUS_H
19 ///////////////////////////////////////////////////////////////////
21 { /////////////////////////////////////////////////////////////////
25 class UserLockQueryManip;
29 ///////////////////////////////////////////////////////////////////
31 // CLASS NAME : ResStatus
35 * \li \c StateField Whether the resolvable is or uninstalled (available).
36 * \li \c ValidateField Validate status computed by the solver as
37 * nonrelevant: it is unimportant for the user
38 * satisfied: it important nothing has to be done
39 * broken: it is incomplete. So e.g. an update is needed
40 * \li \c TransactField Whether to transact this resolvable
41 * (delete if installed install if uninstalled).
42 * In case the resolvable is locked, only USER may modify the
44 * \li \c TransactByField Who triggered the transaction. Transaction
45 * bit may be reset by higer levels only.
46 * \li \c TransactDetailField Reason why the Resolvable transacts.
47 * Splitted into \c InstallDetailValue and \c RemoveDetailValue
48 * dependent on the kind of transaction.
49 * \li \c WeakField The solvable will be recommended/suggested by
50 * a to be installed/deleted solvable.
55 friend std::ostream & operator<<( std::ostream & str, const ResStatus & obj );
56 friend bool operator==( const ResStatus & lhs, const ResStatus & rhs );
59 /** \name BitField range definitions.
61 * \note Enlarge FieldType if more bit's needed. It's not yet
62 * checked by the compiler.
65 typedef uint16_t FieldType;
66 typedef bit::BitField<FieldType> BitFieldType;
67 // Bit Ranges within FieldType defined by 1st bit and size:
68 typedef bit::Range<FieldType,0, 1> StateField;
69 typedef bit::Range<FieldType,StateField::end, 2> ValidateField;
70 typedef bit::Range<FieldType,ValidateField::end, 2> TransactField;
71 typedef bit::Range<FieldType,TransactField::end, 2> TransactByField;
72 typedef bit::Range<FieldType,TransactByField::end, 2> TransactDetailField;
73 typedef bit::Range<FieldType,TransactDetailField::end, 1> LicenceConfirmedField;
74 typedef bit::Range<FieldType,LicenceConfirmedField::end, 4> WeakField;
75 typedef bit::Range<FieldType,WeakField::end, 1> UserLockQueryField; // internal
76 // enlarge FieldType if more bit's needed. It's not yet
77 // checked by the compiler.
81 /** \name Status values.
83 * Each enum corresponds to a BitField range.
84 * \note Take care that enumerator values actually fit into
85 * the corresponding field. It's not yet checked by the compiler.
90 UNINSTALLED = bit::RangeValue<StateField,0>::value,
91 INSTALLED = bit::RangeValue<StateField,1>::value
95 UNDETERMINED = bit::RangeValue<ValidateField,0>::value,
96 BROKEN = bit::RangeValue<ValidateField,1>::value,
97 SATISFIED = bit::RangeValue<ValidateField,2>::value,
98 NONRELEVANT = bit::RangeValue<ValidateField,3>::value
102 KEEP_STATE = bit::RangeValue<TransactField,0>::value,
103 LOCKED = bit::RangeValue<TransactField,1>::value, // locked, must not transact
104 TRANSACT = bit::RangeValue<TransactField,2>::value // transact according to state
108 SOLVER = bit::RangeValue<TransactByField,0>::value,
109 APPL_LOW = bit::RangeValue<TransactByField,1>::value,
110 APPL_HIGH = bit::RangeValue<TransactByField,2>::value,
111 USER = bit::RangeValue<TransactByField,3>::value
116 /** Detail for no transact, i.e. reset any Install/RemoveDetailValue. */
117 NO_DETAIL = bit::RangeValue<TransactDetailField,0>::value,
119 enum InstallDetailValue
121 EXPLICIT_INSTALL = bit::RangeValue<TransactDetailField,0>::value,
122 SOFT_INSTALL = bit::RangeValue<TransactDetailField,1>::value
124 enum RemoveDetailValue
126 EXPLICIT_REMOVE = bit::RangeValue<TransactDetailField,0>::value,
127 SOFT_REMOVE = bit::RangeValue<TransactDetailField,1>::value,
128 DUE_TO_OBSOLETE = bit::RangeValue<TransactDetailField,2>::value,
129 DUE_TO_UPGRADE = bit::RangeValue<TransactDetailField,3>::value
132 enum LicenceConfirmedValue
134 LICENCE_UNCONFIRMED = bit::RangeValue<LicenceConfirmedField,0>::value,
135 LICENCE_CONFIRMED = bit::RangeValue<LicenceConfirmedField,1>::value
138 enum WeakValue // Unlike the other fields those are BITS that may be or'ed!
140 NO_WEAK = bit::RangeValue<WeakField,0>::value,
141 SUGGESTED = bit::RangeValue<WeakField,1<<0>::value,
142 RECOMMENDED = bit::RangeValue<WeakField,1<<1>::value,
143 ORPHANED = bit::RangeValue<WeakField,1<<2>::value,
144 UNNEEDED = bit::RangeValue<WeakField,1<<3>::value
147 enum UserLockQuery // internal
149 USERLOCK_NOMATCH = bit::RangeValue<UserLockQueryField,0>::value,
150 USERLOCK_MATCH = bit::RangeValue<UserLockQueryField,1>::value
159 /** Ctor setting the initial . */
160 ResStatus( bool isInstalled_r );
165 /** Debug helper returning the bitfield.
166 * It's save to expose the bitfield, as it can't be used to
167 * recreate a ResStatus. So it is not possible to bypass
170 BitFieldType bitfield() const
171 { return _bitfield; }
175 bool isLicenceConfirmed() const
176 { return fieldValueIs<LicenceConfirmedField>( LICENCE_CONFIRMED ); }
178 void setLicenceConfirmed( bool toVal_r = true )
179 { fieldValueAssign<LicenceConfirmedField>( toVal_r ? LICENCE_CONFIRMED : LICENCE_UNCONFIRMED ); }
182 bool isRecommended() const
183 { return _bitfield.test( RECOMMENDED ); }
185 bool isSuggested() const
186 { return _bitfield.test( SUGGESTED ); }
188 bool isOrphaned() const
189 { return _bitfield.test( ORPHANED ); }
191 bool isUnneeded() const
192 { return _bitfield.test( UNNEEDED ); }
195 { return ! fieldValueIs<WeakField>( NO_WEAK ); }
198 { return fieldValueAssign<WeakField>( NO_WEAK ); }
200 void setRecommended( bool toVal_r = true )
201 { _bitfield.set( RECOMMENDED, toVal_r ); }
203 void setSuggested( bool toVal_r = true )
204 { _bitfield.set( SUGGESTED, toVal_r ); }
206 void setOrphaned( bool toVal_r = true )
207 { _bitfield.set( ORPHANED, toVal_r ); }
209 void setUnneeded( bool toVal_r = true )
210 { _bitfield.set( UNNEEDED, toVal_r ); }
213 ValidateValue validate() const
214 { return (ValidateValue)_bitfield.value<ValidateField>(); }
216 bool isUndetermined() const
217 { return fieldValueIs<ValidateField>( UNDETERMINED ); }
219 bool isSatisfied() const
220 { return fieldValueIs<ValidateField>( SATISFIED ); }
222 bool isBroken() const
223 { return fieldValueIs<ValidateField>( BROKEN ); }
225 bool isNonRelevant() const
226 { return fieldValueIs<ValidateField>( NONRELEVANT ); }
229 // These two are IMMUTABLE!
231 bool isInstalled() const
232 { return fieldValueIs<StateField>( INSTALLED ); }
234 bool isUninstalled() const
235 { return fieldValueIs<StateField>( UNINSTALLED ); }
239 bool staysInstalled() const
240 { return isInstalled() && !transacts(); }
242 bool wasInstalled() const { return staysInstalled(); } //for old status
244 bool isToBeInstalled() const
245 { return isUninstalled() && transacts(); }
247 bool staysUninstalled() const
248 { return isUninstalled() && !transacts(); }
250 bool wasUninstalled() const { return staysUninstalled(); } // for old status
252 bool isToBeUninstalled() const
253 { return isInstalled() && transacts(); }
255 bool isLocked() const
256 { return fieldValueIs<TransactField>( LOCKED ); }
258 bool isUserLocked() const
259 { return isLocked() && isByUser(); }
261 bool isSoftLocked() const
262 { return isKept() && ( isByApplLow() || isByUser() ); }
265 { return fieldValueIs<TransactField>( KEEP_STATE ); }
267 bool transacts() const
268 { return fieldValueIs<TransactField>( TRANSACT ); }
270 TransactValue getTransactValue() const
271 { return (TransactValue)_bitfield.value<TransactField>(); }
273 /** True if would be on system after commit. */
274 bool onSystem() const
275 { return( isInstalled() != transacts() ); }
277 /** True if would be off system after commit. */
278 bool offSystem() const
279 { return ! onSystem(); }
281 bool isBySolver() const
282 { return fieldValueIs<TransactByField>( SOLVER ); }
284 bool isByApplLow() const
285 { return fieldValueIs<TransactByField>( APPL_LOW ); }
287 bool isByApplHigh() const
288 { return fieldValueIs<TransactByField>( APPL_HIGH ); }
290 bool isByUser() const
291 { return fieldValueIs<TransactByField>( USER ); }
293 TransactByValue getTransactByValue() const
294 { return (TransactByValue)_bitfield.value<TransactByField>(); }
296 bool setTransactByValue(TransactByValue causer)
298 if ( isLessThan<TransactByField>( causer ) ) {
299 fieldValueAssign<TransactByField>( causer );
306 bool isToBeUninstalledDueToObsolete () const
307 { return isToBeUninstalled() && fieldValueIs<TransactDetailField>( DUE_TO_OBSOLETE ); }
309 bool isToBeUninstalledDueToUpgrade() const
310 { return isToBeUninstalled() && fieldValueIs<TransactDetailField>( DUE_TO_UPGRADE ); }
312 bool isToBeInstalledSoft () const
313 { return isToBeInstalled() && fieldValueIs<TransactDetailField>( SOFT_INSTALL ); }
315 bool isToBeInstalledNotSoft () const
316 { return isToBeInstalled() && !fieldValueIs<TransactDetailField>( SOFT_INSTALL ); }
318 bool isToBeUninstalledSoft () const
319 { return isToBeUninstalled() && fieldValueIs<TransactDetailField>( SOFT_REMOVE ); }
323 /** \name Internal hard lock maintainance */
325 friend class resstatus::UserLockQueryManip;
327 bool isUserLockQueryMatch() const
328 { return fieldValueIs<UserLockQueryField>( USERLOCK_MATCH ); }
330 void setUserLockQueryMatch( bool match_r )
331 { fieldValueAssign<UserLockQueryField>( match_r ? USERLOCK_MATCH : USERLOCK_NOMATCH ); }
336 //------------------------------------------------------------------------
337 // get/set functions, returnig \c true if requested status change
338 // was successfull (i.e. leading to the desired transaction).
339 // If a lower level (e.g.SOLVER) wants to transact, but it's
340 // already set by a higher level, \c true should be returned.
341 // Removing a higher levels transaction bit should fail.
343 // The may functions checks only, if the action would return true
346 /** Set TransactValue.
347 * Convenience to set TransactValue from enum.
349 bool setTransactValue( TransactValue newVal_r, TransactByValue causer_r )
354 return setTransact( false, causer_r );
357 return setLock( true, causer_r );
360 return setTransact( true, causer_r );
366 bool maySetTransactValue( TransactValue newVal_r, TransactByValue causer_r )
368 bit::BitField<FieldType> savBitfield = _bitfield;
369 bool ret = setTransactValue( newVal_r, causer_r );
370 _bitfield = savBitfield;
374 /** Apply a lock (prevent transaction).
375 * Currently by USER or APPL_HIGH only, but who knows...
376 * Set LOCKED from KEEP_STATE to be shure all transaction
377 * details were reset properly.
379 bool setLock( bool toLock_r, TransactByValue causer_r )
381 if ( toLock_r == isLocked() )
383 // we're already in the desired state, but in case of
384 // LOCKED, remember a superior causer.
385 if ( isLocked() && isLessThan<TransactByField>( causer_r ) )
386 fieldValueAssign<TransactByField>( causer_r );
389 // Here: Lock status is to be changed:
390 if ( causer_r != USER && causer_r != APPL_HIGH )
393 // We're in unlocked state, which includes TRANSACT.
394 // Causer must be allowed to reset this. But from
395 // KEEP_STATE every causer is allowed to set the lock.
396 if ( ! setTransact( false, causer_r ) )
398 fieldValueAssign<TransactField>( LOCKED );
399 fieldValueAssign<TransactByField>( causer_r );
401 // To leave Locked state it needs a superior causer.
402 if ( isGreaterThan<TransactByField>( causer_r ) )
404 fieldValueAssign<TransactField>( KEEP_STATE );
405 fieldValueAssign<TransactByField>( SOLVER ); // reset to lowest causer
406 // in order to distinguish from keep_state_by_user
411 bool maySetLock( bool to_r, TransactByValue causer_r )
413 bit::BitField<FieldType> savBitfield = _bitfield;
414 bool ret = setLock( to_r, causer_r );
415 _bitfield = savBitfield;
419 /** Toggle between TRANSACT and KEEP_STATE.
420 * LOCKED state means KEEP_STATE. But in contrary to KEEP_STATE,
421 * LOCKED state is immutable for \a causer_r less than TransactByValue.
422 * KEEP_STATE may be canged by any \a causer_r.
424 bool setTransact( bool toTansact_r, TransactByValue causer_r )
426 if ( toTansact_r == transacts() )
428 // we're already in the desired state, but in case of
429 // TRANSACT, remember a superior causer.
430 if ( transacts() && isLessThan<TransactByField>( causer_r ) )
431 fieldValueAssign<TransactByField>( causer_r );
433 fieldValueAssign<TransactDetailField>( NO_DETAIL ); // Details has to be set again
436 // Here: transact status is to be changed:
437 if ( ! fieldValueIs<TransactField>( KEEP_STATE )
438 && isGreaterThan<TransactByField>( causer_r ) ) {
444 fieldValueAssign<TransactField>( TRANSACT );
448 fieldValueAssign<TransactField>( KEEP_STATE );
450 fieldValueAssign<TransactDetailField>( NO_DETAIL ); // Details has to be set again
451 fieldValueAssign<TransactByField>( causer_r );
455 bool maySetTransact( bool val_r, TransactByValue causer )
457 bit::BitField<FieldType> savBitfield = _bitfield;
458 bool ret = setTransact (val_r, causer);
459 _bitfield = savBitfield;
464 bool setSoftLock( TransactByValue causer_r )
466 if ( ! setTransact( false, causer_r ) )
468 if ( fieldValueIs<TransactField>( KEEP_STATE )
469 && isLessThan<TransactByField>( causer_r ) )
470 fieldValueAssign<TransactByField>( causer_r );
474 /** Not the same as setTransact( false ).
476 bool resetTransact( TransactByValue causer_r )
478 if ( ! setTransact( false, causer_r ) )
480 if ( fieldValueIs<TransactField>( KEEP_STATE ) )
481 fieldValueAssign<TransactByField>( SOLVER );
485 /** Soft toggle between TRANSACT and KEEP_STATE.
486 * Similar to setTransact, but leaving KEEP_STATE also requires
487 * a superior \a causerLimit_r. So this is a kind of soft lock.
489 * // SOLVER wants to set TRANSACT, iff KEEP_STATE is
490 * // not superior to APPL_LOW.
491 * setSoftTransact( true, SOLVER, APPL_LOW );
494 bool setSoftTransact( bool toTansact_r, TransactByValue causer_r,
495 TransactByValue causerLimit_r )
497 if ( fieldValueIs<TransactField>( KEEP_STATE )
498 && toTansact_r != transacts()
499 && isGreaterThan<TransactByField>( causerLimit_r ) )
501 // any transact status change requires a superior causer.
504 return setTransact( toTansact_r, causer_r );
507 bool setSoftTransact( bool toTansact_r, TransactByValue causer_r )
508 { return setSoftTransact( toTansact_r, causer_r, causer_r ); }
510 bool maySetSoftTransact( bool val_r, TransactByValue causer,
511 TransactByValue causerLimit_r )
513 bit::BitField<FieldType> savBitfield = _bitfield;
514 bool ret = setSoftTransact( val_r, causer, causerLimit_r );
515 _bitfield = savBitfield;
519 bool maySetSoftTransact( bool val_r, TransactByValue causer )
520 { return maySetSoftTransact( val_r, causer, causer ); }
522 bool setToBeInstalled (TransactByValue causer)
524 if (isInstalled()) return false;
525 return setTransact (true, causer);
528 bool maySetToBeInstalled (TransactByValue causer)
530 bit::BitField<FieldType> savBitfield = _bitfield;
531 bool ret = setToBeInstalled (causer);
532 _bitfield = savBitfield;
536 bool setToBeUninstalled (TransactByValue causer)
538 if (!isInstalled()) return false;
539 return setTransact (true, causer);
542 bool maySetToBeUninstalled (TransactByValue causer)
544 bit::BitField<FieldType> savBitfield = _bitfield;
545 bool ret = setToBeUninstalled (causer);
546 _bitfield = savBitfield;
550 //------------------------------------------------------------------------
551 // *** These are only for the Resolver ***
553 bool setToBeUninstalledDueToObsolete ( )
555 if (!setToBeUninstalled (SOLVER)) return false;
556 fieldValueAssign<TransactDetailField>(DUE_TO_OBSOLETE);
560 bool setToBeUninstalledDueToUpgrade ( TransactByValue causer )
562 if (!setToBeUninstalled (causer)) return false;
563 fieldValueAssign<TransactDetailField>(DUE_TO_UPGRADE);
567 bool setToBeInstalledSoft ( )
570 || !setSoftTransact (true, SOLVER))
573 fieldValueAssign<TransactDetailField>(SOFT_INSTALL);
577 bool setToBeUninstalledSoft ( )
580 || !setSoftTransact (true, SOLVER))
583 fieldValueAssign<TransactDetailField>(SOFT_REMOVE);
587 bool maySetToBeUninstalledSoft ()
589 bit::BitField<FieldType> savBitfield = _bitfield;
590 bool ret = setToBeUninstalledSoft ();
591 _bitfield = savBitfield;
595 bool isSoftInstall () {
596 return fieldValueIs<TransactDetailField> (SOFT_INSTALL);
599 bool isSoftUninstall () {
600 return fieldValueIs<TransactDetailField> (SOFT_REMOVE);
603 bool setSoftInstall (bool flag) {
604 fieldValueAssign<TransactDetailField>(flag?SOFT_INSTALL:0);
608 bool setSoftUninstall (bool flag) {
609 fieldValueAssign<TransactDetailField>(flag?SOFT_REMOVE:0);
613 bool setUndetermined ()
615 fieldValueAssign<ValidateField>(UNDETERMINED);
621 fieldValueAssign<ValidateField>(SATISFIED);
627 fieldValueAssign<ValidateField>(BROKEN);
631 bool setNonRelevant ()
633 fieldValueAssign<ValidateField>(NONRELEVANT);
637 bool setStatus( ResStatus newStatus_r )
639 // State field is immutable!
640 if ( _bitfield.value<StateField>() != newStatus_r._bitfield.value<StateField>() )
642 // Transaction state change allowed?
643 if ( ! setTransactValue( newStatus_r.getTransactValue(), newStatus_r.getTransactByValue() ) )
646 // Ok, we take it all..
647 _bitfield = newStatus_r._bitfield;
651 /** \name Builtin ResStatus constants. */
653 static const ResStatus toBeInstalled;
654 static const ResStatus toBeUninstalled;
655 static const ResStatus toBeUninstalledDueToUpgrade;
656 static const ResStatus toBeUninstalledDueToObsolete;
660 /** Ctor for intialization of builtin constants. */
661 ResStatus( StateValue s,
662 ValidateValue v = UNDETERMINED,
663 TransactValue t = KEEP_STATE,
664 InstallDetailValue i = EXPLICIT_INSTALL,
665 RemoveDetailValue r = EXPLICIT_REMOVE );
667 /** Return whether the corresponding Field has value \a val_r.
669 template<class TField>
670 bool fieldValueIs( FieldType val_r ) const
671 { return _bitfield.isEqual<TField>( val_r ); }
673 /** Set the corresponding Field to value \a val_r.
675 template<class TField>
676 void fieldValueAssign( FieldType val_r )
677 { _bitfield.assign<TField>( val_r ); }
679 /** compare two values.
681 template<class TField>
682 bool isGreaterThan( FieldType val_r )
683 { return _bitfield.value<TField>() > val_r; }
685 template<class TField>
686 bool isLessThan( FieldType val_r )
687 { return _bitfield.value<TField>() < val_r; }
690 friend class resstatus::StatusBackup;
691 BitFieldType _bitfield;
693 ///////////////////////////////////////////////////////////////////
695 /** \relates ResStatus Stream output */
696 std::ostream & operator<<( std::ostream & str, const ResStatus & obj );
698 /** \relates ResStatus Stream output */
699 std::ostream & operator<<( std::ostream & str, ResStatus::TransactValue obj );
701 /** \relates ResStatus Stream output */
702 std::ostream & operator<<( std::ostream & str, ResStatus::TransactByValue obj );
704 /** \relates ResStatus */
705 inline bool operator==( const ResStatus & lhs, const ResStatus & rhs )
706 { return lhs._bitfield == rhs._bitfield; }
708 /** \relates ResStatus */
709 inline bool operator!=( const ResStatus & lhs, const ResStatus & rhs )
710 { return ! (lhs == rhs); }
712 ///////////////////////////////////////////////////////////////////
723 StatusBackup( ResStatus & status_r )
724 : _status( &status_r )
725 , _bitfield( _status->_bitfield )
729 { if ( _status ) _status->_bitfield = _bitfield; }
733 ResStatus::BitFieldType _bitfield;
737 /////////////////////////////////////////////////////////////////
739 ///////////////////////////////////////////////////////////////////
740 #endif // ZYPP_RESSTATUS_H