From patchwork Wed Jan 23 15:21:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 31180 Received: (qmail 63392 invoked by alias); 23 Jan 2019 15:21:39 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 63298 invoked by uid 89); 23 Jan 2019 15:21:38 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, KAM_SHORT, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=decay, wrapped, Requires X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 23 Jan 2019 15:21:36 +0000 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 86302811D5; Wed, 23 Jan 2019 15:21:35 +0000 (UTC) Received: from localhost.localdomain (ovpn04.gateway.prod.ext.ams2.redhat.com [10.39.146.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id A82D567143; Wed, 23 Jan 2019 15:21:34 +0000 (UTC) From: Pedro Alves To: gdb-patches@sourceware.org Cc: Tom Tromey , Andrew Burgess Subject: [PATCH v3 02/17] Introduce scope_exit Date: Wed, 23 Jan 2019 15:21:16 +0000 Message-Id: <20190123152131.29893-3-palves@redhat.com> In-Reply-To: <20190123152131.29893-1-palves@redhat.com> References: <20190123152131.29893-1-palves@redhat.com> This add a new template class scope_exit. scope_exit is a general-purpose scope guard that calls its exit function at the end of the current scope. A scope_exit may be canceled by calling the "release" method. The API is modeled on P0052R5 - Generic Scope Guard and RAII Wrapper for the Standard Library, which is itself based on Andrej Alexandrescu's ScopeGuard/SCOPE_EXIT. The main advantage of scope_exit is avoiding writing single-use RAII classes and its boilerplate. Following patches will remove a few of such classes. There are two forms available: - The "make_scope_exit" form allows canceling the scope guard. Use it like this: auto cleanup = make_scope_exit ( ); ... cleanup.release (); // cancel - If you don't need to cancel the guard, you can use the SCOPE_EXIT macro, like this: SCOPE_EXIT { /* any code you like here. */ } Note: scope_exit instances do not allocate anything on the heap. gdb/ChangeLog: yyyy-mm-dd Pedro Alves Andrew Burgess Tom Tromey * common/scope-exit.h: New file. --- gdb/common/scope-exit.h | 186 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 gdb/common/scope-exit.h diff --git a/gdb/common/scope-exit.h b/gdb/common/scope-exit.h new file mode 100644 index 0000000000..8cdbec305a --- /dev/null +++ b/gdb/common/scope-exit.h @@ -0,0 +1,186 @@ +/* Copyright (C) 2019 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#ifndef COMMON_SCOPE_EXIT_H +#define COMMON_SCOPE_EXIT_H + +#include +#include +#include "common/preprocessor.h" + +/* scope_exit is a general-purpose scope guard that calls its exit + function at the end of the current scope. A scope_exit may be + canceled by calling the "release" method. The API is modeled on + P0052R5 - Generic Scope Guard and RAII Wrapper for the Standard + Library, which is itself based on Andrej Alexandrescu's + ScopeGuard/SCOPE_EXIT. + + There are two forms available: + + - The "make_scope_exit" form allows canceling the scope guard. Use + it like this: + + auto cleanup = make_scope_exit ( ); + ... + cleanup.release (); // cancel + + - If you don't need to cancel the guard, you can use the SCOPE_EXIT + macro, like this: + + SCOPE_EXIT + { + // any code you like here. + } + + See also forward_scope_exit. +*/ + +/* CRTP base class for cancelable scope_exit-like classes. Implements + the common call-custom-function-from-dtor functionality. Classes + that inherit this implement the on_exit() method, which is called + from scope_exit_base's dtor. */ + +template +class scope_exit_base +{ +public: + scope_exit_base () = default; + + ~scope_exit_base () + { + if (!m_released) + { + auto *self = static_cast (this); + self->on_exit (); + } + } + + /* This is needed for make_scope_exit because copy elision isn't + guaranteed until C++17. An optimizing compiler will usually skip + calling this, but it must exist. */ + scope_exit_base (const scope_exit_base &other) + : m_released (other.m_released) + { + other.m_released = true; + } + + void operator= (const scope_exit_base &) = delete; + + /* If this is called, then the wrapped function will not be called + on destruction. */ + void release () noexcept + { + m_released = true; + } + +private: + + /* True if released. Mutable because of the copy ctor hack + above. */ + mutable bool m_released = false; +}; + +/* The scope_exit class. */ + +template +class scope_exit : public scope_exit_base> +{ + /* For access to on_exit(). */ + friend scope_exit_base>; + +public: + + template>> + scope_exit (EFP &&f) + try : m_exit_function ((!std::is_lvalue_reference::value + && std::is_nothrow_constructible::value) + ? std::move (f) + : f) + { + } + catch (...) + { + /* "If the initialization of exit_function throws an exception, + calls f()." */ + f (); + } + + template>> + scope_exit (scope_exit &&rhs) + noexcept (std::is_nothrow_move_constructible::value + || std::is_nothrow_copy_constructible::value) + : m_exit_function (std::is_nothrow_constructible::value + ? std::move (rhs) + : rhs) + { + rhs.release (); + } + + /* This is needed for make_scope_exit because copy elision isn't + guaranteed until C++17. An optimizing compiler will usually skip + calling this, but it must exist. */ + scope_exit (const scope_exit &other) + : scope_exit_base> (other), + m_exit_function (other.m_exit_function) + { + } + + void operator= (const scope_exit &) = delete; + void operator= (scope_exit &&) = delete; + +private: + void on_exit () + { + m_exit_function (); + } + + /* The function to call on scope exit. */ + EF m_exit_function; +}; + +template +scope_exit::type> +make_scope_exit (EF &&f) +{ + return scope_exit::type> (std::forward (f)); +} + +namespace detail +{ + +enum class scope_exit_lhs {}; + +template +scope_exit::type> +operator+ (scope_exit_lhs, EF &&rhs) +{ + return scope_exit::type> (std::forward (rhs)); +} + +} + +/* Register a block of code to run on scope exit. Note that the local + context is captured by reference, which means you should be careful + to avoid inadvertently changing a captured local's value before the + scope exit runs. */ + +#define SCOPE_EXIT \ + auto CONCAT(scope_exit_, __LINE__) = ::detail::scope_exit_lhs () + [&] () + +#endif /* COMMON_SCOPE_EXIT_H */