/* * Scope Guard * Copyright (C) 2017 offa * * This file is part of Scope Guard. * * Scope Guard 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. * * Scope Guard 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 Scope Guard. If not, see . */ #pragma once #include #include namespace sr { namespace detail { template struct is_noexcept_dtor { static constexpr bool value = true; }; template constexpr bool is_noexcept_dtor_v = is_noexcept_dtor::value; template class scope_guard_base : private Strategy { public: template, int> = 0, std::enable_if_t<(!std::is_lvalue_reference_v) && std::is_nothrow_constructible_v, int> = 0 > explicit scope_guard_base(EFP&& exitFunction) : m_exitfunction(std::move(exitFunction)), m_execute_on_destruction(true) { } template, int> = 0, std::enable_if_t, int> = 0 > explicit scope_guard_base(EFP&& exitFunction) try : m_exitfunction(exitFunction), m_execute_on_destruction(true) { } catch( ... ) { exitFunction(); throw; } scope_guard_base(scope_guard_base&& other) noexcept(std::is_nothrow_move_constructible_v || std::is_nothrow_copy_constructible_v) : Strategy(other), m_exitfunction(std::move_if_noexcept(other.m_exitfunction)), m_execute_on_destruction(other.m_execute_on_destruction) { other.release(); } scope_guard_base(const scope_guard_base&) = delete; ~scope_guard_base() noexcept(is_noexcept_dtor_v) { if( (m_execute_on_destruction == true) && (this->should_execute() == true) ) { m_exitfunction(); } } void release() noexcept { m_execute_on_destruction = false; } scope_guard_base& operator=(const scope_guard_base&) = delete; scope_guard_base& operator=(scope_guard_base&&) = delete; private: EF m_exitfunction; bool m_execute_on_destruction; }; } }