Parcourir la source

Implementation of scope_fail class and make_scope_fail() factory

function.
main
offa il y a 7 ans
Parent
révision
ca024cdbca
3 fichiers modifiés avec 283 ajouts et 0 suppressions
  1. +122
    -0
      include/scope_fail.h
  2. +2
    -0
      test/CMakeLists.txt
  3. +159
    -0
      test/ScopeFailTest.cpp

+ 122
- 0
include/scope_fail.h Voir le fichier

@@ -0,0 +1,122 @@
/*
* Scope Guard
* Copyright (C) 2017 offa
*
* This file is part of Scope Guard.
*
* Scope Guard is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scope Guard is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scope Guard. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <utility>
#include <type_traits>
#include <exception>

namespace sr
{

template<class Deleter>
class scope_fail
{
public:

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_fail(D&& deleter) : m_deleter(std::move(deleter)),
m_execute_on_destruction(true),
m_uncaught_on_creation(uncaught_exceptions())
{
}

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_fail(D&& deleter) try : m_deleter(deleter),
m_execute_on_destruction(true),
m_uncaught_on_creation(uncaught_exceptions())
{
}
catch( ... )
{
deleter();
throw;
}

scope_fail(const scope_fail&) = delete;

template<class T = Deleter,
std::enable_if_t<std::is_nothrow_move_constructible<T>::value, int> = 0
>
scope_fail(scope_fail&& other) : m_deleter(std::move(other.m_deleter)),
m_execute_on_destruction(other.m_execute_on_destruction),
m_uncaught_on_creation(uncaught_exceptions())
{
other.release();
}

template<class T = Deleter,
std::enable_if_t<!std::is_nothrow_move_constructible<T>::value, int> = 0
>
scope_fail(scope_fail&& other) : m_deleter(other.m_deleter),
m_execute_on_destruction(other.m_execute_on_destruction),
m_uncaught_on_creation(other.m_uncaught_on_creation)
{
other.release();
}

~scope_fail() noexcept(noexcept(std::declval<Deleter>()))
{
if( (m_execute_on_destruction == true) && ( uncaught_exceptions() > m_uncaught_on_creation ) )
{
m_deleter();
}
}


void release() noexcept
{
m_execute_on_destruction = false;
}


scope_fail& operator=(const scope_fail&) = delete;
scope_fail& operator=(scope_fail&&) = delete;


private:

int uncaught_exceptions() const noexcept
{
return ( std::uncaught_exception() == true ? 1 : 0 );
}

Deleter m_deleter;
bool m_execute_on_destruction;
int m_uncaught_on_creation;
};


template<class Deleter>
scope_fail<std::decay_t<Deleter>> make_scope_fail(Deleter&& deleter)
{
return scope_fail<std::decay_t<Deleter>>{std::forward<Deleter>(deleter)};
}

}


+ 2
- 0
test/CMakeLists.txt Voir le fichier

@@ -19,11 +19,13 @@ endfunction()

add_test_suite(ScopeExitTest)
add_test_suite(ScopeSuccessTest)
add_test_suite(ScopeFailTest)
add_test_suite(UniqueResourceTest)


add_custom_target(unittest ScopeExitTest
COMMAND ScopeSuccessTest
COMMAND ScopeFailTest
COMMAND UniqueResourceTest
COMMENT "Running unittests\n\n"
VERBATIM

+ 159
- 0
test/ScopeFailTest.cpp Voir le fichier

@@ -0,0 +1,159 @@
/*
* Scope Guard
* Copyright (C) 2017 offa
*
* This file is part of Scope Guard.
*
* Scope Guard is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scope Guard is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scope Guard. If not, see <http://www.gnu.org/licenses/>.
*/

#include "scope_fail.h"
#include <catch.hpp>
#include <trompeloeil.hpp>

using namespace trompeloeil;

namespace
{
struct CallMock
{
MAKE_MOCK0(deleter, void());
};


struct ThrowOnCopyMock
{
ThrowOnCopyMock() { }

ThrowOnCopyMock(const ThrowOnCopyMock&)
{
throw std::exception{};
}

MAKE_CONST_MOCK0(deleter, void());

void operator()() const
{
this->deleter();
}

ThrowOnCopyMock& operator=(const ThrowOnCopyMock&)
{
throw std::exception{};
}
};


struct NotNothrowMoveMock
{
NotNothrowMoveMock(CallMock* m) : m_mock(m) { }
NotNothrowMoveMock(const NotNothrowMoveMock& other) : m_mock(other.m_mock) { }
NotNothrowMoveMock(NotNothrowMoveMock&& other) noexcept(false) : m_mock(other.m_mock) { }

void operator()() const
{
m_mock->deleter();
}

NotNothrowMoveMock& operator=(const NotNothrowMoveMock&)
{
throw "Not implemented";
}

NotNothrowMoveMock& operator=(NotNothrowMoveMock&&)
{
throw "Not implemented";
}

CallMock* m_mock;

};


CallMock m;


void deleter()
{
m.deleter();
}
}


TEST_CASE("deleter called on destruction", "[ScopeFail]")
{
REQUIRE_CALL(m, deleter()).TIMES(0);
auto guard = sr::make_scope_fail(deleter);
static_cast<void>(guard);
}

TEST_CASE("deleter lambda called on destruction", "[ScopeFail]")
{
CallMock cm;
REQUIRE_CALL(cm, deleter()).TIMES(0);
auto guard = sr::make_scope_fail([&cm] { cm.deleter(); });
static_cast<void>(guard);
}

TEST_CASE("deleter called and rethrow on copy exception", "[ScopeFail]")
{
REQUIRE_THROWS([] {
const ThrowOnCopyMock noMove;
REQUIRE_CALL(noMove, deleter());

sr::scope_fail<decltype(noMove)> guard{noMove};
}());
}

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

TEST_CASE("move releases moved-from object", "[ScopeFail]")
{
REQUIRE_CALL(m, deleter()).TIMES(0);
auto movedFrom = sr::make_scope_fail(deleter);
auto guard = std::move(movedFrom);
static_cast<void>(guard);
}

TEST_CASE("move with copy init releases moved-from object", "[ScopeFail]")
{
CallMock mock;
const NotNothrowMoveMock notNothrow{&mock};
REQUIRE_CALL(mock, deleter()).TIMES(0);
sr::scope_fail<decltype(notNothrow)> movedFrom{notNothrow};
auto guard = std::move(movedFrom);
}

TEST_CASE("move transfers state", "[ScopeFail]")
{
REQUIRE_CALL(m, deleter()).TIMES(0);
auto movedFrom = sr::make_scope_fail(deleter);
auto guard = std::move(movedFrom);
static_cast<void>(guard);
}

TEST_CASE("move transfers state if released", "[ScopeFail]")
{
REQUIRE_CALL(m, deleter()).TIMES(0);
auto movedFrom = sr::make_scope_fail(deleter);
movedFrom.release();
auto guard = std::move(movedFrom);
static_cast<void>(guard);
}


Chargement…
Annuler
Enregistrer