src/util/SaveAndRestore.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | |||
| 7 | namespace spice::compiler { | ||
| 8 | |||
| 9 | /** | ||
| 10 | * RAII helper to save a value and restore it when the object of this class goes out of scope | ||
| 11 | * | ||
| 12 | * @tparam T Value type | ||
| 13 | */ | ||
| 14 | template <typename T> struct SaveAndRestore { | ||
| 15 | // Constructors | ||
| 16 | 1645797 | explicit SaveAndRestore(T &value) : value(value), oldValue(value) {} | |
| 17 |
1/2✓ Branch 3 → 4 taken 1607072 times.
✗ Branch 3 → 5 not taken.
|
1607072 | SaveAndRestore(T &value, const T &newValue) : value(value), oldValue(value) { value = newValue; } |
| 18 | SaveAndRestore(T &value, T &&newValue) : value(value), oldValue(std::move(value)) { value = std::move(newValue); } | ||
| 19 | |||
| 20 | // Destructor | ||
| 21 | 3252869 | ~SaveAndRestore() { value = std::move(oldValue); } | |
| 22 | |||
| 23 | // Public methods | ||
| 24 | const T &get() { return oldValue; } | ||
| 25 | |||
| 26 | private: | ||
| 27 | // Private members | ||
| 28 | T &value; | ||
| 29 | T oldValue; | ||
| 30 | }; | ||
| 31 | |||
| 32 | } // namespace spice::compiler | ||
| 33 |