Просмотр исходного кода

release() implemented for scope_guard (fixes #4).

main
offa 7 лет назад
Родитель
Сommit
494a662243
2 измененных файлов: 25 добавлений и 2 удалений
  1. +13
    -2
      include/scope_guard.h
  2. +12
    -0
      test/ScopeGuardTest.cpp

+ 13
- 2
include/scope_guard.h Просмотреть файл

@@ -11,7 +11,8 @@ namespace sg
{
public:

explicit scope_guard_t(Deleter&& deleter) noexcept : m_deleter(std::move(deleter))
explicit scope_guard_t(Deleter&& deleter) noexcept : m_deleter(std::move(deleter)),
m_execute_on_destruction(true)
{
}

@@ -20,7 +21,16 @@ namespace sg

~scope_guard_t()
{
m_deleter();
if( m_execute_on_destruction == true )
{
m_deleter();
}
}


void release()
{
m_execute_on_destruction = false;
}


@@ -31,6 +41,7 @@ namespace sg
private:

Deleter m_deleter;
bool m_execute_on_destruction;
};



+ 12
- 0
test/ScopeGuardTest.cpp Просмотреть файл

@@ -14,3 +14,15 @@ TEST_CASE("deleter called on destruction", "[ScopeGuard]")
REQUIRE(executed == true);
}

TEST_CASE("deleter is not called if released", "[ScopeGuard]")
{
bool executed = false;

{
auto guard = sg::scope_guard([&executed] { executed = true; });
guard.release();
}

REQUIRE(executed == false);
}


Загрузка…
Отмена
Сохранить