Skip to content

Commit 00ef6eb

Browse files
committed
add module file for scope and modular example
1 parent 0b4314c commit 00ef6eb

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

examples/scope-module.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2+
3+
// This example uses c++20 modules with beman.scope
4+
//
5+
// The following are by hand instructions for compiling with g++-15
6+
// first line generates gcm.cache file for standard headers - one time only
7+
// g++-15 -std=c++26 -O2 -fmodules -fsearch-include-path -fmodule-only -c bits/std.cc
8+
// g++-15 -std=c++26 -O2 -fmodules -fmodule-only -c ${scope_top}/include/beman/scope/beman.scope.cppm
9+
// g++-15 -std=c++26 -fmodules scope-module.cpp
10+
//
11+
// prints:
12+
// --> scope start
13+
// construct noisy
14+
// --> scope end
15+
// destroy noisy
16+
// scope exit: true success: true fail: false
17+
18+
import std;
19+
import beman.scope;
20+
21+
struct noisy_resource
22+
{
23+
noisy_resource() { std::print("construct noisy\n"); }
24+
~noisy_resource() { std::print("destroy noisy\n"); }
25+
};
26+
27+
int main()
28+
{
29+
30+
bool exit_ran, success_ran, fail_ran = false;
31+
{
32+
std::print( "--> scope start\n" );
33+
beman::scope::scope_exit _([&exit_ran] { exit_ran = true; });
34+
beman::scope::scope_success _([&success_ran]{ success_ran = true;});
35+
beman::scope::scope_fail _([&fail_ran] { fail_ran = true; });
36+
auto resource_ptr = beman::scope::unique_resource
37+
(
38+
new noisy_resource(),
39+
// Cleanup function
40+
[](noisy_resource* ptr) { delete ptr; }
41+
);
42+
std::print( "--> scope end\n");
43+
} // Normal scope exit
44+
45+
std::print("scope exit: {} success: {} fail: {} \n",
46+
exit_ran, success_ran, fail_ran);
47+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2+
// create the beman.scope.gcm in gcm.cache directory
3+
// g++-15 -std=c++26 -O2 -fmodules -fmodule-only -c ${scopetop}/include/beman/scope/beman.scope.cppm
4+
module;
5+
6+
#include "scope.hpp"
7+
8+
export module beman.scope;
9+
10+
export namespace beman::scope {
11+
using ::beman::scope::scope_exit;
12+
using ::beman::scope::scope_fail;
13+
using ::beman::scope::scope_success;
14+
using ::beman::scope::unique_resource;
15+
}

0 commit comments

Comments
 (0)