GCC Code Coverage Report


Directory: ../
File: src/util/DeferredLogic.h
Date: 2025-12-15 05:32:10
Coverage Exec Excl Total
Lines: 100.0% 10 0 10
Functions: 100.0% 3 0 3
Branches: 100.0% 4 0 4

Line Branch Exec Source
1 // Copyright (c) 2021-2025 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 18751 explicit DeferredLogic(std::function<void()> deferredFunc, const bool triggerOnDestruct = true)
16 18751 : deferredFunc(std::move(deferredFunc)), triggerOnDestruct(triggerOnDestruct) {}
17
18 // Destructor
19 18967 ~DeferredLogic() {
20
4/4
✓ Branch 2 → 3 taken 18365 times.
✓ Branch 2 → 5 taken 602 times.
✓ Branch 3 → 4 taken 9423 times.
✓ Branch 3 → 5 taken 8942 times.
18967 if (triggerOnDestruct && !alreadyExecuted)
21 9423 deferredFunc();
22 18967 }
23
24 // Public methods
25 9328 void execute() {
26 9328 deferredFunc();
27 9328 alreadyExecuted = true;
28 9328 }
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