瀏覽代碼

Common code of all scope guards moved to a base class (scope_exit only

so far).
main
offa 7 年之前
父節點
當前提交
172b435dc4
共有 1 個檔案被更改,包括 25 行新增14 行删除
  1. +25
    -14
      include/scope_exit.h

+ 25
- 14
include/scope_exit.h 查看文件

@@ -25,9 +25,8 @@

namespace sr
{

template<class EF>
class scope_exit
class scope_guard_base
{
public:

@@ -36,8 +35,7 @@ namespace sr
std::enable_if_t<(!std::is_lvalue_reference<EFP>::value)
&& std::is_nothrow_constructible<EF, EFP>::value, int> = 0
>
explicit scope_exit(EFP&& exitFunction) : m_exitFunction(std::move(exitFunction)),
m_execute_on_destruction(true)
explicit scope_guard_base(EFP&& exitFunction) : m_exitFunction(std::move(exitFunction)), m_execute_on_destruction(true)
{
}

@@ -45,8 +43,7 @@ namespace sr
std::enable_if_t<std::is_constructible<EF, EFP>::value, int> = 0,
std::enable_if_t<std::is_lvalue_reference<EFP>::value, int> = 0
>
explicit scope_exit(EFP&& exitFunction) try : m_exitFunction(exitFunction),
m_execute_on_destruction(true)
explicit scope_guard_base(EFP&& exitFunction) try : m_exitFunction(exitFunction), m_execute_on_destruction(true)
{
}
catch( ... )
@@ -55,12 +52,10 @@ namespace sr
throw;
}

scope_exit(const scope_exit&) = delete;

template<class T = EF,
std::enable_if_t<std::is_nothrow_move_constructible<T>::value, int> = 0
>
scope_exit(scope_exit&& other) noexcept(std::is_nothrow_move_constructible<T>::value || std::is_nothrow_copy_constructible<T>::value)
scope_guard_base(scope_guard_base&& other) noexcept(std::is_nothrow_move_constructible<T>::value || std::is_nothrow_copy_constructible<T>::value)
: m_exitFunction(std::move(other.m_exitFunction)),
m_execute_on_destruction(other.m_execute_on_destruction)
{
@@ -70,14 +65,16 @@ namespace sr
template<class T = EF,
std::enable_if_t<!std::is_nothrow_move_constructible<T>::value, int> = 0
>
scope_exit(scope_exit&& other) noexcept(std::is_nothrow_move_constructible<T>::value || std::is_nothrow_copy_constructible<T>::value)
scope_guard_base(scope_guard_base&& other) noexcept(std::is_nothrow_move_constructible<T>::value || std::is_nothrow_copy_constructible<T>::value)
: m_exitFunction(other.m_exitFunction),
m_execute_on_destruction(other.m_execute_on_destruction)
{
other.release();
}

~scope_exit() noexcept(true)
scope_guard_base(const scope_guard_base&) = delete;

~scope_guard_base() noexcept(true)
{
if( m_execute_on_destruction == true )
{
@@ -92,17 +89,31 @@ namespace sr
}


scope_exit& operator=(const scope_exit&) = delete;
scope_exit& operator=(scope_exit&&) = delete;
scope_guard_base& operator=(const scope_guard_base&) = delete;
scope_guard_base& operator=(scope_guard_base&&) = delete;


private:
protected:

EF m_exitFunction;
bool m_execute_on_destruction;
};



template<class EF>
class scope_exit : public scope_guard_base<EF>
{
public:

using scope_guard_base<EF>::scope_guard_base;


private:

};


template<class EF>
scope_exit<std::decay_t<EF>> make_scope_exit(EF&& exitFunction)
{

Loading…
取消
儲存