NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
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 try {
104 if (func_pair_.value) {
105 func_pair_.get_base()();
106 }
107 // NOLINTNEXTLINE(bugprone-empty-catch)
108 } catch (...) {
109 // ignore
110 }
111 }
112
118 void release() noexcept { func_pair_.value = false; }
119};
120
121#ifdef NEFORCE_STANDARD_17
122template <typename Func>
123scope_exit(Func) -> scope_exit<Func>;
124#endif
125
126
135template <typename Func>
137private:
138 compressed_pair<Func, int> func_pair_;
139
140public:
148 template <typename F, enable_if_t<!is_same_v<remove_cvref_t<F>, scope_fail> && is_constructible_v<Func, F> &&
149 !is_nothrow_constructible_v<Func, F>,
150 int> = 0>
151 explicit scope_fail(F&& func) try :
152 func_pair_(exact_arg_construct_tag{}, _NEFORCE forward<F>(func), uncaught_exceptions()) {
153 } catch (...) {
154 func();
155 }
156
162 template <typename F, enable_if_t<!is_same_v<remove_cvref_t<F>, scope_fail> && is_constructible_v<Func, F> &&
163 is_nothrow_constructible_v<Func, F>,
164 int> = 0>
165 explicit scope_fail(F&& func) noexcept :
166 func_pair_(exact_arg_construct_tag{}, _NEFORCE forward<F>(func), uncaught_exceptions()) {}
167
168 scope_fail(const scope_fail&) = delete;
169 scope_fail& operator=(const scope_fail&) = delete;
170
175 scope_fail(scope_fail&& rhs) noexcept :
176 func_pair_(_NEFORCE move(rhs.func_pair_)) {
177 rhs.release();
178 }
179
180 scope_fail& operator=(scope_fail&&) = delete;
181
188 ~scope_fail() noexcept {
189 try {
190 if (uncaught_exceptions() > func_pair_.value) {
191 func_pair_.get_base()();
192 }
193 // NOLINTNEXTLINE(bugprone-empty-catch)
194 } catch (...) {
195 // ignore
196 }
197 }
198
204 void release() noexcept { func_pair_.value = numeric_traits<int>::max(); }
205};
206
207#ifdef NEFORCE_STANDARD_17
208template <typename Func>
209scope_fail(Func) -> scope_fail<Func>;
210#endif
211
212
223template <typename Func>
225private:
226 compressed_pair<Func, int> func_pair_;
227
228public:
234 template <typename F, enable_if_t<!is_same_v<remove_cvref_t<F>, scope_success> && is_constructible_v<Func, F> &&
235 !is_nothrow_constructible_v<Func, F>,
236 int> = 0>
237 explicit scope_success(F&& func) try :
238 func_pair_(exact_arg_construct_tag{}, _NEFORCE forward<F>(func), uncaught_exceptions()) {
239 } catch (...) {
240 func();
241 }
242
248 template <typename F, enable_if_t<!is_same_v<remove_cvref_t<F>, scope_success> && is_constructible_v<Func, F> &&
249 is_nothrow_constructible_v<Func, F>,
250 int> = 0>
251 explicit scope_success(F&& func) noexcept :
252 func_pair_(exact_arg_construct_tag{}, _NEFORCE forward<F>(func), uncaught_exceptions()) {}
253
254 scope_success(const scope_success&) = delete;
255 scope_success& operator=(const scope_success&) = delete;
256
262 func_pair_(_NEFORCE move(rhs.func_pair_)) {
263 rhs.release();
264 }
265
266 scope_success& operator=(scope_success&&) = delete;
267
274 ~scope_success() noexcept {
275 try {
276 if (uncaught_exceptions() <= func_pair_.value) {
277 func_pair_.get_base()();
278 }
279 // NOLINTNEXTLINE(bugprone-empty-catch)
280 } catch (...) {
281 // ignore
282 }
283 }
284
290 void release() noexcept { func_pair_.value = -numeric_traits<int>::max(); }
291};
292
293#ifdef NEFORCE_STANDARD_17
294template <typename Func>
295scope_success(Func) -> scope_success<Func>;
296#endif
297 // ScopeGuard
299
300NEFORCE_END_NAMESPACE__
301#endif // NEFORCE_CORE_UTILITY_SCOPE_HPP__
static constexpr T max() noexcept
获取类型的最大值
作用域退出守卫
~scope_exit() noexcept
析构函数
scope_exit(F &&func)
构造函数(异常不安全)
scope_exit(scope_exit &&rhs) noexcept(is_nothrow_move_constructible_v< Func >)
移动构造函数
scope_exit(F &&func) noexcept
构造函数(异常安全)
void release() noexcept
释放守卫
作用域失败守卫
scope_fail(F &&func) noexcept
构造函数(异常安全)
~scope_fail() noexcept
析构函数
scope_fail(F &&func)
构造函数(异常不安全)
void release() noexcept
释放守卫
scope_fail(scope_fail &&rhs) noexcept
移动构造函数
作用域成功守卫
scope_success(F &&func) noexcept
构造函数(异常安全)
~scope_success() noexcept
析构函数
void release() noexcept
释放守卫
scope_success(scope_success &&rhs) noexcept(is_nothrow_move_assignable_v< Func >)
移动构造函数
scope_success(F &&func)
构造函数(异常不安全)
压缩对实现
异常处理框架
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
int uncaught_exceptions() noexcept
未捕获的异常数量
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
constexpr bool is_nothrow_move_assignable_v
is_nothrow_move_assignable的便捷变量模板
constexpr bool is_nothrow_move_constructible_v
is_nothrow_move_constructible的便捷变量模板
统一调用接口
数值特征
压缩对主模板,使用EBCO优化