// base::AutoReset<> is useful for setting a variable to a new value only within // a particular scope. An base::AutoReset<> object resets a variable to its // original value upon destruction, making it an alternative to writing // "var = false;" or "var = old_val;" at all of a block's exit points. // // This should be obvious, but note that an base::AutoReset<> instance should // have a shorter lifetime than its scoped_variable, to prevent invalid memory // writes when the base::AutoReset<> object is destroyed.
// A constructor that's useful for asserting the old value of // `scoped_variable`, especially when it's inconvenient to check this before // constructing the AutoReset object (e.g. in a class member initializer // list). template <typename U>
AutoReset(T* scoped_variable, U&& new_value, const T& expected_old_value)
: AutoReset(scoped_variable, new_value) {
DCHECK_EQ(original_value_, expected_old_value);
}
~AutoReset() { if (scoped_variable_)
*scoped_variable_ = std::move(original_value_);
}
private: // `scoped_variable_` is not a raw_ptr<T> for performance reasons: Large // number of non-PartitionAlloc pointees + AutoReset is typically short-lived // (e.g. allocated on the stack).
RAW_PTR_EXCLUSION T* scoped_variable_;
T original_value_;
};
} // namespace base
#endif// BASE_AUTO_RESET_H_
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-08-25)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.