/* * 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 "scope_exit.h" #include #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::value && std::is_copy_assignable::value), const T&, T &&>> constexpr U move_assign_if_noexcept(T& value) noexcept { return std::move(value); } template::value, T&&, const T&>> constexpr U forward_if_nothrow_move_constructible(T&& value) noexcept { return std::forward(value); } // TODO: Fix old-style casts template struct Wrapper { template::value, int> = 0> explicit Wrapper(TT&& value, G&& g) noexcept(noexcept(Wrapper{(T&&) value})) : Wrapper((T&&) value) { g.release(); } T& get() noexcept { return m_value; } const T& get() const noexcept { return m_value; } void reset(T&& newValue) noexcept(std::is_nothrow_assignable::value) { m_value = move_assign_if_noexcept(newValue); } void reset(const T& newValue) noexcept(std::is_nothrow_assignable::value) { m_value = newValue; } private: Wrapper(const T& value) noexcept(noexcept(T{value})) : m_value(value) { } Wrapper(T&& value) noexcept(noexcept(T{std::move_if_noexcept(value)})) : m_value(std::move_if_noexcept(value)) { } T m_value; }; template struct Wrapper { template::value, int> = 0> explicit Wrapper(TT&& value, G&& g) noexcept(noexcept(static_cast((TT&&) value))) : m_value((T&) value) { g.release(); } T& get() noexcept { return m_value.get(); } const T& get() const noexcept { return m_value.get(); } void reset(T& newValue) noexcept { m_value = std::ref(newValue); } private: std::reference_wrapper m_value; }; template class unique_resource { public: template::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) noexcept(std::is_nothrow_constructible::value && std::is_nothrow_constructible::value) : m_resource(std::forward(r), make_scope_exit([&r, &d] { d(r); })), m_deleter(std::forward
(d), make_scope_exit([this, &d] { d(get()); })), m_execute_on_destruction(true) { } unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible::value && std::is_nothrow_move_constructible::value) : m_resource(forward_if_nothrow_move_constructible(other.m_resource.get()), make_scope_exit([] { })), m_deleter(forward_if_nothrow_move_constructible(other.m_deleter.get()), make_scope_exit([&other] { other.get_deleter()(other.m_resource.get()); other.release(); })), m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false)) { } unique_resource(const unique_resource&) = delete; ~unique_resource() { reset(); } void reset() { if( m_execute_on_destruction == true ) { m_execute_on_destruction = false; get_deleter()(m_resource.get()); } } template void reset(RR&& r) { auto se = make_scope_exit([this, &r] { get_deleter()(r); }); reset(); m_resource.reset(std::forward(r)); m_execute_on_destruction = true; se.release(); } void release() { m_execute_on_destruction = false; } decltype(auto) get() const noexcept { return m_resource.get(); } template::value && ( std::is_class>::value || std::is_union>::value ), int> = 0 > RR operator->() const noexcept { return m_resource.get(); } template::value && !std::is_void>::value), int> = 0> std::add_lvalue_reference_t> operator*() const noexcept { return *get(); } const D& get_deleter() const noexcept { return m_deleter.get(); } template::value || std::is_nothrow_copy_assignable::value) && (std::is_nothrow_copy_assignable
::value || std::is_nothrow_copy_assignable
::value), int> = 0 > unique_resource& operator=(unique_resource&& other) { if( this != &other ) { reset(); m_resource = std::move(other.m_resource); m_deleter = std::move(other.m_deleter); m_execute_on_destruction = std::exchange(other.m_execute_on_destruction, false); } return *this; } unique_resource& operator=(const unique_resource&) = delete; private: Wrapper m_resource; Wrapper m_deleter; bool m_execute_on_destruction; }; template unique_resource, std::decay_t> make_unique_resource(R&& r, D&& d) noexcept(std::is_nothrow_constructible, R>::value && std::is_nothrow_constructible, D>::value) { return unique_resource, std::decay_t>{std::forward(r), std::forward(d)}; } template unique_resource> make_unique_resource(std::reference_wrapper r, D d) noexcept(std::is_nothrow_constructible, D>::value) { return unique_resource>(r.get(), std::forward(d)); } template unique_resource, std::decay_t> make_unique_resource_checked(R&& r, const S& invalid, D&& d) noexcept(std::is_nothrow_constructible, R>::value && std::is_nothrow_constructible, D>::value) { const bool mustRelease{r == invalid}; auto ur = make_unique_resource(r, d); if( mustRelease == true ) { ur.release(); } return ur; } }