File tree Expand file tree Collapse file tree 3 files changed +37
-1
lines changed
Expand file tree Collapse file tree 3 files changed +37
-1
lines changed Original file line number Diff line number Diff line change 1+ # ignore temp build files
12/compile_commands.json
23/build
4+
5+ # ignore emacs temp files
6+ * ~
Original file line number Diff line number Diff line change 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
55message ("Examples to be built: ${ALL_EXAMPLES} " )
66
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments