Skip to content

Commit c3ad24d

Browse files
committed
Allow function as finalizer
1 parent f0c9d20 commit c3ad24d

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

internals/testing/finally_test.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
using namespace testing_v1;
66

7-
auto finally_test = test([]() {
7+
auto finally_lambda_test = test([]() {
88
bool finalized = false;
99
bool executed = false;
1010

@@ -16,3 +16,18 @@ auto finally_test = test([]() {
1616
verify(finalized);
1717
verify(executed);
1818
});
19+
20+
static bool s_finalized = false;
21+
static void s_function() { s_finalized = true; }
22+
23+
auto finally_function_test = test([]() {
24+
bool executed = false;
25+
26+
{
27+
auto finalizer = dumpster_v1::finally(s_function);
28+
executed = true;
29+
}
30+
31+
verify(s_finalized);
32+
verify(executed);
33+
});

provides/include/dumpster_v1/finally.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
template <class Action> struct dumpster_v1::finally_t {
88
~finally_t() { m_action(); }
99

10-
finally_t(Action &&action) : m_action(std::move(action)) {}
11-
finally_t(const Action &action) : m_action(action) {}
10+
template <class ForwardableAction>
11+
finally_t(ForwardableAction &&action)
12+
: m_action(std::forward<ForwardableAction>(action)) {}
1213

1314
finally_t(const finally_t &) = delete;
1415
finally_t operator=(const finally_t &) = delete;
1516

1617
private:
17-
Action m_action;
18+
std::conditional_t<std::is_function_v<Action>, Action *, Action> m_action;
1819
};
1920

2021
template <class Action>

0 commit comments

Comments
 (0)