Procházet zdrojové kódy

NoopGuard introduced to simplify the implementation of unique_resource

(#154).
main
offa před 5 roky
rodič
revize
f0c723421d
2 změnil soubory, kde provedl 14 přidání a 7 odebrání
  1. +11
    -4
      include/detail/wrapper.h
  2. +3
    -3
      include/unique_resource.h

+ 11
- 4
include/detail/wrapper.h Zobrazit soubor

@@ -27,14 +27,21 @@

namespace sr::detail
{
struct NoopGuard
{
constexpr void release() const
{
}
};


template<class T>
class Wrapper
{
public:

template<class TT, class G, std::enable_if_t<std::is_constructible_v<T, TT>, int> = 0>
Wrapper(TT&& v, G&& g) noexcept(std::is_nothrow_constructible_v<T, TT>) : value(std::forward<TT>(v))
template<class TT, class G = NoopGuard, std::enable_if_t<std::is_constructible_v<T, TT>, int> = 0>
Wrapper(TT&& v, G&& g = G{}) noexcept(std::is_nothrow_constructible_v<T, TT>) : value(std::forward<TT>(v))
{
g.release();
}
@@ -80,8 +87,8 @@ namespace sr::detail
{
public:

template<class TT, class G, std::enable_if_t<std::is_convertible_v<TT, T&>, int> = 0>
Wrapper(TT&& v, G&& g) noexcept(std::is_nothrow_constructible_v<TT, T&>) : value(static_cast<T&>(v))
template<class TT, class G = NoopGuard, std::enable_if_t<std::is_convertible_v<TT, T&>, int> = 0>
Wrapper(TT&& v, G&& g = G{}) noexcept(std::is_nothrow_constructible_v<TT, T&>) : value(static_cast<T&>(v))
{
g.release();
}

+ 3
- 3
include/unique_resource.h Zobrazit soubor

@@ -47,8 +47,8 @@ namespace sr
public:

unique_resource()
: resource(R{}, scope_exit{[] { }}),
deleter(D{}, scope_exit{[] { }}),
: resource(R{}),
deleter(D{}),
execute_on_reset(false)
{
}
@@ -68,7 +68,7 @@ namespace sr

unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible_v<R>
&& std::is_nothrow_move_constructible_v<D>)
: resource(std::move_if_noexcept(other.resource.get()), scope_exit{[] { }}),
: resource(std::move_if_noexcept(other.resource.get())),
deleter(std::move_if_noexcept(other.deleter.get()), scope_exit{[&other] {
if( other.execute_on_reset == true )
{

Načítá se…
Zrušit
Uložit