OGLplus  (0.59.0) a C++ wrapper for rendering APIs

scope_exit.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_SCOPE_EXIT_HPP
10 #define EAGINE_SCOPE_EXIT_HPP
11 
12 #include "callable_ref.hpp"
13 #include "nothing.hpp"
14 #include <exception>
15 #include <type_traits>
16 
17 namespace eagine {
18 
23 template <typename OnException = nothing_t>
25 public:
27  using action_type = callable_ref<void()>;
28 
30  on_scope_exit(action_type action) noexcept
31  : _action(std::move(action)) {}
32 
34  template <typename Func>
35  on_scope_exit(Func& action) noexcept
36  : _action(construct_from, action) {}
37 
39  on_scope_exit(on_scope_exit&& temp) noexcept
40  : on_scope_exit(temp.release()) {}
41 
43  on_scope_exit(const on_scope_exit&) = delete;
44 
46  auto operator=(on_scope_exit&&) = delete;
47 
49  auto operator=(const on_scope_exit&) = delete;
50 
54  ~on_scope_exit() noexcept(false) {
55  _invoke(OnException());
56  }
57 
61  auto is_active() const noexcept -> bool {
62  return _action.is_valid();
63  }
64 
69  auto release() noexcept -> action_type {
70  return std::move(_action);
71  }
72 
77  void cancel() noexcept {
78  _action = {};
79  }
80 
81 private:
82  action_type _action;
83 
84  void _invoke(std::true_type) const {
85  if(_action) {
86  if(std::uncaught_exceptions()) {
87  try {
88  _action();
89  } catch(...) {
90  }
91  }
92  }
93  }
94 
95  void _invoke(std::false_type) const {
96  if(_action) {
97  if(!std::uncaught_exceptions()) {
98  _action();
99  }
100  }
101  }
102 
103  void _invoke(nothing_t) const {
104  if(_action) {
105  _action();
106  }
107  }
108 };
109 
120 template <typename Func, typename OnException = nothing_t>
122 private:
123  Func _func;
125 
126 public:
129  : _func(std::move(func))
130  , _ose(_func) {}
131 
133  void cancel() noexcept {
134  _ose.cancel();
135  }
136 };
137 
143 template <typename Func>
144 [[nodiscard]] static inline auto finally(Func func)
146  return func;
147 }
148 
149 } // namespace eagine
150 
151 #endif // EAGINE_SCOPE_EXIT_HPP
Declaration of class template storing a reference to a callable object.
Definition: callable_ref.hpp:24
on_scope_exit(action_type action) noexcept
Construction intializing with the specified action.
Definition: scope_exit.hpp:30
Common code is placed in this namespace.
Definition: eagine.hpp:21
on_scope_exit(on_scope_exit &&temp) noexcept
Move constructor.
Definition: scope_exit.hpp:39
auto operator=(on_scope_exit &&)=delete
Not move constructible.
func_on_scope_exit(Func func)
Initialization from the specified callable object.
Definition: scope_exit.hpp:128
callable_ref< void()> action_type
The callable action type.
Definition: scope_exit.hpp:27
Class executing a specified action on scope exit.
Definition: scope_exit.hpp:24
on_scope_exit(Func &action) noexcept
Construction intializing with the specified action.
Definition: scope_exit.hpp:35
Class storing a callable object and an instance of on_scope_exit.
Definition: scope_exit.hpp:121
~on_scope_exit() noexcept(false)
Invokes the stored action, unless it was released or cancelled.
Definition: scope_exit.hpp:54
auto release() noexcept -> action_type
Moves out and returns the stored action.
Definition: scope_exit.hpp:69
auto is_active() const noexcept -> bool
Indicates if there is any on-scope-exit action.
Definition: scope_exit.hpp:61
void cancel() noexcept
Cancels this on scope exit action.
Definition: scope_exit.hpp:133
constexpr const construct_from_t construct_from
The construct-from tag-dispatch constant.
Definition: selector.hpp:47
void cancel() noexcept
Resets the stored action to empty default.
Definition: scope_exit.hpp:77

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).