NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
scope.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_UTILITY_SCOPE_HPP__
2#define NEFORCE_CORE_UTILITY_SCOPE_HPP__
3
22
27NEFORCE_BEGIN_NAMESPACE__
28
34
45template <typename Func>
47private:
49
50public:
58 template <typename F, enable_if_t<!is_same_v<remove_cvref_t<F>, scope_exit> && is_constructible_v<Func, F> &&
59 !is_nothrow_constructible_v<Func, F>,
60 int> = 0>
61 explicit scope_exit(F&& func) try :
62 func_pair_(exact_arg_construct_tag{}, _NEFORCE forward<F>(func), true) {
63 } catch (...) {
64 func();
65 }
66
74 template <typename F, enable_if_t<!is_same_v<remove_cvref_t<F>, scope_exit> && is_constructible_v<Func, F> &&
75 is_nothrow_constructible_v<Func, F>,
76 int> = 0>
77 explicit scope_exit(F&& func) noexcept :
78 func_pair_(exact_arg_construct_tag{}, _NEFORCE forward<F>(func), true) {}
79
80 scope_exit(const scope_exit&) = delete;
81 scope_exit& operator=(const scope_exit&) = delete;
82
90 func_pair_(_NEFORCE move(rhs.func_pair_)) {
91 rhs.release();
92 }
93
94 scope_exit& operator=(scope_exit&&) = delete;
95
102 ~scope_exit() noexcept {
103 if (func_pair_.value) {
104 func_pair_.get_base()();
105 }
106 }
107
113 void release() noexcept { func_pair_.value = false; }
114};
115
116#ifdef NEFORCE_STANDARD_17
117template <typename Func>
119#endif
120
121
130template <typename Func>
132private:
133 compressed_pair<Func, int> func_pair_;
134
135public:
143 template <typename F, enable_if_t<!is_same_v<remove_cvref_t<F>, scope_fail> && is_constructible_v<Func, F> &&
144 !is_nothrow_constructible_v<Func, F>,
145 int> = 0>
146 explicit scope_fail(F&& func) try :
147 func_pair_(exact_arg_construct_tag{}, _NEFORCE forward<F>(func), uncaught_exceptions()) {
148 } catch (...) {
149 func();
150 }
151
157 template <typename F, enable_if_t<!is_same_v<remove_cvref_t<F>, scope_fail> && is_constructible_v<Func, F> &&
158 is_nothrow_constructible_v<Func, F>,
159 int> = 0>
160 explicit scope_fail(F&& func) noexcept :
161 func_pair_(exact_arg_construct_tag{}, _NEFORCE forward<F>(func), uncaught_exceptions()) {}
162
163 scope_fail(const scope_fail&) = delete;
164 scope_fail& operator=(const scope_fail&) = delete;
165
170 scope_fail(scope_fail&& rhs) noexcept :
171 func_pair_(_NEFORCE move(rhs.func_pair_)) {
172 rhs.release();
173 }
174
175 scope_fail& operator=(scope_fail&&) = delete;
176
183 ~scope_fail() noexcept {
184 if (uncaught_exceptions() > func_pair_.value) {
185 func_pair_.get_base()();
186 }
187 }
188
194 void release() noexcept { func_pair_.value = numeric_traits<int>::max(); }
195};
196
197#ifdef NEFORCE_STANDARD_17
198template <typename Func>
200#endif
201
202
213template <typename Func>
215private:
216 compressed_pair<Func, int> func_pair_;
217
218public:
224 template <typename F, enable_if_t<!is_same_v<remove_cvref_t<F>, scope_success> && is_constructible_v<Func, F> &&
225 !is_nothrow_constructible_v<Func, F>,
226 int> = 0>
227 explicit scope_success(F&& func) try :
228 func_pair_(exact_arg_construct_tag{}, _NEFORCE forward<F>(func), uncaught_exceptions()) {
229 } catch (...) {
230 func();
231 }
232
238 template <typename F, enable_if_t<!is_same_v<remove_cvref_t<F>, scope_success> && is_constructible_v<Func, F> &&
239 is_nothrow_constructible_v<Func, F>,
240 int> = 0>
241 explicit scope_success(F&& func) noexcept :
242 func_pair_(exact_arg_construct_tag{}, _NEFORCE forward<F>(func), uncaught_exceptions()) {}
243
244 scope_success(const scope_success&) = delete;
245 scope_success& operator=(const scope_success&) = delete;
246
252 func_pair_(_NEFORCE move(rhs.func_pair_)) {
253 rhs.release();
254 }
255
256 scope_success& operator=(scope_success&&) = delete;
257
265 if (uncaught_exceptions() <= func_pair_.value) {
266 func_pair_.get_base()();
267 }
268 }
269
275 void release() noexcept { func_pair_.value = -numeric_traits<int>::max(); }
276};
277
278#ifdef NEFORCE_STANDARD_17
279template <typename Func>
281#endif
282 // ScopeGuard
284
285NEFORCE_END_NAMESPACE__
286#endif // NEFORCE_CORE_UTILITY_SCOPE_HPP__
static constexpr T max() noexcept
获取类型的最大值
作用域退出守卫
scope_exit(scope_exit &&rhs) noexcept(is_nothrow_move_constructible_v< Func >)
移动构造函数
void release() noexcept
释放守卫
scope_exit(F &&func) noexcept
构造函数(异常安全)
~scope_exit() noexcept
析构函数
scope_exit(F &&func)
构造函数(异常不安全)
作用域失败守卫
scope_fail(scope_fail &&rhs) noexcept
移动构造函数
scope_fail(F &&func) noexcept
构造函数(异常安全)
~scope_fail() noexcept
析构函数
void release() noexcept
释放守卫
scope_fail(F &&func)
构造函数(异常不安全)
作用域成功守卫
void release() noexcept
释放守卫
~scope_success() noexcept(is_nothrow_invocable_v< Func >)
析构函数
scope_success(F &&func)
构造函数(异常不安全)
scope_success(F &&func) noexcept
构造函数(异常安全)
scope_success(scope_success &&rhs) noexcept(is_nothrow_move_assignable_v< Func >)
移动构造函数
压缩对实现
异常处理框架
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
int uncaught_exceptions() noexcept
未捕获的异常数量
constexpr bool is_nothrow_invocable_v
is_nothrow_invocable的便捷变量模板
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
constexpr bool is_nothrow_move_constructible_v
is_nothrow_move_constructible的便捷变量模板
constexpr bool is_nothrow_move_assignable_v
is_nothrow_move_assignable的便捷变量模板
统一调用接口
数值特征
压缩对主模板,使用EBCO优化
精确参数构造标签