Skip to content

Commit 9fd948e

Browse files
committed
add tmp emacs to gitignore - unique resource file example
1 parent 01775c0 commit 9fd948e

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
# ignore temp build files
12
/compile_commands.json
23
/build
4+
5+
# ignore emacs temp files
6+
*~

examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
22

3-
set(ALL_EXAMPLES scope_example unique_resource)
3+
set(ALL_EXAMPLES scope_example unique_resource unique_resource-file)
44

55
message("Examples to be built: ${ALL_EXAMPLES}")
66

examples/unique_resource-file.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2+
3+
#include <iostream>
4+
#include <memory>
5+
#include <cstdio>
6+
#include <beman/scope/scope.hpp>
7+
8+
//clang-format off
9+
int main() {
10+
11+
{
12+
auto file = beman::scope::unique_resource(
13+
fopen("example.txt", "w"), // Acquire the FILE*
14+
[](FILE* f) {
15+
if (f) {
16+
std::cout << "Closing file.\n";
17+
fclose(f); // Release (cleanup) the resource
18+
}
19+
}
20+
);
21+
22+
if (!file.get()) {
23+
std::cerr << "Failed to open file.\n";
24+
return 1;
25+
}
26+
}
27+
28+
// Resource is automatically released when `file` goes out of scope
29+
std::cout << "File has been closed \n";
30+
31+
}
32+
//clang-format on

0 commit comments

Comments
 (0)