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> LicenceConfirmedField;
74 typedef bit::Range<FieldType,LicenceConfirmedField::end, 2> 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
140 NO_WEAK = bit::RangeValue<WeakField,0>::value,
141 SUGGESTED = bit::RangeValue<WeakField,1>::value,
142 RECOMMENDED = bit::RangeValue<WeakField,2>::value,
143 SUGGESTED_AND_RECOMMENDED = bit::RangeValue<WeakField,3>::value
146 enum UserLockQuery // internal
148 USERLOCK_NOMATCH = bit::RangeValue<UserLockQueryField,0>::value,
149 USERLOCK_MATCH = bit::RangeValue<UserLockQueryField,1>::value
158 /** Ctor setting the initial . */
159 ResStatus( bool isInstalled_r );
164 /** Debug helper returning the bitfield.
165 * It's save to expose the bitfield, as it can't be used to
166 * recreate a ResStatus. So it is not possible to bypass
169 BitFieldType bitfield() const
170 { return _bitfield; }
174 bool isLicenceConfirmed() const
175 { return fieldValueIs<LicenceConfirmedField>( LICENCE_CONFIRMED ); }
177 void setLicenceConfirmed( bool toVal_r = true )
178 { fieldValueAssign<LicenceConfirmedField>( toVal_r ? LICENCE_CONFIRMED : LICENCE_UNCONFIRMED ); }
182 bool isRecommended() const
183 { return fieldValueIs<WeakField>( RECOMMENDED ); }
185 bool isSuggested() const
186 { return fieldValueIs<WeakField>( SUGGESTED ); }
188 bool resetWeak() const
189 { return fieldValueIs<WeakField>( NO_WEAK ); }
191 void setRecommended( bool toVal_r = true )
193 fieldValueAssign<WeakField>( toVal_r ? SUGGESTED_AND_RECOMMENDED : SUGGESTED );
195 fieldValueAssign<WeakField>( toVal_r ? RECOMMENDED : NO_WEAK );
198 void setSuggested( bool toVal_r = true )
199 { if (isRecommended())
200 fieldValueAssign<WeakField>( toVal_r ? SUGGESTED_AND_RECOMMENDED : RECOMMENDED );
202 fieldValueAssign<WeakField>( toVal_r ? SUGGESTED : NO_WEAK );
205 ValidateValue validate() const
206 { return (ValidateValue)_bitfield.value<ValidateField>(); }
208 bool isUndetermined() const
209 { return fieldValueIs<ValidateField>( UNDETERMINED ); }
211 bool isSatisfied() const
212 { return fieldValueIs<ValidateField>( SATISFIED ); }
214 bool isBroken() const
215 { return fieldValueIs<ValidateField>( BROKEN ); }
217 bool isNonRelevant() const
218 { return fieldValueIs<ValidateField>( NONRELEVANT ); }
221 // These two are IMMUTABLE!
223 bool isInstalled() const
224 { return fieldValueIs<StateField>( INSTALLED ); }
226 bool isUninstalled() const
227 { return fieldValueIs<StateField>( UNINSTALLED ); }
231 bool staysInstalled() const
232 { return isInstalled() && !transacts(); }
234 bool wasInstalled() const { return staysInstalled(); } //for old status
236 bool isToBeInstalled() const
237 { return isUninstalled() && transacts(); }
239 bool staysUninstalled() const
240 { return isUninstalled() && !transacts(); }
242 bool wasUninstalled() const { return staysUninstalled(); } // for old status
244 bool isToBeUninstalled() const
245 { return isInstalled() && transacts(); }
247 bool isLocked() const
248 { return fieldValueIs<TransactField>( LOCKED ); }
250 bool isUserLocked() const
251 { return isLocked() && isByUser(); }
253 bool isSoftLocked( TransactByValue causer_r = USER ) const
254 { return isKept() && fieldValueIs<TransactByField>( causer_r ); }
257 { return fieldValueIs<TransactField>( KEEP_STATE ); }
259 bool transacts() const
260 { return fieldValueIs<TransactField>( TRANSACT ); }
262 TransactValue getTransactValue() const
263 { return (TransactValue)_bitfield.value<TransactField>(); }
265 /** True if would be on system after commit. */
266 bool onSystem() const
267 { return( isInstalled() != transacts() ); }
269 /** True if would be off system after commit. */
270 bool offSystem() const
271 { return ! onSystem(); }
273 bool isBySolver() const
274 { return fieldValueIs<TransactByField>( SOLVER ); }
276 bool isByApplLow() const
277 { return fieldValueIs<TransactByField>( APPL_LOW ); }
279 bool isByApplHigh() const
280 { return fieldValueIs<TransactByField>( APPL_HIGH ); }
282 bool isByUser() const
283 { return fieldValueIs<TransactByField>( USER ); }
285 TransactByValue getTransactByValue() const
286 { return (TransactByValue)_bitfield.value<TransactByField>(); }
288 bool setTransactByValue(TransactByValue causer)
290 if ( isLessThan<TransactByField>( causer ) ) {
291 fieldValueAssign<TransactByField>( causer );
298 bool isToBeUninstalledDueToObsolete () const
299 { return isToBeUninstalled() && fieldValueIs<TransactDetailField>( DUE_TO_OBSOLETE ); }
301 bool isToBeUninstalledDueToUpgrade() const
302 { return isToBeUninstalled() && fieldValueIs<TransactDetailField>( DUE_TO_UPGRADE ); }
304 bool isToBeInstalledSoft () const
305 { return isToBeInstalled() && fieldValueIs<TransactDetailField>( SOFT_INSTALL ); }
307 bool isToBeInstalledNotSoft () const
308 { return isToBeInstalled() && !fieldValueIs<TransactDetailField>( SOFT_INSTALL ); }
310 bool isToBeUninstalledSoft () const
311 { return isToBeUninstalled() && fieldValueIs<TransactDetailField>( SOFT_REMOVE ); }
315 /** \name Internal hard lock maintainance */
317 friend class resstatus::UserLockQueryManip;
319 bool isUserLockQueryMatch() const
320 { return fieldValueIs<UserLockQueryField>( USERLOCK_MATCH ); }
322 void setUserLockQueryMatch( bool match_r )
323 { fieldValueAssign<UserLockQueryField>( match_r ? USERLOCK_MATCH : USERLOCK_NOMATCH ); }
328 //------------------------------------------------------------------------
329 // get/set functions, returnig \c true if requested status change
330 // was successfull (i.e. leading to the desired transaction).
331 // If a lower level (e.g.SOLVER) wants to transact, but it's
332 // already set by a higher level, \c true should be returned.
333 // Removing a higher levels transaction bit should fail.
335 // The may functions checks only, if the action would return true
338 /** Set TransactValue.
339 * Convenience to set TransactValue from enum.
341 bool setTransactValue( TransactValue newVal_r, TransactByValue causer_r )
346 return setTransact( false, causer_r );
349 return setLock( true, causer_r );
352 return setTransact( true, causer_r );
358 bool maySetTransactValue( TransactValue newVal_r, TransactByValue causer_r )
360 bit::BitField<FieldType> savBitfield = _bitfield;
361 bool ret = setTransactValue( newVal_r, causer_r );
362 _bitfield = savBitfield;
366 /** Apply a lock (prevent transaction).
367 * Currently by USER or APPL_HIGH only, but who knows...
368 * Set LOCKED from KEEP_STATE to be shure all transaction
369 * details were reset properly.
371 bool setLock( bool toLock_r, TransactByValue causer_r )
373 if ( toLock_r == isLocked() )
375 // we're already in the desired state, but in case of
376 // LOCKED, remember a superior causer.
377 if ( isLocked() && isLessThan<TransactByField>( causer_r ) )
378 fieldValueAssign<TransactByField>( causer_r );
381 // Here: Lock status is to be changed:
382 if ( causer_r != USER && causer_r != APPL_HIGH )
385 // We're in unlocked state, which includes TRANSACT.
386 // Causer must be allowed to reset this. But from
387 // KEEP_STATE every causer is allowed to set the lock.
388 if ( ! setTransact( false, causer_r ) )
390 fieldValueAssign<TransactField>( LOCKED );
391 fieldValueAssign<TransactByField>( causer_r );
393 // To leave Locked state it needs a superior causer.
394 if ( isGreaterThan<TransactByField>( causer_r ) )
396 fieldValueAssign<TransactField>( KEEP_STATE );
397 fieldValueAssign<TransactByField>( SOLVER ); // reset to lowest causer
398 // in order to distinguish from keep_state_by_user
403 bool maySetLock( bool to_r, TransactByValue causer_r )
405 bit::BitField<FieldType> savBitfield = _bitfield;
406 bool ret = setLock( to_r, causer_r );
407 _bitfield = savBitfield;
411 /** Toggle between TRANSACT and KEEP_STATE.
412 * LOCKED state means KEEP_STATE. But in contrary to KEEP_STATE,
413 * LOCKED state is immutable for \a causer_r less than TransactByValue.
414 * KEEP_STATE may be canged by any \a causer_r.
416 bool setTransact( bool toTansact_r, TransactByValue causer_r )
418 if ( toTansact_r == transacts() )
420 // we're already in the desired state, but in case of
421 // TRANSACT, remember a superior causer.
422 if ( transacts() && isLessThan<TransactByField>( causer_r ) )
423 fieldValueAssign<TransactByField>( causer_r );
425 fieldValueAssign<TransactDetailField>( NO_DETAIL ); // Details has to be set again
428 // Here: transact status is to be changed:
429 if ( ! fieldValueIs<TransactField>( KEEP_STATE )
430 && isGreaterThan<TransactByField>( causer_r ) ) {
436 fieldValueAssign<TransactField>( TRANSACT );
440 fieldValueAssign<TransactField>( KEEP_STATE );
442 fieldValueAssign<TransactDetailField>( NO_DETAIL ); // Details has to be set again
443 fieldValueAssign<TransactByField>( causer_r );
447 bool maySetTransact( bool val_r, TransactByValue causer )
449 bit::BitField<FieldType> savBitfield = _bitfield;
450 bool ret = setTransact (val_r, causer);
451 _bitfield = savBitfield;
456 bool setSoftLock( TransactByValue causer_r )
458 if ( ! setTransact( false, causer_r ) )
460 if ( fieldValueIs<TransactField>( KEEP_STATE )
461 && isLessThan<TransactByField>( causer_r ) )
462 fieldValueAssign<TransactByField>( causer_r );
466 /** Not the same as setTransact( false ).
468 bool resetTransact( TransactByValue causer_r )
470 if ( ! setTransact( false, causer_r ) )
472 if ( fieldValueIs<TransactField>( KEEP_STATE ) )
473 fieldValueAssign<TransactByField>( SOLVER );
477 /** Soft toggle between TRANSACT and KEEP_STATE.
478 * Similar to setTransact, but leaving KEEP_STATE also requires
479 * a superior \a causerLimit_r. So this is a kind of soft lock.
481 * // SOLVER wants to set TRANSACT, iff KEEP_STATE is
482 * // not superior to APPL_LOW.
483 * setSoftTransact( true, SOLVER, APPL_LOW );
486 bool setSoftTransact( bool toTansact_r, TransactByValue causer_r,
487 TransactByValue causerLimit_r )
489 if ( fieldValueIs<TransactField>( KEEP_STATE )
490 && toTansact_r != transacts()
491 && isGreaterThan<TransactByField>( causerLimit_r ) )
493 // any transact status change requires a superior causer.
496 return setTransact( toTansact_r, causer_r );
499 bool setSoftTransact( bool toTansact_r, TransactByValue causer_r )
500 { return setSoftTransact( toTansact_r, causer_r, causer_r ); }
502 bool maySetSoftTransact( bool val_r, TransactByValue causer,
503 TransactByValue causerLimit_r )
505 bit::BitField<FieldType> savBitfield = _bitfield;
506 bool ret = setSoftTransact( val_r, causer, causerLimit_r );
507 _bitfield = savBitfield;
511 bool maySetSoftTransact( bool val_r, TransactByValue causer )
512 { return maySetSoftTransact( val_r, causer, causer ); }
514 bool setToBeInstalled (TransactByValue causer)
516 if (isInstalled()) return false;
517 return setTransact (true, causer);
520 bool maySetToBeInstalled (TransactByValue causer)
522 bit::BitField<FieldType> savBitfield = _bitfield;
523 bool ret = setToBeInstalled (causer);
524 _bitfield = savBitfield;
528 bool setToBeUninstalled (TransactByValue causer)
530 if (!isInstalled()) return false;
531 return setTransact (true, causer);
534 bool maySetToBeUninstalled (TransactByValue causer)
536 bit::BitField<FieldType> savBitfield = _bitfield;
537 bool ret = setToBeUninstalled (causer);
538 _bitfield = savBitfield;
542 //------------------------------------------------------------------------
543 // *** These are only for the Resolver ***
545 bool setToBeUninstalledDueToObsolete ( )
547 if (!setToBeUninstalled (SOLVER)) return false;
548 fieldValueAssign<TransactDetailField>(DUE_TO_OBSOLETE);
552 bool setToBeUninstalledDueToUpgrade ( TransactByValue causer )
554 if (!setToBeUninstalled (causer)) return false;
555 fieldValueAssign<TransactDetailField>(DUE_TO_UPGRADE);
559 bool setToBeInstalledSoft ( )
562 || !setSoftTransact (true, SOLVER))
565 fieldValueAssign<TransactDetailField>(SOFT_INSTALL);
569 bool setToBeUninstalledSoft ( )
572 || !setSoftTransact (true, SOLVER))
575 fieldValueAssign<TransactDetailField>(SOFT_REMOVE);
579 bool maySetToBeUninstalledSoft ()
581 bit::BitField<FieldType> savBitfield = _bitfield;
582 bool ret = setToBeUninstalledSoft ();
583 _bitfield = savBitfield;
587 bool isSoftInstall () {
588 return fieldValueIs<TransactDetailField> (SOFT_INSTALL);
591 bool isSoftUninstall () {
592 return fieldValueIs<TransactDetailField> (SOFT_REMOVE);
595 bool setSoftInstall (bool flag) {
596 fieldValueAssign<TransactDetailField>(flag?SOFT_INSTALL:0);
600 bool setSoftUninstall (bool flag) {
601 fieldValueAssign<TransactDetailField>(flag?SOFT_REMOVE:0);
605 bool setUndetermined ()
607 fieldValueAssign<ValidateField>(UNDETERMINED);
613 fieldValueAssign<ValidateField>(SATISFIED);
619 fieldValueAssign<ValidateField>(BROKEN);
623 bool setNonRelevant ()
625 fieldValueAssign<ValidateField>(NONRELEVANT);
629 bool setStatus( ResStatus newStatus_r )
631 // State field is immutable!
632 if ( _bitfield.value<StateField>() != newStatus_r._bitfield.value<StateField>() )
634 // Transaction state change allowed?
635 if ( ! setTransactValue( newStatus_r.getTransactValue(), newStatus_r.getTransactByValue() ) )
638 // Ok, we take it all..
639 _bitfield = newStatus_r._bitfield;
643 /** \name Builtin ResStatus constants. */
645 static const ResStatus toBeInstalled;
646 static const ResStatus toBeUninstalled;
647 static const ResStatus toBeUninstalledDueToUpgrade;
648 static const ResStatus toBeUninstalledDueToObsolete;
652 /** Ctor for intialization of builtin constants. */
653 ResStatus( StateValue s,
654 ValidateValue v = UNDETERMINED,
655 TransactValue t = KEEP_STATE,
656 InstallDetailValue i = EXPLICIT_INSTALL,
657 RemoveDetailValue r = EXPLICIT_REMOVE );
659 /** Return whether the corresponding Field has value \a val_r.
661 template<class _Field>
662 bool fieldValueIs( FieldType val_r ) const
663 { return _bitfield.isEqual<_Field>( val_r ); }
665 /** Set the corresponding Field to value \a val_r.
667 template<class _Field>
668 void fieldValueAssign( FieldType val_r )
669 { _bitfield.assign<_Field>( val_r ); }
671 /** compare two values.
673 template<class _Field>
674 bool isGreaterThan( FieldType val_r )
675 { return _bitfield.value<_Field>() > val_r; }
677 template<class _Field>
678 bool isLessThan( FieldType val_r )
679 { return _bitfield.value<_Field>() < val_r; }
682 friend class resstatus::StatusBackup;
683 BitFieldType _bitfield;
685 ///////////////////////////////////////////////////////////////////
687 /** \relates ResStatus Stream output */
688 std::ostream & operator<<( std::ostream & str, const ResStatus & obj );
690 /** \relates ResStatus Stream output */
691 std::ostream & operator<<( std::ostream & str, ResStatus::TransactValue obj );
693 /** \relates ResStatus Stream output */
694 std::ostream & operator<<( std::ostream & str, ResStatus::TransactByValue obj );
696 /** \relates ResStatus */
697 inline bool operator==( const ResStatus & lhs, const ResStatus & rhs )
698 { return lhs._bitfield == rhs._bitfield; }
700 /** \relates ResStatus */
701 inline bool operator!=( const ResStatus & lhs, const ResStatus & rhs )
702 { return ! (lhs == rhs); }
704 ///////////////////////////////////////////////////////////////////
715 StatusBackup( ResStatus & status_r )
716 : _status( &status_r )
717 , _bitfield( _status->_bitfield )
721 { if ( _status ) _status->_bitfield = _bitfield; }
725 ResStatus::BitFieldType _bitfield;
729 /////////////////////////////////////////////////////////////////
731 ///////////////////////////////////////////////////////////////////
732 #endif // ZYPP_RESSTATUS_H