diff --git a/CMakeLists.txt b/CMakeLists.txt index c8e3f7f..f27f0c6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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( @@ -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) +endif() if(BEMAN_SCOPE_BUILD_EXAMPLES) add_subdirectory(examples) diff --git a/README.md b/README.md index 01437a1..712e9a5 100644 --- a/README.md +++ b/README.md @@ -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) +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: diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index fdf5da5..56bd8f4 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -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() diff --git a/examples/scope_example.cpp b/examples/scope_example.cpp index 393c3a0..6595353 100644 --- a/examples/scope_example.cpp +++ b/examples/scope_example.cpp @@ -1,10 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#include + #include -#include #include #include +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"; @@ -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; @@ -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; @@ -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; diff --git a/examples/unique_resource_example.cpp b/examples/unique_resource_example.cpp index 18d8807..67d0f05 100644 --- a/examples/unique_resource_example.cpp +++ b/examples/unique_resource_example.cpp @@ -1,4 +1,3 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -int main() -{} \ No newline at end of file +int main() {} diff --git a/include/beman/scope/scope.hpp b/include/beman/scope/scope.hpp index 3425d16..465aebc 100644 --- a/include/beman/scope/scope.hpp +++ b/include/beman/scope/scope.hpp @@ -93,6 +93,33 @@ namespace beman::scope { // template // unique_resource(R, D) -> unique_resource; +// TODO: Implement +struct scope_exit { + template + scope_exit(F) {} + ~scope_exit() { + // TODO: Cleanup + } +}; + +// TODO: Implement +struct scope_fail { + template + scope_fail(F) {} + ~scope_fail() { + // TODO: Cleanup + } +}; + +// TODO: Implement +struct scope_success { + template + scope_success(F) {} + ~scope_success() { + // TODO: Cleanup + } +}; + } // namespace beman::scope #endif // BEMAN_SCOPE_HPP diff --git a/src/beman/scope/CMakeLists.txt b/src/beman/scope/CMakeLists.txt index 873b69b..c3b27fa 100644 --- a/src/beman/scope/CMakeLists.txt +++ b/src/beman/scope/CMakeLists.txt @@ -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) diff --git a/src/beman/scope/identity.cpp b/src/beman/scope/scope.cpp similarity index 62% rename from src/beman/scope/identity.cpp rename to src/beman/scope/scope.cpp index ad8182c..6912bb7 100644 --- a/src/beman/scope/identity.cpp +++ b/src/beman/scope/scope.cpp @@ -1,3 +1,3 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#include +#include diff --git a/tests/beman/scope/CMakeLists.txt b/tests/beman/scope/CMakeLists.txt index 95af6d3..a3bb0f6 100644 --- a/tests/beman/scope/CMakeLists.txt +++ b/tests/beman/scope/CMakeLists.txt @@ -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) diff --git a/tests/beman/scope/identity.test.cpp b/tests/beman/scope/identity.test.cpp deleted file mode 100644 index 8395bfb..0000000 --- a/tests/beman/scope/identity.test.cpp +++ /dev/null @@ -1,55 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -#include - -#include - -#include -#include - -namespace exe = beman::exemplar; - -TEST(IdentityTest, call_identity_with_int) { - for (int i = -100; i < 100; ++i) { - EXPECT_EQ(i, exe::identity()(i)); - } -} - -TEST(IdentityTest, call_identity_with_custom_type) { - struct S { - int i; - }; - - for (int i = -100; i < 100; ++i) { - const S s{i}; - const S s_id = exe::identity()(s); - EXPECT_EQ(s.i, s_id.i); - } -} - -TEST(IdentityTest, compare_std_vs_beman) { -// Requires: std::identity support. -#if defined(__cpp_lib_identity) - std::identity std_id; - exe::identity beman_id; - for (int i = -100; i < 100; ++i) { - EXPECT_EQ(std_id(i), beman_id(i)); - } -#endif -} - -TEST(IdentityTest, check_is_transparent) { -// Requires: transparent operators support. -#if defined(__cpp_lib_transparent_operators) - - exe::identity id; - - const auto container = {1, 2, 3, 4, 5}; - auto it = std::find(std::begin(container), std::end(container), 3); - EXPECT_EQ(3, *it); - auto it_with_id = std::find(std::begin(container), std::end(container), id(3)); - EXPECT_EQ(3, *it_with_id); - - EXPECT_EQ(it, it_with_id); -#endif -} diff --git a/tests/beman/scope/scope.test.cpp b/tests/beman/scope/scope.test.cpp new file mode 100644 index 0000000..b471b7d --- /dev/null +++ b/tests/beman/scope/scope.test.cpp @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +int main() { + // TODO: Add tests +}