From 9a2ae3c96fe9d3c9b1da9c0dc73123848ed81706 Mon Sep 17 00:00:00 2001 From: Jeremy Rifkin <51220084+jeremy-rifkin@users.noreply.github.com> Date: Thu, 20 Feb 2025 21:43:25 -0600 Subject: [PATCH] Add some NODISCARD attributes and uncomment scope_exit utility code --- src/platform/dbghelp_utils.hpp | 8 +++--- src/utils/utils.hpp | 48 +++++++++++++++++----------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/platform/dbghelp_utils.hpp b/src/platform/dbghelp_utils.hpp index 57571aa..9092eab 100644 --- a/src/platform/dbghelp_utils.hpp +++ b/src/platform/dbghelp_utils.hpp @@ -5,6 +5,8 @@ || defined(CPPTRACE_GET_SYMBOLS_WITH_DBGHELP) \ || defined(CPPTRACE_DEMANGLE_WITH_WINAPI) +#include "utils/common.hpp" + #include #include @@ -20,8 +22,8 @@ namespace detail { ~dbghelp_syminit_info(); void release(); - static dbghelp_syminit_info make_not_owned(void* handle); - static dbghelp_syminit_info make_owned(void* handle, bool should_close_handle); + NODISCARD static dbghelp_syminit_info make_not_owned(void* handle); + NODISCARD static dbghelp_syminit_info make_owned(void* handle, bool should_close_handle); dbghelp_syminit_info(const dbghelp_syminit_info&) = delete; dbghelp_syminit_info(dbghelp_syminit_info&&); @@ -39,7 +41,7 @@ namespace detail { // - Calls SymInitialize and returns an owning dbghelp_syminit_info which will handle cleanup dbghelp_syminit_info ensure_syminit(); - std::unique_lock get_dbghelp_lock(); + NODISCARD std::unique_lock get_dbghelp_lock(); } } diff --git a/src/utils/utils.hpp b/src/utils/utils.hpp index 3d50bec..50ab134 100644 --- a/src/utils/utils.hpp +++ b/src/utils/utils.hpp @@ -278,31 +278,31 @@ namespace detail { } }; - // template - // class scope_guard { - // F f; - // bool active; - // public: - // scope_guard(F&& f) : f(std::forward(f)), active(true) {} - // ~scope_guard() { - // if(active) { - // f(); - // } - // } - // scope_guard(const scope_guard&) = delete; - // scope_guard(scope_guard&& other) : f(std::move(other.f)), active(exchange(other.active, false)) {} - // scope_guard& operator=(const scope_guard&) = delete; - // scope_guard& operator=(scope_guard&& other) { - // f = std::move(other.f); - // active = exchange(other.active, false); - // return *this; - // } - // }; + template + class scope_guard { + F f; + bool active; + public: + scope_guard(F&& f) : f(std::forward(f)), active(true) {} + ~scope_guard() { + if(active) { + f(); + } + } + scope_guard(const scope_guard&) = delete; + scope_guard(scope_guard&& other) : f(std::move(other.f)), active(exchange(other.active, false)) {} + scope_guard& operator=(const scope_guard&) = delete; + scope_guard& operator=(scope_guard&& other) { + f = std::move(other.f); + active = exchange(other.active, false); + return *this; + } + }; - // template - // auto scope_exit(F&& f) { - // return scope_guard(std::forward(f)); - // } + template + NODISCARD auto scope_exit(F&& f) { + return scope_guard(std::forward(f)); + } } }