Преглед на файлове

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

behaviour for LValue references (see #41).
main
offa преди 7 години
родител
ревизия
de9455c2b7
променени са 2 файла, в които са добавени 26 реда и са изтрити 8 реда
  1. +19
    -1
      include/scope_guard.h
  2. +7
    -7
      test/ScopeGuardTest.cpp

+ 19
- 1
include/scope_guard.h Целия файл

@@ -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 Целия файл

@@ -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…
Отказ
Запис