瀏覽代碼

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 查看文件



namespace sr namespace sr
{ {

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


std::enable_if_t<(!std::is_lvalue_reference<EFP>::value) std::enable_if_t<(!std::is_lvalue_reference<EFP>::value)
&& std::is_nothrow_constructible<EF, EFP>::value, int> = 0 && 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)
{ {
} }


std::enable_if_t<std::is_constructible<EF, EFP>::value, int> = 0, std::enable_if_t<std::is_constructible<EF, EFP>::value, int> = 0,
std::enable_if_t<std::is_lvalue_reference<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( ... ) catch( ... )
throw; throw;
} }


scope_exit(const scope_exit&) = delete;

template<class T = EF, template<class T = EF,
std::enable_if_t<std::is_nothrow_move_constructible<T>::value, int> = 0 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_exitFunction(std::move(other.m_exitFunction)),
m_execute_on_destruction(other.m_execute_on_destruction) m_execute_on_destruction(other.m_execute_on_destruction)
{ {
template<class T = EF, template<class T = EF,
std::enable_if_t<!std::is_nothrow_move_constructible<T>::value, int> = 0 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_exitFunction(other.m_exitFunction),
m_execute_on_destruction(other.m_execute_on_destruction) m_execute_on_destruction(other.m_execute_on_destruction)
{ {
other.release(); 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 ) if( m_execute_on_destruction == true )
{ {
} }




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; EF m_exitFunction;
bool m_execute_on_destruction; 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> template<class EF>
scope_exit<std::decay_t<EF>> make_scope_exit(EF&& exitFunction) scope_exit<std::decay_t<EF>> make_scope_exit(EF&& exitFunction)
{ {

Loading…
取消
儲存