File indexing completed on 2024-05-05 04:38:47

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #ifndef KDEVPLATFORM_PUSHVALUE_H
0008 #define KDEVPLATFORM_PUSHVALUE_H
0009 
0010 #include <QScopedValueRollback>
0011 
0012 /**
0013  * A simple helper-class that does the following:
0014  * Backup the given reference-value given through @p ptr,
0015  * replace it with the value given through @p push,
0016  * restore the backed up value back on destruction.
0017  *
0018  * NOTE: This is _not_ a direct alias of QScopedValueRollback,
0019  *       the behavior of the constructor is different:
0020  *       PushValue will *always* push, PushPositiveValue will only
0021  *       push if the value evaluates to true. QScopedValueRollback
0022  *       will *always* push with the ctor that takes a new value,
0023  *       and *never* with the ctor that just takes a ref.
0024  **/
0025 template<class Value>
0026 class PushValue : public QScopedValueRollback<Value>
0027 {
0028 public:
0029     explicit PushValue(Value& ref, const Value& newValue = Value())
0030         : QScopedValueRollback<Value>(ref, newValue)
0031     {
0032     }
0033 };
0034 
0035 ///Only difference to PushValue: The value is only replaced if the new value is positive
0036 template<class Value>
0037 class PushPositiveValue : public QScopedValueRollback<Value>
0038 {
0039 public:
0040     explicit PushPositiveValue(Value& ref, const Value& newValue = Value())
0041         : QScopedValueRollback<Value>(ref)
0042     {
0043         if (newValue) {
0044             ref = newValue;
0045         }
0046     }
0047 };
0048 
0049 #endif