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 Wheter 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> SolverStateField;
74 typedef bit::Range<FieldType,SolverStateField::end, 1> LicenceConfirmedField;
75 typedef bit::Range<FieldType,LicenceConfirmedField::end, 2> WeakField;
76 typedef bit::Range<FieldType,WeakField::end, 1> UserLockQueryField; // internal
77 // enlarge FieldType if more bit's needed. It's not yet
78 // checked by the compiler.
82 /** \name Status values.
84 * Each enum corresponds to a BitField range.
85 * \note Take care that enumerator values actually fit into
86 * the corresponding field. It's not yet checked by the compiler.
91 UNINSTALLED = bit::RangeValue<StateField,0>::value,
92 INSTALLED = bit::RangeValue<StateField,1>::value
96 UNDETERMINED = bit::RangeValue<ValidateField,0>::value,
97 BROKEN = bit::RangeValue<ValidateField,1>::value,
98 SATISFIED = bit::RangeValue<ValidateField,2>::value,
99 NONRELEVANT = bit::RangeValue<ValidateField,3>::value
103 KEEP_STATE = bit::RangeValue<TransactField,0>::value,
104 LOCKED = bit::RangeValue<TransactField,1>::value, // locked, must not transact
105 TRANSACT = bit::RangeValue<TransactField,2>::value // transact according to state
109 SOLVER = bit::RangeValue<TransactByField,0>::value,
110 APPL_LOW = bit::RangeValue<TransactByField,1>::value,
111 APPL_HIGH = bit::RangeValue<TransactByField,2>::value,
112 USER = bit::RangeValue<TransactByField,3>::value
117 /** Detail for no transact, i.e. reset any Install/RemoveDetailValue. */
118 NO_DETAIL = bit::RangeValue<TransactDetailField,0>::value,
121 enum InstallDetailValue
123 EXPLICIT_INSTALL = bit::RangeValue<TransactDetailField,0>::value,
124 SOFT_INSTALL = bit::RangeValue<TransactDetailField,1>::value
126 enum RemoveDetailValue
128 EXPLICIT_REMOVE = bit::RangeValue<TransactDetailField,0>::value,
129 SOFT_REMOVE = bit::RangeValue<TransactDetailField,1>::value,
130 DUE_TO_UPGRADE = bit::RangeValue<TransactDetailField,2>::value
132 enum SolverStateValue
134 NORMAL = bit::RangeValue<SolverStateField,0>::value, // default, notthing special
135 SEEN = bit::RangeValue<SolverStateField,1>::value, // already seen during ResolverUpgrade
138 enum LicenceConfirmedValue
140 LICENCE_UNCONFIRMED = bit::RangeValue<LicenceConfirmedField,0>::value,
141 LICENCE_CONFIRMED = bit::RangeValue<LicenceConfirmedField,1>::value
146 NO_WEAK = bit::RangeValue<WeakField,0>::value,
147 SUGGESTED = bit::RangeValue<WeakField,1>::value,
148 RECOMMENDED = bit::RangeValue<WeakField,2>::value,
149 SUGGESTED_AND_RECOMMENDED = bit::RangeValue<WeakField,3>::value
152 enum UserLockQuery // internal
154 USERLOCK_NOMATCH = bit::RangeValue<UserLockQueryField,0>::value,
155 USERLOCK_MATCH = bit::RangeValue<UserLockQueryField,1>::value
164 /** Ctor setting the initial . */
165 ResStatus( bool isInstalled_r );
170 /** Debug helper returning the bitfield.
171 * It's save to expose the bitfield, as it can't be used to
172 * recreate a ResStatus. So it is not possible to bypass
175 BitFieldType bitfield() const
176 { return _bitfield; }
180 bool isLicenceConfirmed() const
181 { return fieldValueIs<LicenceConfirmedField>( LICENCE_CONFIRMED ); }
183 void setLicenceConfirmed( bool toVal_r = true )
184 { fieldValueAssign<LicenceConfirmedField>( toVal_r ? LICENCE_CONFIRMED : LICENCE_UNCONFIRMED ); }
188 bool isRecommended() const
189 { return fieldValueIs<WeakField>( RECOMMENDED ); }
191 bool isSuggested() const
192 { return fieldValueIs<WeakField>( SUGGESTED ); }
194 bool resetWeak() const
195 { return fieldValueIs<WeakField>( NO_WEAK ); }
197 void setRecommended( bool toVal_r = true )
199 fieldValueAssign<WeakField>( toVal_r ? SUGGESTED_AND_RECOMMENDED : SUGGESTED );
201 fieldValueAssign<WeakField>( toVal_r ? RECOMMENDED : NO_WEAK );
204 void setSuggested( bool toVal_r = true )
205 { if (isRecommended())
206 fieldValueAssign<WeakField>( toVal_r ? SUGGESTED_AND_RECOMMENDED : RECOMMENDED );
208 fieldValueAssign<WeakField>( toVal_r ? SUGGESTED : NO_WEAK );
211 ValidateValue validate() const
212 { return (ValidateValue)_bitfield.value<ValidateField>(); }
214 bool isUndetermined() const
215 { return fieldValueIs<ValidateField>( UNDETERMINED ); }
217 bool isSatisfied() const
218 { return fieldValueIs<ValidateField>( SATISFIED ); }
220 bool isBroken() const
221 { return fieldValueIs<ValidateField>( BROKEN ); }
223 bool isNonRelevant() const
224 { return fieldValueIs<ValidateField>( NONRELEVANT ); }
227 // These two are IMMUTABLE!
229 bool isInstalled() const
230 { return fieldValueIs<StateField>( INSTALLED ); }
232 bool isUninstalled() const
233 { return fieldValueIs<StateField>( UNINSTALLED ); }
237 bool staysInstalled() const
238 { return isInstalled() && !transacts(); }
240 bool wasInstalled() const { return staysInstalled(); } //for old status
242 bool isToBeInstalled() const
243 { return isUninstalled() && transacts(); }
245 bool staysUninstalled() const
246 { return isUninstalled() && !transacts(); }
248 bool wasUninstalled() const { return staysUninstalled(); } // for old status
250 bool isToBeUninstalled() const
251 { return isInstalled() && transacts(); }
253 bool isLocked() const
254 { return fieldValueIs<TransactField>( LOCKED ); }
256 bool isUserLocked() const
257 { return isLocked() && isByUser(); }
259 bool isSoftLocked( TransactByValue causer_r = USER ) const
260 { return isKept() && fieldValueIs<TransactByField>( causer_r ); }
263 { return fieldValueIs<TransactField>( KEEP_STATE ); }
265 bool transacts() const
266 { return fieldValueIs<TransactField>( TRANSACT ); }
268 TransactValue getTransactValue() const
269 { return (TransactValue)_bitfield.value<TransactField>(); }
271 /** True if would be on system after commit. */
272 bool onSystem() const
273 { return( isInstalled() != transacts() ); }
275 /** True if would be off system after commit. */
276 bool offSystem() const
277 { return ! onSystem(); }
279 bool isBySolver() const
280 { return fieldValueIs<TransactByField>( SOLVER ); }
282 bool isByApplLow() const
283 { return fieldValueIs<TransactByField>( APPL_LOW ); }
285 bool isByApplHigh() const
286 { return fieldValueIs<TransactByField>( APPL_HIGH ); }
288 bool isByUser() const
289 { return fieldValueIs<TransactByField>( USER ); }
291 TransactByValue getTransactByValue() const
292 { return (TransactByValue)_bitfield.value<TransactByField>(); }
294 bool setTransactByValue(TransactByValue causer)
296 if ( isLessThan<TransactByField>( causer ) ) {
297 fieldValueAssign<TransactByField>( causer );
304 bool isToBeUninstalledDueToObsolete () const
305 { return isToBeUninstalled() && fieldValueIs<TransactDetailField>( DUE_TO_OBSOLETE ); }
307 bool isToBeUninstalledDueToUpgrade() const
308 { return isToBeUninstalled() && fieldValueIs<TransactDetailField>( DUE_TO_UPGRADE ); }
310 bool isToBeInstalledSoft () const
311 { return isToBeInstalled() && fieldValueIs<TransactDetailField>( SOFT_INSTALL ); }
313 bool isToBeInstalledNotSoft () const
314 { return isToBeInstalled() && !fieldValueIs<TransactDetailField>( SOFT_INSTALL ); }
316 bool isToBeUninstalledSoft () const
317 { return isToBeUninstalled() && fieldValueIs<TransactDetailField>( SOFT_REMOVE ); }
321 /** \name Internal hard lock maintainance */
323 friend class resstatus::UserLockQueryManip;
325 bool isUserLockQueryMatch() const
326 { return fieldValueIs<UserLockQueryField>( USERLOCK_MATCH ); }
328 void setUserLockQueryMatch( bool match_r )
329 { fieldValueAssign<UserLockQueryField>( match_r ? USERLOCK_MATCH : USERLOCK_NOMATCH ); }
334 //------------------------------------------------------------------------
335 // get/set functions, returnig \c true if requested status change
336 // was successfull (i.e. leading to the desired transaction).
337 // If a lower level (e.g.SOLVER) wants to transact, but it's
338 // already set by a higher level, \c true should be returned.
339 // Removing a higher levels transaction bit should fail.
341 // The may functions checks only, if the action would return true
344 /** Set TransactValue.
345 * Convenience to set TransactValue from enum.
347 bool setTransactValue( TransactValue newVal_r, TransactByValue causer_r )
352 return setTransact( false, causer_r );
355 return setLock( true, causer_r );
358 return setTransact( true, causer_r );
364 bool maySetTransactValue( TransactValue newVal_r, TransactByValue causer_r )
366 bit::BitField<FieldType> savBitfield = _bitfield;
367 bool ret = setTransactValue( newVal_r, causer_r );
368 _bitfield = savBitfield;
372 /** Apply a lock (prevent transaction).
373 * Currently by USER or APPL_HIGH only, but who knows...
374 * Set LOCKED from KEEP_STATE to be shure all transaction
375 * details were reset properly.
377 bool setLock( bool toLock_r, TransactByValue causer_r )
379 if ( toLock_r == isLocked() )
381 // we're already in the desired state, but in case of
382 // LOCKED, remember a superior causer.
383 if ( isLocked() && isLessThan<TransactByField>( causer_r ) )
384 fieldValueAssign<TransactByField>( causer_r );
387 // Here: Lock status is to be changed:
388 if ( causer_r != USER && causer_r != APPL_HIGH )
391 // We're in unlocked state, which includes TRANSACT.
392 // Causer must be allowed to reset this. But from
393 // KEEP_STATE every causer is allowed to set the lock.
394 if ( ! setTransact( false, causer_r ) )
396 fieldValueAssign<TransactField>( LOCKED );
397 fieldValueAssign<TransactByField>( causer_r );
399 // To leave Locked state it needs a superior causer.
400 if ( isGreaterThan<TransactByField>( causer_r ) )
402 fieldValueAssign<TransactField>( KEEP_STATE );
403 fieldValueAssign<TransactByField>( SOLVER ); // reset to lowest causer
404 // in order to distinguish from keep_state_by_user
409 bool maySetLock( bool to_r, TransactByValue causer_r )
411 bit::BitField<FieldType> savBitfield = _bitfield;
412 bool ret = setLock( to_r, causer_r );
413 _bitfield = savBitfield;
417 /** Toggle between TRANSACT and KEEP_STATE.
418 * LOCKED state means KEEP_STATE. But in contrary to KEEP_STATE,
419 * LOCKED state is immutable for \a causer_r less than TransactByValue.
420 * KEEP_STATE may be canged by any \a causer_r.
422 bool setTransact( bool toTansact_r, TransactByValue causer_r )
424 if ( toTansact_r == transacts() )
426 // we're already in the desired state, but in case of
427 // TRANSACT, remember a superior causer.
428 if ( transacts() && isLessThan<TransactByField>( causer_r ) )
429 fieldValueAssign<TransactByField>( causer_r );
431 fieldValueAssign<TransactDetailField>( NO_DETAIL ); // Details has to be set again
434 // Here: transact status is to be changed:
435 if ( ! fieldValueIs<TransactField>( KEEP_STATE )
436 && isGreaterThan<TransactByField>( causer_r ) ) {
442 fieldValueAssign<TransactField>( TRANSACT );
446 fieldValueAssign<TransactField>( KEEP_STATE );
448 fieldValueAssign<TransactDetailField>( NO_DETAIL ); // Details has to be set again
449 fieldValueAssign<TransactByField>( causer_r );
453 bool maySetTransact( bool val_r, TransactByValue causer )
455 bit::BitField<FieldType> savBitfield = _bitfield;
456 bool ret = setTransact (val_r, causer);
457 _bitfield = savBitfield;
462 bool setSoftLock( TransactByValue causer_r )
464 if ( ! setTransact( false, causer_r ) )
466 if ( fieldValueIs<TransactField>( KEEP_STATE )
467 && isLessThan<TransactByField>( causer_r ) )
468 fieldValueAssign<TransactByField>( causer_r );
472 /** Not the same as setTransact( false ).
474 bool resetTransact( TransactByValue causer_r )
476 if ( ! setTransact( false, causer_r ) )
478 if ( fieldValueIs<TransactField>( KEEP_STATE ) )
479 fieldValueAssign<TransactByField>( SOLVER );
483 /** Soft toggle between TRANSACT and KEEP_STATE.
484 * Similar to setTransact, but leaving KEEP_STATE also requires
485 * a superior \a causerLimit_r. So this is a kind of soft lock.
487 * // SOLVER wants to set TRANSACT, iff KEEP_STATE is
488 * // not superior to APPL_LOW.
489 * setSoftTransact( true, SOLVER, APPL_LOW );
492 bool setSoftTransact( bool toTansact_r, TransactByValue causer_r,
493 TransactByValue causerLimit_r )
495 if ( fieldValueIs<TransactField>( KEEP_STATE )
496 && toTansact_r != transacts()
497 && isGreaterThan<TransactByField>( causerLimit_r ) )
499 // any transact status change requires a superior causer.
502 return setTransact( toTansact_r, causer_r );
505 bool setSoftTransact( bool toTansact_r, TransactByValue causer_r )
506 { return setSoftTransact( toTansact_r, causer_r, causer_r ); }
508 bool maySetSoftTransact( bool val_r, TransactByValue causer,
509 TransactByValue causerLimit_r )
511 bit::BitField<FieldType> savBitfield = _bitfield;
512 bool ret = setSoftTransact( val_r, causer, causerLimit_r );
513 _bitfield = savBitfield;
517 bool maySetSoftTransact( bool val_r, TransactByValue causer )
518 { return maySetSoftTransact( val_r, causer, causer ); }
520 bool setToBeInstalled (TransactByValue causer)
522 if (isInstalled()) return false;
523 return setTransact (true, causer);
526 bool maySetToBeInstalled (TransactByValue causer)
528 bit::BitField<FieldType> savBitfield = _bitfield;
529 bool ret = setToBeInstalled (causer);
530 _bitfield = savBitfield;
534 bool setToBeUninstalled (TransactByValue causer)
536 if (!isInstalled()) return false;
537 return setTransact (true, causer);
540 bool maySetToBeUninstalled (TransactByValue causer)
542 bit::BitField<FieldType> savBitfield = _bitfield;
543 bool ret = setToBeUninstalled (causer);
544 _bitfield = savBitfield;
548 //------------------------------------------------------------------------
549 // *** These are only for the Resolver ***
551 bool setToBeUninstalledDueToUpgrade ( TransactByValue causer )
553 if (!setToBeUninstalled (causer)) return false;
554 fieldValueAssign<TransactDetailField>(DUE_TO_UPGRADE);
558 bool setToBeInstalledSoft ( )
561 || !setSoftTransact (true, SOLVER))
564 fieldValueAssign<TransactDetailField>(SOFT_INSTALL);
568 bool setToBeUninstalledSoft ( )
571 || !setSoftTransact (true, SOLVER))
574 fieldValueAssign<TransactDetailField>(SOFT_REMOVE);
578 bool maySetToBeUninstalledSoft ()
580 bit::BitField<FieldType> savBitfield = _bitfield;
581 bool ret = setToBeUninstalledSoft ();
582 _bitfield = savBitfield;
586 bool isSoftInstall () {
587 return fieldValueIs<TransactDetailField> (SOFT_INSTALL);
590 bool isSoftUninstall () {
591 return fieldValueIs<TransactDetailField> (SOFT_REMOVE);
594 bool setSoftInstall (bool flag) {
595 fieldValueAssign<TransactDetailField>(flag?SOFT_INSTALL:0);
599 bool setSoftUninstall (bool flag) {
600 fieldValueAssign<TransactDetailField>(flag?SOFT_REMOVE:0);
604 bool setUndetermined ()
606 fieldValueAssign<ValidateField>(UNDETERMINED);
612 fieldValueAssign<ValidateField>(SATISFIED);
618 fieldValueAssign<ValidateField>(BROKEN);
622 bool setNonRelevant ()
624 fieldValueAssign<ValidateField>(NONRELEVANT);
629 { return fieldValueIs<SolverStateField>( SEEN ); }
631 bool setSeen (bool value)
633 fieldValueAssign<SolverStateField>( value ? SEEN : NORMAL );
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;
659 /** Ctor for intialization of builtin constants. */
660 ResStatus( StateValue s,
661 ValidateValue v = UNDETERMINED,
662 TransactValue t = KEEP_STATE,
663 InstallDetailValue i = EXPLICIT_INSTALL,
664 RemoveDetailValue r = EXPLICIT_REMOVE,
665 SolverStateValue ssv = NORMAL );
667 /** Return whether the corresponding Field has value \a val_r.
669 template<class _Field>
670 bool fieldValueIs( FieldType val_r ) const
671 { return _bitfield.isEqual<_Field>( val_r ); }
673 /** Set the corresponding Field to value \a val_r.
675 template<class _Field>
676 void fieldValueAssign( FieldType val_r )
677 { _bitfield.assign<_Field>( val_r ); }
679 /** compare two values.
681 template<class _Field>
682 bool isGreaterThan( FieldType val_r )
683 { return _bitfield.value<_Field>() > val_r; }
685 template<class _Field>
686 bool isLessThan( FieldType val_r )
687 { return _bitfield.value<_Field>() < 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 */
699 inline bool operator==( const ResStatus & lhs, const ResStatus & rhs )
700 { return lhs._bitfield == rhs._bitfield; }
702 /** \relates ResStatus */
703 inline bool operator!=( const ResStatus & lhs, const ResStatus & rhs )
704 { return ! (lhs == rhs); }
706 ///////////////////////////////////////////////////////////////////
717 StatusBackup( ResStatus & status_r )
718 : _status( &status_r )
719 , _bitfield( _status->_bitfield )
723 { if ( _status ) _status->_bitfield = _bitfield; }
727 ResStatus::BitFieldType _bitfield;
731 /////////////////////////////////////////////////////////////////
733 ///////////////////////////////////////////////////////////////////
734 #endif // ZYPP_RESSTATUS_H