Browse Source

Ctors of scope guard (almost) implemented. Still missing: Correct

behaviour for LValue references (see #41).
main
offa 7 years ago
parent
commit
de9455c2b7
2 changed files with 26 additions and 8 deletions
  1. +19
    -1
      include/scope_guard.h
  2. +7
    -7
      test/ScopeGuardTest.cpp

+ 19
- 1
include/scope_guard.h View File

@@ -21,6 +21,7 @@
#pragma once

#include <utility>
#include <type_traits>

namespace sr
{
@@ -30,11 +31,28 @@ namespace sr
{
public:

explicit scope_guard_t(Deleter&& deleter) noexcept : m_deleter(std::move(deleter)),

template<class D,
std::enable_if_t<std::is_constructible<Deleter, D>::value, int> = 0,
std::enable_if_t<(!std::is_lvalue_reference<D>::value)
&& std::is_nothrow_constructible<Deleter, D>::value, int> = 0
>
explicit scope_guard_t(D&& deleter) noexcept : m_deleter(std::move(deleter)),
m_execute_on_destruction(true)
{
}

template<class D,
std::enable_if_t<std::is_constructible<Deleter, D>::value, int> = 0,
std::enable_if_t<std::is_lvalue_reference<D>::value, int> = 0
>
explicit scope_guard_t(D&& deleter) noexcept : m_deleter(deleter),
m_execute_on_destruction(true)
{
// TODO: Handle copying correctly (#41)
}


scope_guard_t(const scope_guard_t&) = delete;

scope_guard_t(scope_guard_t&& other) noexcept : m_deleter(std::move(other.m_deleter)),

+ 7
- 7
test/ScopeGuardTest.cpp View File

@@ -47,13 +47,6 @@ TEST_CASE("deleter called on destruction", "[ScopeGuard]")
static_cast<void>(guard);
}

TEST_CASE("deleter is not called if released", "[ScopeGuard]")
{
REQUIRE_CALL(m, deleter()).TIMES(0);
auto guard = sr::scope_guard(deleter);
guard.release();
}

TEST_CASE("deleter lambda called on destruction", "[ScopeGuard]")
{
CallMock cm;
@@ -62,6 +55,13 @@ TEST_CASE("deleter lambda called on destruction", "[ScopeGuard]")
static_cast<void>(guard);
}

TEST_CASE("deleter is not called if released", "[ScopeGuard]")
{
REQUIRE_CALL(m, deleter()).TIMES(0);
auto guard = sr::scope_guard(deleter);
guard.release();
}

TEST_CASE("move releases moved-from object", "[ScopeGuard]")
{
REQUIRE_CALL(m, deleter());

Loading…
Cancel
Save