src/util/DeferredLogic.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #include <functional> | ||
| 6 | #include <utility> | ||
| 7 | |||
| 8 | namespace spice::compiler { | ||
| 9 | |||
| 10 | /** | ||
| 11 | * RAII helper that can run a lambda at the end of the scope | ||
| 12 | **/ | ||
| 13 | struct DeferredLogic { | ||
| 14 | // Constructors | ||
| 15 | 19073 | explicit DeferredLogic(std::function<void()> deferredFunc, const bool triggerOnDestruct = true) | |
| 16 | 19073 | : deferredFunc(std::move(deferredFunc)), triggerOnDestruct(triggerOnDestruct) {} | |
| 17 | |||
| 18 | // Destructor | ||
| 19 | 19291 | ~DeferredLogic() { | |
| 20 |
4/4✓ Branch 2 → 3 taken 18677 times.
✓ Branch 2 → 5 taken 614 times.
✓ Branch 3 → 4 taken 9592 times.
✓ Branch 3 → 5 taken 9085 times.
|
19291 | if (triggerOnDestruct && !alreadyExecuted) |
| 21 | 9592 | deferredFunc(); | |
| 22 | 19291 | } | |
| 23 | |||
| 24 | // Public methods | ||
| 25 | 9481 | void execute() { | |
| 26 | 9481 | deferredFunc(); | |
| 27 | 9481 | alreadyExecuted = true; | |
| 28 | 9481 | } | |
| 29 | |||
| 30 | private: | ||
| 31 | // Private members | ||
| 32 | std::function<void()> deferredFunc; | ||
| 33 | bool triggerOnDestruct; | ||
| 34 | bool alreadyExecuted = false; | ||
| 35 | }; | ||
| 36 | |||
| 37 | } // namespace spice::compiler | ||
| 38 |