GCC Code Coverage Report


Directory: ../
File: src/util/DeferredLogic.h
Date: 2024-11-22 23:10:59
Exec Total Coverage
Lines: 10 10 100.0%
Functions: 3 3 100.0%
Branches: 4 4 100.0%

Line Branch Exec Source
1 // Copyright (c) 2021-2024 ChilliBits. All rights reserved.
2
3 #pragma once
4
5 #include <functional>
6 #include <utility>
7
8 namespace spice::compiler {
9
10 struct DeferredLogic {
11 public:
12 // Constructors
13 12649 explicit DeferredLogic(std::function<void()> deferredFunc, bool triggerOnDestruct = true)
14 12649 : deferredFunc(std::move(deferredFunc)), triggerOnDestruct(triggerOnDestruct) {}
15
16 // Destructor
17 12779 ~DeferredLogic() {
18
4/4
✓ Branch 0 taken 12400 times.
✓ Branch 1 taken 379 times.
✓ Branch 2 taken 6545 times.
✓ Branch 3 taken 5855 times.
12779 if (triggerOnDestruct && !alreadyExecuted)
19 6545 deferredFunc();
20 12779 }
21
22 // Public methods
23 6104 void execute() {
24 6104 deferredFunc();
25 6104 alreadyExecuted = true;
26 6104 }
27
28 private:
29 // Private members
30 std::function<void()> deferredFunc;
31 bool triggerOnDestruct;
32 bool alreadyExecuted = false;
33 };
34
35 } // namespace spice::compiler
36