1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/ResStatus.h
12 #ifndef ZYPP_RESSTATUS_H
13 #define ZYPP_RESSTATUS_H
19 ///////////////////////////////////////////////////////////////////
21 { /////////////////////////////////////////////////////////////////
25 struct 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 ); }
228 std::string validateValueAsString() const
229 { return validateValueAsString( validate() ); }
231 /** ValidateValue to string used in the history file. */
232 static std::string validateValueAsString( ValidateValue val_r );
234 /** ValidateValue from string used in the history file. */
235 static ValidateValue stringToValidateValue( const std::string & str_r );
238 // These two are IMMUTABLE!
240 bool isInstalled() const
241 { return fieldValueIs<StateField>( INSTALLED ); }
243 bool isUninstalled() const
244 { return fieldValueIs<StateField>( UNINSTALLED ); }
248 bool staysInstalled() const
249 { return isInstalled() && !transacts(); }
251 bool wasInstalled() const { return staysInstalled(); } //for old status
253 bool isToBeInstalled() const
254 { return isUninstalled() && transacts(); }
256 bool staysUninstalled() const
257 { return isUninstalled() && !transacts(); }
259 bool wasUninstalled() const { return staysUninstalled(); } // for old status
261 bool isToBeUninstalled() const
262 { return isInstalled() && transacts(); }
264 bool isLocked() const
265 { return fieldValueIs<TransactField>( LOCKED ); }
267 bool isUserLocked() const
268 { return isLocked() && isByUser(); }
270 bool isSoftLocked() const
271 { return isKept() && ( isByApplLow() || isByUser() ); }
274 { return fieldValueIs<TransactField>( KEEP_STATE ); }
276 bool transacts() const
277 { return fieldValueIs<TransactField>( TRANSACT ); }
279 TransactValue getTransactValue() const
280 { return (TransactValue)_bitfield.value<TransactField>(); }
282 /** True if would be on system after commit. */
283 bool onSystem() const
284 { return( isInstalled() != transacts() ); }
286 /** True if would be off system after commit. */
287 bool offSystem() const
288 { return ! onSystem(); }
290 bool isBySolver() const
291 { return fieldValueIs<TransactByField>( SOLVER ); }
293 bool isByApplLow() const
294 { return fieldValueIs<TransactByField>( APPL_LOW ); }
296 bool isByApplHigh() const
297 { return fieldValueIs<TransactByField>( APPL_HIGH ); }
299 bool isByUser() const
300 { return fieldValueIs<TransactByField>( USER ); }
302 TransactByValue getTransactByValue() const
303 { return (TransactByValue)_bitfield.value<TransactByField>(); }
305 bool setTransactByValue(TransactByValue causer)
307 if ( isLessThan<TransactByField>( causer ) ) {
308 fieldValueAssign<TransactByField>( causer );
315 bool isToBeUninstalledDueToObsolete () const
316 { return isToBeUninstalled() && fieldValueIs<TransactDetailField>( DUE_TO_OBSOLETE ); }
318 bool isToBeUninstalledDueToUpgrade() const
319 { return isToBeUninstalled() && fieldValueIs<TransactDetailField>( DUE_TO_UPGRADE ); }
321 bool isToBeInstalledSoft () const
322 { return isToBeInstalled() && fieldValueIs<TransactDetailField>( SOFT_INSTALL ); }
324 bool isToBeInstalledNotSoft () const
325 { return isToBeInstalled() && !fieldValueIs<TransactDetailField>( SOFT_INSTALL ); }
327 bool isToBeUninstalledSoft () const
328 { return isToBeUninstalled() && fieldValueIs<TransactDetailField>( SOFT_REMOVE ); }
332 /** \name Internal hard lock maintainance */
334 friend struct resstatus::UserLockQueryManip;
336 bool isUserLockQueryMatch() const
337 { return fieldValueIs<UserLockQueryField>( USERLOCK_MATCH ); }
339 void setUserLockQueryMatch( bool match_r )
340 { fieldValueAssign<UserLockQueryField>( match_r ? USERLOCK_MATCH : USERLOCK_NOMATCH ); }
345 //------------------------------------------------------------------------
346 // get/set functions, returnig \c true if requested status change
347 // was successfull (i.e. leading to the desired transaction).
348 // If a lower level (e.g.SOLVER) wants to transact, but it's
349 // already set by a higher level, \c true should be returned.
350 // Removing a higher levels transaction bit should fail.
352 // The may functions checks only, if the action would return true
355 /** Set TransactValue.
356 * Convenience to set TransactValue from enum.
358 bool setTransactValue( TransactValue newVal_r, TransactByValue causer_r )
363 return setTransact( false, causer_r );
366 return setLock( true, causer_r );
369 return setTransact( true, causer_r );
375 bool maySetTransactValue( TransactValue newVal_r, TransactByValue causer_r )
377 bit::BitField<FieldType> savBitfield = _bitfield;
378 bool ret = setTransactValue( newVal_r, causer_r );
379 _bitfield = savBitfield;
383 /** Apply a lock (prevent transaction).
384 * Currently by USER or APPL_HIGH only, but who knows...
385 * Set LOCKED from KEEP_STATE to be shure all transaction
386 * details were reset properly.
388 bool setLock( bool toLock_r, TransactByValue causer_r )
390 if ( toLock_r == isLocked() )
392 // we're already in the desired state, but in case of
393 // LOCKED, remember a superior causer.
394 if ( isLocked() && isLessThan<TransactByField>( causer_r ) )
395 fieldValueAssign<TransactByField>( causer_r );
398 // Here: Lock status is to be changed:
399 if ( causer_r != USER && causer_r != APPL_HIGH )
402 // We're in unlocked state, which includes TRANSACT.
403 // Causer must be allowed to reset this. But from
404 // KEEP_STATE every causer is allowed to set the lock.
405 if ( ! setTransact( false, causer_r ) )
407 fieldValueAssign<TransactField>( LOCKED );
408 fieldValueAssign<TransactByField>( causer_r );
410 // To leave Locked state it needs a superior causer.
411 if ( isGreaterThan<TransactByField>( causer_r ) )
413 fieldValueAssign<TransactField>( KEEP_STATE );
414 fieldValueAssign<TransactByField>( SOLVER ); // reset to lowest causer
415 // in order to distinguish from keep_state_by_user
420 bool maySetLock( bool to_r, TransactByValue causer_r )
422 bit::BitField<FieldType> savBitfield = _bitfield;
423 bool ret = setLock( to_r, causer_r );
424 _bitfield = savBitfield;
428 /** Toggle between TRANSACT and KEEP_STATE.
429 * LOCKED state means KEEP_STATE. But in contrary to KEEP_STATE,
430 * LOCKED state is immutable for \a causer_r less than TransactByValue.
431 * KEEP_STATE may be canged by any \a causer_r.
433 bool setTransact( bool toTansact_r, TransactByValue causer_r )
435 if ( toTansact_r == transacts() )
437 // we're already in the desired state, but in case of
438 // TRANSACT, remember a superior causer.
439 if ( transacts() && isLessThan<TransactByField>( causer_r ) )
440 fieldValueAssign<TransactByField>( causer_r );
442 fieldValueAssign<TransactDetailField>( NO_DETAIL ); // Details has to be set again
445 // Here: transact status is to be changed:
446 if ( ! fieldValueIs<TransactField>( KEEP_STATE )
447 && isGreaterThan<TransactByField>( causer_r ) ) {
453 fieldValueAssign<TransactField>( TRANSACT );
457 fieldValueAssign<TransactField>( KEEP_STATE );
459 fieldValueAssign<TransactDetailField>( NO_DETAIL ); // Details has to be set again
460 fieldValueAssign<TransactByField>( causer_r );
464 bool maySetTransact( bool val_r, TransactByValue causer )
466 bit::BitField<FieldType> savBitfield = _bitfield;
467 bool ret = setTransact (val_r, causer);
468 _bitfield = savBitfield;
473 bool setSoftLock( TransactByValue causer_r )
475 if ( ! setTransact( false, causer_r ) )
477 if ( fieldValueIs<TransactField>( KEEP_STATE )
478 && isLessThan<TransactByField>( causer_r ) )
479 fieldValueAssign<TransactByField>( causer_r );
483 /** Not the same as setTransact( false ).
485 bool resetTransact( TransactByValue causer_r )
487 if ( ! setTransact( false, causer_r ) )
489 if ( fieldValueIs<TransactField>( KEEP_STATE ) )
490 fieldValueAssign<TransactByField>( SOLVER );
494 /** Soft toggle between TRANSACT and KEEP_STATE.
495 * Similar to setTransact, but leaving KEEP_STATE also requires
496 * a superior \a causerLimit_r. So this is a kind of soft lock.
498 * // SOLVER wants to set TRANSACT, iff KEEP_STATE is
499 * // not superior to APPL_LOW.
500 * setSoftTransact( true, SOLVER, APPL_LOW );
503 bool setSoftTransact( bool toTansact_r, TransactByValue causer_r,
504 TransactByValue causerLimit_r )
506 if ( fieldValueIs<TransactField>( KEEP_STATE )
507 && toTansact_r != transacts()
508 && isGreaterThan<TransactByField>( causerLimit_r ) )
510 // any transact status change requires a superior causer.
513 return setTransact( toTansact_r, causer_r );
516 bool setSoftTransact( bool toTansact_r, TransactByValue causer_r )
517 { return setSoftTransact( toTansact_r, causer_r, causer_r ); }
519 bool maySetSoftTransact( bool val_r, TransactByValue causer,
520 TransactByValue causerLimit_r )
522 bit::BitField<FieldType> savBitfield = _bitfield;
523 bool ret = setSoftTransact( val_r, causer, causerLimit_r );
524 _bitfield = savBitfield;
528 bool maySetSoftTransact( bool val_r, TransactByValue causer )
529 { return maySetSoftTransact( val_r, causer, causer ); }
531 bool setToBeInstalled (TransactByValue causer)
533 if (isInstalled()) return false;
534 return setTransact (true, causer);
537 bool maySetToBeInstalled (TransactByValue causer)
539 bit::BitField<FieldType> savBitfield = _bitfield;
540 bool ret = setToBeInstalled (causer);
541 _bitfield = savBitfield;
545 bool setToBeUninstalled (TransactByValue causer)
547 if (!isInstalled()) return false;
548 return setTransact (true, causer);
551 bool maySetToBeUninstalled (TransactByValue causer)
553 bit::BitField<FieldType> savBitfield = _bitfield;
554 bool ret = setToBeUninstalled (causer);
555 _bitfield = savBitfield;
559 //------------------------------------------------------------------------
560 // *** These are only for the Resolver ***
562 bool setToBeUninstalledDueToObsolete ( )
564 if (!setToBeUninstalled (SOLVER)) return false;
565 fieldValueAssign<TransactDetailField>(DUE_TO_OBSOLETE);
569 bool setToBeUninstalledDueToUpgrade ( TransactByValue causer )
571 if (!setToBeUninstalled (causer)) return false;
572 fieldValueAssign<TransactDetailField>(DUE_TO_UPGRADE);
576 bool setToBeInstalledSoft ( )
579 || !setSoftTransact (true, SOLVER))
582 fieldValueAssign<TransactDetailField>(SOFT_INSTALL);
586 bool setToBeUninstalledSoft ( )
589 || !setSoftTransact (true, SOLVER))
592 fieldValueAssign<TransactDetailField>(SOFT_REMOVE);
596 bool maySetToBeUninstalledSoft ()
598 bit::BitField<FieldType> savBitfield = _bitfield;
599 bool ret = setToBeUninstalledSoft ();
600 _bitfield = savBitfield;
604 bool isSoftInstall () {
605 return fieldValueIs<TransactDetailField> (SOFT_INSTALL);
608 bool isSoftUninstall () {
609 return fieldValueIs<TransactDetailField> (SOFT_REMOVE);
612 bool setSoftInstall (bool flag) {
613 fieldValueAssign<TransactDetailField>(flag?SOFT_INSTALL:0);
617 bool setSoftUninstall (bool flag) {
618 fieldValueAssign<TransactDetailField>(flag?SOFT_REMOVE:0);
622 bool setUndetermined ()
624 fieldValueAssign<ValidateField>(UNDETERMINED);
630 fieldValueAssign<ValidateField>(SATISFIED);
636 fieldValueAssign<ValidateField>(BROKEN);
640 bool setNonRelevant ()
642 fieldValueAssign<ValidateField>(NONRELEVANT);
646 bool setStatus( ResStatus newStatus_r )
648 // State field is immutable!
649 if ( _bitfield.value<StateField>() != newStatus_r._bitfield.value<StateField>() )
651 // Transaction state change allowed?
652 if ( ! setTransactValue( newStatus_r.getTransactValue(), newStatus_r.getTransactByValue() ) )
655 // Ok, we take it all..
656 _bitfield = newStatus_r._bitfield;
660 /** \name Builtin ResStatus constants. */
662 static const ResStatus toBeInstalled;
663 static const ResStatus toBeUninstalled;
664 static const ResStatus toBeUninstalledDueToUpgrade;
665 static const ResStatus toBeUninstalledDueToObsolete;
669 /** Ctor for intialization of builtin constants. */
670 ResStatus( StateValue s,
671 ValidateValue v = UNDETERMINED,
672 TransactValue t = KEEP_STATE,
673 InstallDetailValue i = EXPLICIT_INSTALL,
674 RemoveDetailValue r = EXPLICIT_REMOVE );
676 /** Return whether the corresponding Field has value \a val_r.
678 template<class TField>
679 bool fieldValueIs( FieldType val_r ) const
680 { return _bitfield.isEqual<TField>( val_r ); }
682 /** Set the corresponding Field to value \a val_r.
684 template<class TField>
685 void fieldValueAssign( FieldType val_r )
686 { _bitfield.assign<TField>( val_r ); }
688 /** compare two values.
690 template<class TField>
691 bool isGreaterThan( FieldType val_r )
692 { return _bitfield.value<TField>() > val_r; }
694 template<class TField>
695 bool isLessThan( FieldType val_r )
696 { return _bitfield.value<TField>() < val_r; }
699 friend class resstatus::StatusBackup;
700 BitFieldType _bitfield;
702 ///////////////////////////////////////////////////////////////////
704 /** \relates ResStatus Stream output */
705 std::ostream & operator<<( std::ostream & str, const ResStatus & obj );
707 /** \relates ResStatus Stream output */
708 std::ostream & operator<<( std::ostream & str, ResStatus::TransactValue obj );
710 /** \relates ResStatus Stream output */
711 std::ostream & operator<<( std::ostream & str, ResStatus::TransactByValue obj );
713 /** \relates ResStatus */
714 inline bool operator==( const ResStatus & lhs, const ResStatus & rhs )
715 { return lhs._bitfield == rhs._bitfield; }
717 /** \relates ResStatus */
718 inline bool operator!=( const ResStatus & lhs, const ResStatus & rhs )
719 { return ! (lhs == rhs); }
721 ///////////////////////////////////////////////////////////////////
732 StatusBackup( ResStatus & status_r )
733 : _status( &status_r )
734 , _bitfield( _status->_bitfield )
738 { if ( _status ) _status->_bitfield = _bitfield; }
742 ResStatus::BitFieldType _bitfield;
746 /////////////////////////////////////////////////////////////////
748 ///////////////////////////////////////////////////////////////////
749 #endif // ZYPP_RESSTATUS_H