Browse Source

Strategy extracted for scope exit.

main
offa 7 years ago
parent
commit
176180601d
2 changed files with 16 additions and 5 deletions
  1. +13
    -2
      include/scope_exit.h
  2. +3
    -3
      include/scope_guard_base.h

+ 13
- 2
include/scope_exit.h View File



namespace sr namespace sr
{ {
namespace detail
{
struct scope_exit_strategy
{
constexpr bool should_execute() const
{
return true;
}
};
}



template<class EF> template<class EF>
class scope_exit : public detail::scope_guard_base<EF>
class scope_exit : public detail::scope_guard_base<EF, detail::scope_exit_strategy>
{ {
public: public:


using detail::scope_guard_base<EF>::scope_guard_base;
using detail::scope_guard_base<EF, detail::scope_exit_strategy>::scope_guard_base;




private: private:

+ 3
- 3
include/scope_guard_base.h View File

namespace detail namespace detail
{ {


template<class EF>
class scope_guard_base
template<class EF, class Strategy>
class scope_guard_base : private Strategy
{ {
public: public:




~scope_guard_base() noexcept(true) ~scope_guard_base() noexcept(true)
{ {
if( m_execute_on_destruction == true )
if( m_execute_on_destruction == true && this->should_execute() == true )
{ {
m_exitFunction(); m_exitFunction();
} }

Loading…
Cancel
Save