Skip to content

Commit 4c07b17

Browse files
committed
add unique resource test
1 parent 78ff752 commit 4c07b17

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

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)
3+
set(ALL_EXAMPLES scope_example unique_resource)
44

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
11
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
22

3-
int main() {}
3+
#include <iostream>
4+
#include <memory>
5+
#include <beman/scope.hpp>
6+
7+
constexpr size_t arr_size = 10;
8+
9+
int main() {
10+
11+
{
12+
// Allocate an array
13+
auto resoure_ptr = beman::unique_resource
14+
(
15+
new int[arr_szie], // acquire array resource
16+
// Cleanup function
17+
[](int* ptr) { delete[] ptr;
18+
std::cout << "Array deleted.\n"; }
19+
);
20+
21+
// Use the array
22+
for (size_t i = 0; i < size; ++i) {
23+
resource_ptr.get()[i] = static_cast<int>(i * 2);
24+
}
25+
std::cout << "First element: " << resource_ptr.get()[0] << "\n";
26+
}
27+
28+
// Resource is automatically released when `resource_ptr` goes out of scope
29+
std::cout << "After scope: \n";
30+
31+
}
32+

0 commit comments

Comments
 (0)