/*
* Scope Guard
* Copyright (C) 2017-2018 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 "detail/wrapper.h"
#include
#include
namespace sr
{
namespace detail
{
template, U&&, U>
>
constexpr R forward_if_nothrow_constructible(U&& arg)
{
return std::forward(arg);
}
}
template
class unique_resource
{
public:
template && std::is_constructible_v
&& (std::is_nothrow_constructible_v || std::is_constructible_v)
&& (std::is_nothrow_constructible_v || std::is_constructible_v)), int> = 0
>
explicit unique_resource(RR&& r, DD&& d) noexcept((std::is_nothrow_constructible_v || std::is_nothrow_constructible_v)
&& (std::is_nothrow_constructible_v || std::is_nothrow_constructible_v))
: m_resource(detail::forward_if_nothrow_constructible(std::forward(r)), scope_exit{[&r, &d] { d(r); }}),
m_deleter(detail::forward_if_nothrow_constructible(std::forward(d)), scope_exit{[this, &d] { d(get()); }}),
m_execute_on_destruction(true)
{
}
unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible_v
&& std::is_nothrow_move_constructible_v)
: m_resource(std::move_if_noexcept(other.m_resource.get()), scope_exit{[] { }}),
m_deleter(std::move_if_noexcept(other.m_deleter.get()), 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() noexcept
{
if( m_execute_on_destruction == true )
{
m_execute_on_destruction = false;
get_deleter()(m_resource.get());
}
}
template
void reset(RR&& r)
{
reset();
using R1 = typename detail::Wrapper::type;
auto se = scope_exit{[this, &r] { get_deleter()(r); }};
if constexpr( std::is_nothrow_assignable_v == true )
{
m_resource.reset(std::forward(r));
}
else
{
m_resource.reset(std::as_const(r));
}
m_execute_on_destruction = true;
se.release();
}
void release() noexcept
{
m_execute_on_destruction = false;
}
const R& get() const noexcept
{
return m_resource.get();
}
template, int> = 0>
RR operator->() const noexcept
{
return m_resource.get();
}
template
&& !std::is_void_v>), int> = 0>
std::add_lvalue_reference_t> operator*() const noexcept
{
return *get();
}
const D& get_deleter() const noexcept
{
return m_deleter.get();
}
template
|| std::is_nothrow_copy_assignable_v)
&& (std::is_nothrow_move_assignable_v
|| std::is_nothrow_copy_assignable_v), int> = 0
>
unique_resource& operator=(unique_resource&& other) noexcept(std::is_nothrow_assignable_v
&& std::is_nothrow_assignable_v)
{
if( this != &other )
{
reset();
if constexpr( std::is_nothrow_move_assignable_v == true )
{
if constexpr( std::is_nothrow_move_assignable_v == true )
{
m_resource.reset(std::move(other.m_resource));
m_deleter.reset(std::move(other.m_deleter));
}
else
{
m_deleter.reset(other.m_deleter);
m_resource.reset(std::move(other.m_resource));
}
}
else
{
if constexpr( std::is_nothrow_move_assignable_v == true )
{
m_resource.reset(other.m_resource);
m_deleter.reset(std::move(other.m_deleter));
}
else
{
m_resource.reset(other.m_resource);
m_deleter.reset(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:
detail::Wrapper m_resource;
detail::Wrapper m_deleter;
bool m_execute_on_destruction;
};
template
unique_resource(R, D) -> unique_resource;
template>
unique_resource, std::decay_t> make_unique_resource_checked(R&& r, const S& invalid, D&& d)
noexcept(std::is_nothrow_constructible_v, R>
&& std::is_nothrow_constructible_v, D>)
{
const bool must_release{r == invalid};
unique_resource, std::decay_t> ur{std::forward(r), std::forward(d)};
if( must_release == true )
{
ur.release();
}
return ur;
}
}