From f1a9e8840fc0da9a5ab6fb14bad3f1d954b827b8 Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Thu, 6 May 2021 14:58:47 -0400 Subject: [PATCH] Add scope_exit utilities --- Builds/CMake/RippledCore.cmake | 1 + src/ripple/basics/scope.h | 191 ++++++++++++++++++++++++++++++++ src/test/basics/scope_test.cpp | 193 +++++++++++++++++++++++++++++++++ 3 files changed, 385 insertions(+) create mode 100644 src/ripple/basics/scope.h create mode 100644 src/test/basics/scope_test.cpp diff --git a/Builds/CMake/RippledCore.cmake b/Builds/CMake/RippledCore.cmake index 6c67da01b..2b126bc5f 100644 --- a/Builds/CMake/RippledCore.cmake +++ b/Builds/CMake/RippledCore.cmake @@ -712,6 +712,7 @@ target_sources (rippled PRIVATE src/test/basics/KeyCache_test.cpp src/test/basics/PerfLog_test.cpp src/test/basics/RangeSet_test.cpp + src/test/basics/scope_test.cpp src/test/basics/Slice_test.cpp src/test/basics/StringUtilities_test.cpp src/test/basics/TaggedCache_test.cpp diff --git a/src/ripple/basics/scope.h b/src/ripple/basics/scope.h new file mode 100644 index 000000000..54c05998f --- /dev/null +++ b/src/ripple/basics/scope.h @@ -0,0 +1,191 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2021 Ripple Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef RIPPLE_BASICS_SCOPE_H_INCLUDED +#define RIPPLE_BASICS_SCOPE_H_INCLUDED + +#include +#include +#include + +namespace ripple { + +// RAII scope helpers. As specified in Library Fundamental, Version 3 +// Basic design of idea: https://www.youtube.com/watch?v=WjTrfoiB0MQ +// Specification: +// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/n4873.html#scopeguard + +// This implementation deviates from the spec slightly: +// The scope_exit and scope_fail constructors taking a functor are not +// permitted to throw an exception. This was done because some compilers +// did not like the superfluous try/catch in the common instantiations +// where the construction was noexcept. Instead a static_assert is used +// to enforce this restriction. + +template +class scope_exit +{ + EF exit_function_; + bool execute_on_destruction_{true}; + +public: + ~scope_exit() + { + if (execute_on_destruction_) + exit_function_(); + } + + scope_exit(scope_exit&& rhs) noexcept( + std::is_nothrow_move_constructible_v || + std::is_nothrow_copy_constructible_v) + : exit_function_{std::forward(rhs.exit_function_)} + , execute_on_destruction_{rhs.execute_on_destruction_} + { + rhs.release(); + } + + scope_exit& + operator=(scope_exit&&) = delete; + + template + explicit scope_exit( + EFP&& f, + std::enable_if_t< + !std::is_same_v, scope_exit> && + std::is_constructible_v>* = 0) noexcept + : exit_function_{std::forward(f)} + { + static_assert( + std:: + is_nothrow_constructible_v(f))>); + } + + void + release() noexcept + { + execute_on_destruction_ = false; + } +}; + +template +scope_exit(EF) -> scope_exit; + +template +class scope_fail +{ + EF exit_function_; + bool execute_on_destruction_{true}; + int uncaught_on_creation_{std::uncaught_exceptions()}; + +public: + ~scope_fail() + { + if (execute_on_destruction_ && + std::uncaught_exceptions() > uncaught_on_creation_) + exit_function_(); + } + + scope_fail(scope_fail&& rhs) noexcept( + std::is_nothrow_move_constructible_v || + std::is_nothrow_copy_constructible_v) + : exit_function_{std::forward(rhs.exit_function_)} + , execute_on_destruction_{rhs.execute_on_destruction_} + , uncaught_on_creation_{rhs.uncaught_on_creation_} + { + rhs.release(); + } + + scope_fail& + operator=(scope_fail&&) = delete; + + template + explicit scope_fail( + EFP&& f, + std::enable_if_t< + !std::is_same_v, scope_fail> && + std::is_constructible_v>* = 0) noexcept + : exit_function_{std::forward(f)} + { + static_assert( + std:: + is_nothrow_constructible_v(f))>); + } + + void + release() noexcept + { + execute_on_destruction_ = false; + } +}; + +template +scope_fail(EF) -> scope_fail; + +template +class scope_success +{ + EF exit_function_; + bool execute_on_destruction_{true}; + int uncaught_on_creation_{std::uncaught_exceptions()}; + +public: + ~scope_success() noexcept(noexcept(exit_function_())) + { + if (execute_on_destruction_ && + std::uncaught_exceptions() <= uncaught_on_creation_) + exit_function_(); + } + + scope_success(scope_success&& rhs) noexcept( + std::is_nothrow_move_constructible_v || + std::is_nothrow_copy_constructible_v) + : exit_function_{std::forward(rhs.exit_function_)} + , execute_on_destruction_{rhs.execute_on_destruction_} + , uncaught_on_creation_{rhs.uncaught_on_creation_} + { + rhs.release(); + } + + scope_success& + operator=(scope_success&&) = delete; + + template + explicit scope_success( + EFP&& f, + std::enable_if_t< + !std::is_same_v, scope_success> && + std::is_constructible_v>* = + 0) noexcept(std::is_nothrow_constructible_v || std::is_nothrow_constructible_v) + : exit_function_{std::forward(f)} + { + } + + void + release() noexcept + { + execute_on_destruction_ = false; + } +}; + +template +scope_success(EF) -> scope_success; + +} // namespace ripple + +#endif diff --git a/src/test/basics/scope_test.cpp b/src/test/basics/scope_test.cpp new file mode 100644 index 000000000..414e67b5c --- /dev/null +++ b/src/test/basics/scope_test.cpp @@ -0,0 +1,193 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github0.com/ripple/rippled + Copyright (c) 2021 Ripple Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#include +#include + +namespace ripple { +namespace test { + +struct scope_test : beast::unit_test::suite +{ + void + test_scope_exit() + { + // scope_exit always executes the functor on destruction, + // unless release() is called + int i = 0; + { + scope_exit x{[&i]() { i = 1; }}; + } + BEAST_EXPECT(i == 1); + { + scope_exit x{[&i]() { i = 2; }}; + x.release(); + } + BEAST_EXPECT(i == 1); + { + scope_exit x{[&i]() { i += 2; }}; + auto x2 = std::move(x); + } + BEAST_EXPECT(i == 3); + { + scope_exit x{[&i]() { i = 4; }}; + x.release(); + auto x2 = std::move(x); + } + BEAST_EXPECT(i == 3); + { + try + { + scope_exit x{[&i]() { i = 5; }}; + throw 1; + } + catch (...) + { + } + } + BEAST_EXPECT(i == 5); + { + try + { + scope_exit x{[&i]() { i = 6; }}; + x.release(); + throw 1; + } + catch (...) + { + } + } + BEAST_EXPECT(i == 5); + } + + void + test_scope_fail() + { + // scope_fail executes the functor on destruction only + // if an exception is unwinding, unless release() is called + int i = 0; + { + scope_fail x{[&i]() { i = 1; }}; + } + BEAST_EXPECT(i == 0); + { + scope_fail x{[&i]() { i = 2; }}; + x.release(); + } + BEAST_EXPECT(i == 0); + { + scope_fail x{[&i]() { i = 3; }}; + auto x2 = std::move(x); + } + BEAST_EXPECT(i == 0); + { + scope_fail x{[&i]() { i = 4; }}; + x.release(); + auto x2 = std::move(x); + } + BEAST_EXPECT(i == 0); + { + try + { + scope_fail x{[&i]() { i = 5; }}; + throw 1; + } + catch (...) + { + } + } + BEAST_EXPECT(i == 5); + { + try + { + scope_fail x{[&i]() { i = 6; }}; + x.release(); + throw 1; + } + catch (...) + { + } + } + BEAST_EXPECT(i == 5); + } + + void + test_scope_success() + { + // scope_success executes the functor on destruction only + // if an exception is not unwinding, unless release() is called + int i = 0; + { + scope_success x{[&i]() { i = 1; }}; + } + BEAST_EXPECT(i == 1); + { + scope_success x{[&i]() { i = 2; }}; + x.release(); + } + BEAST_EXPECT(i == 1); + { + scope_success x{[&i]() { i += 2; }}; + auto x2 = std::move(x); + } + BEAST_EXPECT(i == 3); + { + scope_success x{[&i]() { i = 4; }}; + x.release(); + auto x2 = std::move(x); + } + BEAST_EXPECT(i == 3); + { + try + { + scope_success x{[&i]() { i = 5; }}; + throw 1; + } + catch (...) + { + } + } + BEAST_EXPECT(i == 3); + { + try + { + scope_success x{[&i]() { i = 6; }}; + x.release(); + throw 1; + } + catch (...) + { + } + } + BEAST_EXPECT(i == 3); + } + + void + run() override + { + test_scope_exit(); + test_scope_fail(); + test_scope_success(); + } +}; + +BEAST_DEFINE_TESTSUITE(scope, ripple_basics, ripple); + +} // namespace test +} // namespace ripple