/* * 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 { template class scope_exit { public: template::value, int> = 0, std::enable_if_t<(!std::is_lvalue_reference::value) && std::is_nothrow_constructible::value, int> = 0 > explicit scope_exit(D&& deleter) : m_deleter(std::move(deleter)), m_execute_on_destruction(true) { } template::value, int> = 0, std::enable_if_t::value, int> = 0 > explicit scope_exit(D&& deleter) try : m_deleter(deleter), m_execute_on_destruction(true) { } catch( ... ) { deleter(); throw; } scope_exit(const scope_exit&) = delete; template::value, int> = 0 > scope_exit(scope_exit&& other) : m_deleter(std::move(other.m_deleter)), m_execute_on_destruction(other.m_execute_on_destruction) { other.release(); } template::value, int> = 0 > scope_exit(scope_exit&& other) : m_deleter(other.m_deleter), m_execute_on_destruction(other.m_execute_on_destruction) { other.release(); } ~scope_exit() noexcept(true) { if( m_execute_on_destruction == true ) { m_deleter(); } } void release() noexcept { m_execute_on_destruction = false; } scope_exit& operator=(const scope_exit&) = delete; scope_exit& operator=(scope_exit&&) = delete; private: Deleter m_deleter; bool m_execute_on_destruction; }; template scope_exit> make_scope_exit(Deleter&& deleter) { return scope_exit>{std::forward(deleter)}; } }