/*
* 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 "detail/wrapper.h"
#include
#include
namespace sr
{
namespace detail
{
template
using is_ntmocp_constructible = std::conditional_t
|| !std::is_nothrow_move_constructible_v,
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
constexpr auto is_nothrow_swappable_v = std::is_nothrow_move_constructible_v
&& std::is_nothrow_move_assignable_v;
template, T&&, const T&>>
constexpr U forward_if_nothrow_move_constructible(T&& value) noexcept
{
return std::forward(value);
}
}
template
class unique_resource
{
public:
template || std::is_nothrow_move_constructible_v)
&& (std::is_copy_constructible_v || std::is_nothrow_move_constructible_v), 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_v
&& std::is_nothrow_constructible_v)
: m_resource(std::forward(r), scope_exit{[&r, &d] { d(r); }}),
m_deleter(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(detail::forward_if_nothrow_move_constructible(other.m_resource.get()), scope_exit{[] { }}),
m_deleter(detail::forward_if_nothrow_move_constructible(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 swap(unique_resource& other) noexcept(detail::is_nothrow_swappable_v
&& detail::is_nothrow_swappable_v
&& detail::is_nothrow_swappable_v)
{
using std::swap;
swap(m_resource.get(), other.m_resource.get());
swap(m_deleter.get(), other.m_deleter.get());
swap(m_execute_on_destruction, other.m_execute_on_destruction);
}
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 = 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
&& (std::is_class_v>
|| std::is_union_v>), 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)
{
if( this != &other )
{
reset();
if constexpr( std::is_nothrow_move_assignable_v == true )
{
m_deleter.reset(detail::forward_if_nothrow_move_constructible(other.m_deleter.get()));
m_resource.reset(std::forward(other.m_resource.get()));
}
else if constexpr( std::is_nothrow_move_assignable_v == true )
{
m_resource.reset(detail::forward_if_nothrow_move_constructible(other.m_resource.get()));
m_deleter.reset(std::forward(other.m_deleter.get()));
}
else
{
m_resource = other.m_resource;
m_deleter = 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&& r, D&& d) -> unique_resource, std::decay_t>;
template
unique_resource(std::reference_wrapper r, D&& 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};
auto ur = unique_resource{r, d};
if( must_release == true )
{
ur.release();
}
return ur;
}
}