Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.25)

project(beman.scope DESCRIPTION "Generic Scope Guard" LANGUAGES CXX)

# enable_testing()
enable_testing()

# [CMAKE.SKIP_TESTS]
option(
Expand All @@ -20,14 +20,13 @@ option(
${PROJECT_IS_TOP_LEVEL}
)

include(FetchContent)
include(GNUInstallDirs)

# add_subdirectory(src/beman/scope)
add_subdirectory(src/beman/scope)

#if(BEMAN_SCOPE_BUILD_TESTS)
# add_subdirectory(tests/beman/scope)
#endif()
if(BEMAN_SCOPE_BUILD_TESTS)
add_subdirectory(tests/beman/scope)
Copy link
Member

@JeffGarland JeffGarland Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't we decide to flatten these directories to TOP/tests and TOP/src -- if we didn't we should. Only for headers do we need the distinction. And yes, I wouldn't be surprised if exemplar if wrong

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is not flattened, exemplar is correct here. This is codified in the beman standard, see here.

[DIRECTORY.SOURCES] RECOMMENDATION: Sources and headers not part of the public interface should reside in the top-level src/ directory, and should use the same structure from include/ - e.g., src/beman/<short_name>/. Check CMAKE.AVOID_PASSTHROUGHS.

[DIRECTORY.TESTS] REQUIREMENT: All test files must reside within the top-level tests/ directory, and should use the same structure from include/. If multiple test types are present, subdirectories can be made (e.g., unit tests, performance etc).

I believe this is so that we can do a giant beman build, basically merging all beman projects together and check if they can compile together as a single library.

We decided to not flatten examples directory.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boost does a giant build and doesn't require this -- we should rediscuss this point.

Copy link
Member Author

@wusatosi wusatosi Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the directory structure is not negotiable here. You will have to change the beman standard.

Feel free to bring it up at the next sync.

Note that it might be a little bit too late to change this, all current projects follow this folder structure. You will have to update a lot of current projects if you want to change this rule.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's never too late to change things

endif()

if(BEMAN_SCOPE_BUILD_EXAMPLES)
add_subdirectory(examples)
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# Overview

During the C++20 cycle [P0052 Generic Scope Guard and RAII Wrapper for the Standard Library](https://wg21.link/P0052) added 4 types: `scope_exit`, `scope_fail`, `scope_success` and `unique_resource` to [LTFSv3](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/n4908#scopeguard). In the intervening time, two standard libraries have implemented support as well as Boost. With the imperative for safety and security in C++ developers need every tool in the toolbox. The authors believe it is time to move this facility into the standard. The paper will re-examine the five year old design and any learning from deployment of the LTFSv3.
During the C++20 cycle [P0052 Generic Scope Guard and RAII Wrapper for the Standard Library](https://wg21.link/P0052)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I expressed in discourse I believe this change should be reverted and the linter line length restriction removed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm approving, and if this isn't reverted when you merge I'll fix it in the next PR :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright

added 4 types: `scope_exit`, `scope_fail`, `scope_success`
and `unique_resource` to [LTFSv3](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/n4908#scopeguard).
In the intervening time, two standard libraries have implemented support as well as Boost.
With the imperative for safety and security in C++ developers need every tool in the toolbox.
The authors believe it is time to move this facility into the standard.
The paper will re-examine the five year old design and any learning from deployment of the LTFSv3.

For discussions of this library see:

Expand Down
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ message("Examples to be built: ${ALL_EXAMPLES}")
foreach(example ${ALL_EXAMPLES})
add_executable(beman.scope.examples.${example})
target_sources(beman.scope.examples.${example} PRIVATE ${example}.cpp)
target_link_libraries(beman.scope.examples.${example} beman::scope)
endforeach()
11 changes: 7 additions & 4 deletions examples/scope_example.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <beman/scope/scope.hpp>

#include <cstdlib>
#include <experimental/scope>
#include <iostream>
#include <string_view>

namespace scope = beman::scope;

void print_exit_status(std::string_view name, bool exit_status, bool did_throw) {
std::cout << name << ":\n";
std::cout << " Throwed exception " << (did_throw ? "yes" : "no") << "\n";
Expand Down Expand Up @@ -32,7 +35,7 @@ int main() {
// Using scope_exit: runs on scope exit (success or exception)
exit_status = did_throw = false;
try {
auto guard = std::experimental::scope_exit{[&] { exit_status = true; }};
auto guard = scope::scope_exit{[&] { exit_status = true; }};
maybe_throw();
} catch (...) {
did_throw = true;
Expand All @@ -42,7 +45,7 @@ int main() {
// Using scope_fail: runs only if an exception occurs
exit_status = did_throw = false;
try {
auto guard = std::experimental::scope_fail{[&] { exit_status = true; }};
auto guard = scope::scope_fail{[&] { exit_status = true; }};
maybe_throw();
} catch (...) {
did_throw = true;
Expand All @@ -52,7 +55,7 @@ int main() {
// Using scope_success: runs only if no exception occurs
exit_status = did_throw = false;
try {
auto guard = std::experimental::scope_success{[&] { exit_status = true; }};
auto guard = scope::scope_success{[&] { exit_status = true; }};
maybe_throw();
} catch (...) {
did_throw = true;
Expand Down
3 changes: 1 addition & 2 deletions examples/unique_resource_example.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

int main()
{}
int main() {}
27 changes: 27 additions & 0 deletions include/beman/scope/scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,33 @@ namespace beman::scope {
// template <typename R, typename D>
// unique_resource(R, D) -> unique_resource<R, D>;

// TODO: Implement
struct scope_exit {
template <typename F>
scope_exit(F) {}
~scope_exit() {
// TODO: Cleanup
}
};

// TODO: Implement
struct scope_fail {
template <typename F>
scope_fail(F) {}
~scope_fail() {
// TODO: Cleanup
}
};

// TODO: Implement
struct scope_success {
template <typename F>
scope_success(F) {}
~scope_success() {
// TODO: Cleanup
}
};

} // namespace beman::scope

#endif // BEMAN_SCOPE_HPP
4 changes: 2 additions & 2 deletions src/beman/scope/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
add_library(beman.scope)
add_library(beman::scope ALIAS beman.scope)

target_sources(beman.scope PRIVATE identity.cpp)
target_sources(beman.scope PRIVATE scope.cpp)

target_sources(
beman.scope
PUBLIC
FILE_SET HEADERS
BASE_DIRS ${PROJECT_SOURCE_DIR}/include
FILES ${PROJECT_SOURCE_DIR}/include/beman/scope/identity.hpp
FILES ${PROJECT_SOURCE_DIR}/include/beman/scope/scope.hpp
)

set_target_properties(beman.scope PROPERTIES VERIFY_INTERFACE_HEADER_SETS ON)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <beman/scope/identity.hpp>
#include <beman/scope/scope.hpp>
8 changes: 6 additions & 2 deletions tests/beman/scope/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#add_executable(beman.scope.tests.identity)
#target_sources(beman.scope.tests.identity PRIVATE identity.test.cpp)
add_executable(beman.scope.tests.scope)
target_sources(beman.scope.tests.scope PRIVATE scope.test.cpp)

target_link_libraries(beman.scope.tests.scope PRIVATE beman::scope)

add_test(NAME beman.scope.tests.scope COMMAND beman.scope.tests.scope)
55 changes: 0 additions & 55 deletions tests/beman/scope/identity.test.cpp

This file was deleted.

5 changes: 5 additions & 0 deletions tests/beman/scope/scope.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

int main() {
// TODO: Add tests
}