Skip to content

Commit 4b9ee19

Browse files
franzpoeschelAI Agent
andauthored
HDF5: Close operations also upon failure (#1866)
Replicate a Golang-inspired defer pattern in order to ensure resource cleanup also upon early return. --------- Co-authored-by: AI Agent <ai@anthropic.com>
1 parent cd6ee79 commit 4b9ee19

File tree

6 files changed

+695
-369
lines changed

6 files changed

+695
-369
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@ if(openPMD_BUILD_TESTING)
826826
elseif(${test_name} STREQUAL "Core")
827827
list(APPEND ${out_list}
828828
test/Files_Core/automatic_variable_encoding.cpp
829+
test/Files_Core/read_nonexistent_attribute.cpp
829830
)
830831
endif()
831832
endmacro()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#pragma once
2+
3+
#include <functional>
4+
#include <type_traits>
5+
#include <utility>
6+
7+
namespace openPMD::auxiliary
8+
{
9+
template <typename F>
10+
struct defer_type
11+
{
12+
F functor;
13+
bool do_run_this = true;
14+
~defer_type()
15+
{
16+
if (!do_run_this)
17+
{
18+
return;
19+
}
20+
do_run_this = false;
21+
std::move(functor)();
22+
}
23+
24+
auto to_opaque() && -> defer_type<std::function<void()>>
25+
{
26+
do_run_this = false;
27+
if (!do_run_this)
28+
{
29+
return defer_type<std::function<void()>>{{}, false};
30+
}
31+
else
32+
{
33+
return defer_type<std::function<void()>>{
34+
std::function<void()>{std::move(functor)}};
35+
}
36+
}
37+
};
38+
39+
using opaque_defer_type = defer_type<std::function<void()>>;
40+
41+
template <typename F>
42+
auto defer(F &&functor) -> defer_type<std::remove_reference_t<F>>
43+
{
44+
return defer_type<std::remove_reference_t<F>>{std::forward<F>(functor)};
45+
}
46+
} // namespace openPMD::auxiliary

0 commit comments

Comments
 (0)