浏览代码

Templateparameter names adapted from P0052.

main
offa 7 年前
父节点
当前提交
30752c93f1
共有 1 个文件被更改,包括 17 次插入17 次删除
  1. +17
    -17
      include/scope_fail.h

+ 17
- 17
include/scope_fail.h 查看文件

namespace sr namespace sr
{ {


template<class Deleter>
template<class EF>
class scope_fail class scope_fail
{ {
public: 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
template<class EFP,
std::enable_if_t<std::is_constructible<EF, EFP>::value, int> = 0,
std::enable_if_t<(!std::is_lvalue_reference<EFP>::value)
&& std::is_nothrow_constructible<EF, EFP>::value, int> = 0
> >
explicit scope_fail(D&& deleter) : m_deleter(std::move(deleter)),
explicit scope_fail(EFP&& deleter) : m_deleter(std::move(deleter)),
m_execute_on_destruction(true), m_execute_on_destruction(true),
m_uncaught_on_creation(uncaught_exceptions()) 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
template<class EFP,
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_fail(D&& deleter) try : m_deleter(deleter),
explicit scope_fail(EFP&& deleter) try : m_deleter(deleter),
m_execute_on_destruction(true), m_execute_on_destruction(true),
m_uncaught_on_creation(uncaught_exceptions()) m_uncaught_on_creation(uncaught_exceptions())
{ {


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


template<class T = Deleter,
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_fail(scope_fail&& other) : m_deleter(std::move(other.m_deleter)), scope_fail(scope_fail&& other) : m_deleter(std::move(other.m_deleter)),
other.release(); other.release();
} }


template<class T = Deleter,
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_fail(scope_fail&& other) : m_deleter(other.m_deleter), scope_fail(scope_fail&& other) : m_deleter(other.m_deleter),
other.release(); other.release();
} }


~scope_fail() noexcept(noexcept(std::declval<Deleter>()))
~scope_fail() noexcept(noexcept(std::declval<EF>()))
{ {
if( (m_execute_on_destruction == true) && ( uncaught_exceptions() > m_uncaught_on_creation ) ) if( (m_execute_on_destruction == true) && ( uncaught_exceptions() > m_uncaught_on_creation ) )
{ {
return ( std::uncaught_exception() == true ? 1 : 0 ); return ( std::uncaught_exception() == true ? 1 : 0 );
} }


Deleter m_deleter;
EF m_deleter;
bool m_execute_on_destruction; bool m_execute_on_destruction;
int m_uncaught_on_creation; int m_uncaught_on_creation;
}; };




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


} }

正在加载...
取消
保存