From 1477e5d6353cf58584e85b1d260d6f08224e547a Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Mon, 9 Sep 2024 13:10:53 -0700 Subject: [PATCH 1/6] feat: :sparkles: Allow specifying when to run a scope guard --- scope_guard.hpp | 67 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 15 deletions(-) diff --git a/scope_guard.hpp b/scope_guard.hpp index 3d67d79..d3ecd77 100644 --- a/scope_guard.hpp +++ b/scope_guard.hpp @@ -8,9 +8,20 @@ #ifndef SCOPE_GUARD_HPP_ #define SCOPE_GUARD_HPP_ +#include #include #include +#if __cplusplus >= 202002L +#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER) +#define SG_NO_UNIQUE_ADRESSS [[msvc::no_unique_address]] +#elif +#define SG_NO_UNIQUE_ADRESSS [[no_unique_address]] +#endif +#elif +#define SG_NO_UNIQUE_ADRESSS +#endif + #if __cplusplus >= 201703L #define SG_NODISCARD [[nodiscard]] #ifdef SG_REQUIRE_NOEXCEPT_IN_CPP17 @@ -22,6 +33,13 @@ namespace sg { + enum class ExitType { + ALWAYS, +#if __cplusplus >= 201411L + ON_SUCCESS, + ON_FAILURE, +#endif + }; namespace detail { /* --- Some custom type traits --- */ @@ -76,10 +94,29 @@ namespace sg std::is_nothrow_destructible> {}; + template class should_run; + template<> class should_run{ + constexpr static bool should_run() { return true; } + }; +#if __cplusplus >= 202002L + template<> class should_run{ + static bool should_run() { return std::uncaught_exceptions() == 0; } + }; + template<> class should_run{ + static bool should_run() { return std::uncaught_exceptions() != 0; } + }; +#elif __cplusplus >= 201411L + template<> class should_run{ + static bool should_run() { return !std::uncaught_exception(); } + }; + template<> class should_run{ + static bool should_run() { return std::uncaught_exception(); } + }; +#endif /* --- The actual scope_guard template --- */ - template::value>::type> class scope_guard; @@ -97,8 +134,8 @@ namespace sg /* --- The template specialization that actually defines the class --- */ - template - class SG_NODISCARD scope_guard final + template + class SG_NODISCARD scope_guard final { public: typedef Callback callback_type; @@ -142,8 +179,8 @@ namespace sg } // namespace sg //////////////////////////////////////////////////////////////////////////////// -template -sg::detail::scope_guard::scope_guard(Callback&& callback) +template +sg::detail::scope_guard::scope_guard(Callback&& callback) noexcept(std::is_nothrow_constructible::value) : m_callback(std::forward(callback)) /* use () instead of {} because of DR 1467 (https://is.gd/WHmWuo), which still impacts older compilers @@ -153,17 +190,17 @@ noexcept(std::is_nothrow_constructible::value) {} //////////////////////////////////////////////////////////////////////////////// -template -sg::detail::scope_guard::scope_guard::~scope_guard() noexcept /* +template +sg::detail::scope_guard::scope_guard::~scope_guard() noexcept /* need the extra injected-class-name here to make different compilers happy */ { - if(m_active) + if(m_active && should_run::should_run()) m_callback(); } //////////////////////////////////////////////////////////////////////////////// -template -sg::detail::scope_guard::scope_guard(scope_guard&& other) +template +sg::detail::scope_guard::scope_guard(scope_guard&& other) noexcept(std::is_nothrow_constructible::value) : m_callback(std::forward(other.m_callback)) // idem , m_active{std::move(other.m_active)} @@ -172,19 +209,19 @@ noexcept(std::is_nothrow_constructible::value) } //////////////////////////////////////////////////////////////////////////////// -template -inline void sg::detail::scope_guard::dismiss() noexcept +template +inline void sg::detail::scope_guard::dismiss() noexcept { m_active = false; } //////////////////////////////////////////////////////////////////////////////// -template +template inline auto sg::detail::make_scope_guard(Callback&& callback) noexcept(std::is_nothrow_constructible::value) --> detail::scope_guard +-> detail::scope_guard { - return detail::scope_guard{std::forward(callback)}; + return detail::scope_guard{std::forward(callback)}; } #endif /* SCOPE_GUARD_HPP_ */ From 752c0b9961a71c97b57607b3f19040a869c2083a Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Mon, 9 Sep 2024 14:25:15 -0700 Subject: [PATCH 2/6] ci: :construction_worker: allow manually running test workflow --- .github/workflows/test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d2ef82b..075e50c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,6 +1,7 @@ name: Test on: + workflow_dispatch: pull_request: push: branches: [ "main" ] From 530c4ece2f62ecee74584099dd8df90497f62520 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Mon, 9 Sep 2024 14:34:02 -0700 Subject: [PATCH 3/6] fix: :bug: Make the code compile --- scope_guard.hpp | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/scope_guard.hpp b/scope_guard.hpp index d3ecd77..9685e46 100644 --- a/scope_guard.hpp +++ b/scope_guard.hpp @@ -12,16 +12,6 @@ #include #include -#if __cplusplus >= 202002L -#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER) -#define SG_NO_UNIQUE_ADRESSS [[msvc::no_unique_address]] -#elif -#define SG_NO_UNIQUE_ADRESSS [[no_unique_address]] -#endif -#elif -#define SG_NO_UNIQUE_ADRESSS -#endif - #if __cplusplus >= 201703L #define SG_NODISCARD [[nodiscard]] #ifdef SG_REQUIRE_NOEXCEPT_IN_CPP17 @@ -95,28 +85,28 @@ namespace sg {}; template class should_run; - template<> class should_run{ + template<> class should_run { constexpr static bool should_run() { return true; } }; #if __cplusplus >= 202002L - template<> class should_run{ + template<> class should_run { static bool should_run() { return std::uncaught_exceptions() == 0; } }; - template<> class should_run{ + template<> class should_run { static bool should_run() { return std::uncaught_exceptions() != 0; } }; #elif __cplusplus >= 201411L - template<> class should_run{ + template<> class should_run { static bool should_run() { return !std::uncaught_exception(); } }; - template<> class should_run{ + template<> class should_run { static bool should_run() { return std::uncaught_exception(); } }; #endif /* --- The actual scope_guard template --- */ - template::value>::type> class scope_guard; @@ -179,7 +169,7 @@ namespace sg } // namespace sg //////////////////////////////////////////////////////////////////////////////// -template +template sg::detail::scope_guard::scope_guard(Callback&& callback) noexcept(std::is_nothrow_constructible::value) : m_callback(std::forward(callback)) /* use () instead of {} because @@ -190,7 +180,7 @@ noexcept(std::is_nothrow_constructible::value) {} //////////////////////////////////////////////////////////////////////////////// -template +template sg::detail::scope_guard::scope_guard::~scope_guard() noexcept /* need the extra injected-class-name here to make different compilers happy */ { @@ -199,7 +189,7 @@ need the extra injected-class-name here to make different compilers happy */ } //////////////////////////////////////////////////////////////////////////////// -template +template sg::detail::scope_guard::scope_guard(scope_guard&& other) noexcept(std::is_nothrow_constructible::value) : m_callback(std::forward(other.m_callback)) // idem @@ -209,14 +199,14 @@ noexcept(std::is_nothrow_constructible::value) } //////////////////////////////////////////////////////////////////////////////// -template +template inline void sg::detail::scope_guard::dismiss() noexcept { m_active = false; } //////////////////////////////////////////////////////////////////////////////// -template +template inline auto sg::detail::make_scope_guard(Callback&& callback) noexcept(std::is_nothrow_constructible::value) -> detail::scope_guard From 462547120a8bc0d1696f5ebf7531d7bd0445f2bd Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Mon, 9 Sep 2024 14:38:00 -0700 Subject: [PATCH 4/6] fix: :bug: Make the code actually compile --- scope_guard.hpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scope_guard.hpp b/scope_guard.hpp index 9685e46..bc34b69 100644 --- a/scope_guard.hpp +++ b/scope_guard.hpp @@ -84,22 +84,22 @@ namespace sg std::is_nothrow_destructible> {}; - template class should_run; - template<> class should_run { + template class ShouldRun; + template<> class ShouldRun { constexpr static bool should_run() { return true; } }; #if __cplusplus >= 202002L - template<> class should_run { + template<> class ShouldRun { static bool should_run() { return std::uncaught_exceptions() == 0; } }; - template<> class should_run { + template<> class ShouldRun { static bool should_run() { return std::uncaught_exceptions() != 0; } }; #elif __cplusplus >= 201411L - template<> class should_run { + template<> class ShouldRun { static bool should_run() { return !std::uncaught_exception(); } }; - template<> class should_run { + template<> class ShouldRun { static bool should_run() { return std::uncaught_exception(); } }; #endif @@ -184,7 +184,7 @@ template sg::detail::scope_guard::scope_guard::~scope_guard() noexcept /* need the extra injected-class-name here to make different compilers happy */ { - if(m_active && should_run::should_run()) + if(m_active && ShouldRun::should_run()) m_callback(); } From 0d6046cbdaf9f408c3f3833ee2e4d32b17676b27 Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Mon, 9 Sep 2024 14:42:41 -0700 Subject: [PATCH 5/6] fix: :bug: Make the code compile, take 3 --- scope_guard.hpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scope_guard.hpp b/scope_guard.hpp index bc34b69..5913b59 100644 --- a/scope_guard.hpp +++ b/scope_guard.hpp @@ -84,22 +84,22 @@ namespace sg std::is_nothrow_destructible> {}; - template class ShouldRun; - template<> class ShouldRun { + template struct ShouldRun; + template<> struct ShouldRun { constexpr static bool should_run() { return true; } }; -#if __cplusplus >= 202002L - template<> class ShouldRun { +#if __cplusplus >= 201703L + template<> struct ShouldRun { static bool should_run() { return std::uncaught_exceptions() == 0; } }; - template<> class ShouldRun { + template<> struct ShouldRun { static bool should_run() { return std::uncaught_exceptions() != 0; } }; #elif __cplusplus >= 201411L - template<> class ShouldRun { + template<> struct ShouldRun { static bool should_run() { return !std::uncaught_exception(); } }; - template<> class ShouldRun { + template<> struct ShouldRun { static bool should_run() { return std::uncaught_exception(); } }; #endif From b4d37e41f73f49c769418fe68f45e23d035afdbe Mon Sep 17 00:00:00 2001 From: ion098 <146852218+ion098@users.noreply.github.com> Date: Wed, 11 Sep 2024 11:11:09 -0700 Subject: [PATCH 6/6] ci: :green_heart: Fix missing compiler in GH tests --- .github/workflows/test.yml | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 075e50c..b0f67f4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -22,16 +22,31 @@ jobs: - os: ubuntu-latest compiler: g++-11 install: g++-11 + - os: ubuntu-latest + compiler: g++-14 + install: g++-14 extra_build_flags: -DENABLE_COVERAGE:BOOL=ON # coverage build - os: ubuntu-20.04 compiler: clang++-7 install: clang-7 - - os: ubuntu-latest + - os: ubuntu-21.04 compiler: clang++-12 - - os: ubuntu-latest + install: clang-12 + - os: ubuntu-21.04 compiler: clang++-13 + install: clang-13 - os: ubuntu-latest compiler: clang++-14 + install: clang-14 + - os: ubuntu-latest + compiler: clang++-15 + install: clang-15 + - os: ubuntu-latest + compiler: clang++-16 + install: clang-16 + - os: ubuntu-latest + compiler: clang++-18 + install: clang-18 - os: macos-11 compiler: g++-10 - os: macos-latest