/* * 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 using is_ntmocp_constructible = std::conditional_t::value || !std::is_nothrow_move_constructible::value, typename std::is_constructible::type, typename std::is_constructible::type>; template constexpr auto is_nothrow_move_or_copy_constructible_from_v = is_ntmocp_constructible::value; template class unique_resource { public: template::value) && std::is_nothrow_constructible::value, int> = 0, std::enable_if_t<(!std::is_lvalue_reference
::value) && std::is_nothrow_constructible::value, int> = 0, std::enable_if_t<(std::is_copy_constructible::value || std::is_nothrow_move_constructible::value) && (std::is_copy_constructible::value || std::is_nothrow_move_constructible::value), int> = 0, std::enable_if_t, int> = 0, std::enable_if_t, int> = 0 > explicit unique_resource(RR&& r, DD&& d) : m_resource(std::move(r)), m_deleter(std::move(d)), m_execute_on_destruction(true) { } template::value || std::is_lvalue_reference
::value, int> = 0, std::enable_if_t<(std::is_copy_constructible::value || std::is_nothrow_move_constructible::value) && (std::is_copy_constructible::value || std::is_nothrow_move_constructible::value), int> = 0, std::enable_if_t, int> = 0, std::enable_if_t, int> = 0 > explicit unique_resource(RR&& r, DD&& d) try : m_resource(r), m_deleter(d), m_execute_on_destruction(true) { } catch( ... ) { d(r); throw; } template::value && std::is_nothrow_move_constructible::value), int> = 0 > unique_resource(unique_resource&& other) : m_resource(std::forward(other.m_resource)), m_deleter(std::forward(other.m_deleter)), m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false)) { } template::value || !std::is_nothrow_move_constructible::value), int> = 0 > unique_resource(unique_resource&& other) : m_resource(other.m_resource), m_deleter(other.m_deleter), m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false)) { } unique_resource(const unique_resource&) = delete; ~unique_resource() { if( m_execute_on_destruction == true ) { m_deleter(m_resource); } } void reset() { if( m_execute_on_destruction == true ) { m_execute_on_destruction = false; m_deleter(m_resource); } } void release() { m_execute_on_destruction = false; } const R& get() const noexcept { return m_resource; } unique_resource& operator=(unique_resource&& other); unique_resource& operator=(const unique_resource&) = delete; private: R m_resource; D m_deleter; bool m_execute_on_destruction; }; template unique_resource, std::decay_t> make_unique_resource(Resource&& r, Deleter&& d) { return unique_resource, std::decay_t>{std::forward(r), std::forward(d)}; } }