Browse Source

NoopGuard introduced to simplify the implementation of unique_resource

(#154).
main
offa 5 years ago
parent
commit
f0c723421d
2 changed files with 14 additions and 7 deletions
  1. +11
    -4
      include/detail/wrapper.h
  2. +3
    -3
      include/unique_resource.h

+ 11
- 4
include/detail/wrapper.h View File



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



template<class T> template<class T>
class Wrapper class Wrapper
{ {
public: 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(); g.release();
} }
{ {
public: 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(); g.release();
} }

+ 3
- 3
include/unique_resource.h View File

public: public:


unique_resource() unique_resource()
: resource(R{}, scope_exit{[] { }}),
deleter(D{}, scope_exit{[] { }}),
: resource(R{}),
deleter(D{}),
execute_on_reset(false) execute_on_reset(false)
{ {
} }


unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible_v<R> unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible_v<R>
&& std::is_nothrow_move_constructible_v<D>) && 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] { deleter(std::move_if_noexcept(other.deleter.get()), scope_exit{[&other] {
if( other.execute_on_reset == true ) if( other.execute_on_reset == true )
{ {

Loading…
Cancel
Save