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
26NEFORCE_BEGIN_NAMESPACE__
27
33
44template <typename Func>
46private:
48
49public:
57 template <typename F, enable_if_t<!is_same_v<remove_cvref_t<F>, scope_exit> && is_constructible_v<Func, F> &&
58 !is_nothrow_constructible_v<Func, F>,
59 int> = 0>
60 explicit scope_exit(F&& func) try :
61 func_pair_(exact_arg_construct_tag{}, _NEFORCE forward<F>(func), true) {
62 } catch (...) {
63 func();
64 }
65
73 template <typename F, enable_if_t<!is_same_v<remove_cvref_t<F>, scope_exit> && is_constructible_v<Func, F> &&
74 is_nothrow_constructible_v<Func, F>,
75 int> = 0>
76 explicit scope_exit(F&& func) noexcept :
77 func_pair_(exact_arg_construct_tag{}, _NEFORCE forward<F>(func), true) {}
78
79 scope_exit(const scope_exit&) = delete;
80 scope_exit& operator=(const scope_exit&) = delete;
81
89 func_pair_(move(rhs.func_pair_)) {
90 rhs.release();
91 }
92
93 scope_exit& operator=(scope_exit&&) = delete;
94
101 ~scope_exit() noexcept {
102 if (func_pair_.value) {
103 func_pair_.get_base()();
104 }
105 }
106
112 void release() noexcept { func_pair_.value = false; }
113};
114
115#ifdef NEFORCE_STANDARD_17
116template <typename Func>
118#endif
119
120
129template <typename Func>
131private:
132 compressed_pair<Func, int> func_pair_;
133
134public:
142 template <typename F, enable_if_t<!is_same_v<remove_cvref_t<F>, scope_fail> && is_constructible_v<Func, F> &&
143 !is_nothrow_constructible_v<Func, F>,
144 int> = 0>
145 explicit scope_fail(F&& func) try :
146 func_pair_(exact_arg_construct_tag{}, _NEFORCE forward<F>(func), uncaught_exceptions()) {
147 } catch (...) {
148 func();
149 }
150
156 template <typename F, enable_if_t<!is_same_v<remove_cvref_t<F>, scope_fail> && is_constructible_v<Func, F> &&
157 is_nothrow_constructible_v<Func, F>,
158 int> = 0>
159 explicit scope_fail(F&& func) noexcept :
160 func_pair_(exact_arg_construct_tag{}, _NEFORCE forward<F>(func), uncaught_exceptions()) {}
161
162 scope_fail(const scope_fail&) = delete;
163 scope_fail& operator=(const scope_fail&) = delete;
164
169 scope_fail(scope_fail&& rhs) noexcept :
170 func_pair_(move(rhs.func_pair_)) {
171 rhs.release();
172 }
173
174 scope_fail& operator=(scope_fail&&) = delete;
175
182 ~scope_fail() noexcept {
183 if (uncaught_exceptions() > func_pair_.value) {
184 func_pair_.get_base()();
185 }
186 }
187
193 void release() noexcept { func_pair_.value = numeric_traits<int>::max(); }
194};
195
196#ifdef NEFORCE_STANDARD_17
197template <typename Func>
199#endif
200
201
212template <typename Func>
214private:
215 compressed_pair<Func, int> func_pair_;
216
217public:
223 template <typename F, enable_if_t<!is_same_v<remove_cvref_t<F>, scope_success> && is_constructible_v<Func, F> &&
224 !is_nothrow_constructible_v<Func, F>,
225 int> = 0>
226 explicit scope_success(F&& func) try :
227 func_pair_(exact_arg_construct_tag{}, _NEFORCE forward<F>(func), uncaught_exceptions()) {
228 } catch (...) {
229 func();
230 }
231
237 template <typename F, enable_if_t<!is_same_v<remove_cvref_t<F>, scope_success> && is_constructible_v<Func, F> &&
238 is_nothrow_constructible_v<Func, F>,
239 int> = 0>
240 explicit scope_success(F&& func) noexcept :
241 func_pair_(exact_arg_construct_tag{}, _NEFORCE forward<F>(func), uncaught_exceptions()) {}
242
243 scope_success(const scope_success&) = delete;
244 scope_success& operator=(const scope_success&) = delete;
245
251 func_pair_(move(rhs.func_pair_)) {
252 rhs.release();
253 }
254
255 scope_success& operator=(scope_success&&) = delete;
256
264 if (uncaught_exceptions() <= func_pair_.value) {
265 func_pair_.get_base()();
266 }
267 }
268
274 void release() noexcept { func_pair_.value = -numeric_traits<int>::max(); }
275};
276
277#ifdef NEFORCE_STANDARD_17
278template <typename Func>
280#endif
281 // ScopeGuard
283
284NEFORCE_END_NAMESPACE__
285#endif // NEFORCE_CORE_UTILITY_SCOPE_HPP__
static NEFORCE_NODISCARD 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 >)
移动构造函数
压缩对实现
异常处理框架
NEFORCE_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
int NEFORCE_API uncaught_exceptions() noexcept NEFORCE_PURE_FUNCTION
未捕获的异常数量
NEFORCE_INLINE17 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)))
移动范围元素
NEFORCE_INLINE17 constexpr bool is_nothrow_move_assignable_v
is_nothrow_move_assignable的便捷变量模板
NEFORCE_INLINE17 constexpr bool is_nothrow_move_constructible_v
is_nothrow_move_constructible的便捷变量模板
数值特征
压缩对主模板,使用EBCO优化
精确参数构造标签