YSTest: scope_guard.hpp 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

scope_guard.hpp
浏览该文件的文档.
1 /*
2 © 2015-2016 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
30 #ifndef YB_INC_ystdex_scope_guard_hpp_
31 #define YB_INC_ystdex_scope_guard_hpp_ 1
32
33 #include "type_traits.hpp" // for is_constructible, is_reference,
34 // is_nothrow_swappable, std::swap, std::declval, is_nothrow_copyable;
35 #include "base.h" // for noncopyable;
36 #include "memory.hpp" // for ystdex::construct_in, ystdex::destruct_in;
37
38 namespace ystdex
39 {
40
42
43
47 template<typename _func, bool _bNoThrow = true>
48 struct guard
49 {
50 _func func;
51
53 template<typename... _tParams>
54 guard(_tParams&&... args)
55 ynoexcept(is_constructible<_func, _tParams&&...>::value)
56 : func(yforward(args)...)
57 {}
58 guard(guard&&) = default;
59 ~guard() ynoexcept_spec(!_bNoThrow)
60 {
61 func();
62 }
63
64 guard&
65 operator=(guard&&) = default;
66 };
67
73 template<typename _type, bool _bNoThrow = true, typename... _tParams>
75 guard<_type>
76 make_guard(_tParams&&... args) ynoexcept_spec(guard<_type, _bNoThrow>(
77 guard<_type, _bNoThrow>(yforward(args)...)))
78 {
79 return guard<_type, _bNoThrow>(yforward(args)...);
80 }
81 template<bool _bNoThrow = true, typename _type>
82 guard<_type>
83 make_guard(_type f)
84 ynoexcept_spec(guard<_type, _bNoThrow>(guard<_type, _bNoThrow>(f)))
85 {
86 return guard<_type, _bNoThrow>(f);
87 }
88
90 template<bool _bNoThrow = true, typename _type>
91 guard<one_shot<_type>>
92 unique_guard(_type f) ynoexcept_spec(
93 guard<one_shot<_type>, _bNoThrow>(guard<one_shot<_type>, _bNoThrow>(f)))
94 {
95 return guard<one_shot<_type>, _bNoThrow>(f, true);
96 }
98
99
104 template<typename _func, typename _type = void>
105 inline std::shared_ptr<_type>
106 share_guard(_func f, _type* p = {})
107 {
108 return ystdex::share_raw(p, f);
109 }
110
111
112 namespace details
113 {
114
116
117 template<typename _type, typename _tToken,
118 bool _bRef = is_reference<_tToken>::value>
119 struct state_guard_traits
120 {
122
123 static void
124 save(_tToken t, _type& val) ynoexcept_spec(t(true, val))
125 {
126 t(true, val);
127 }
128
129 static void
130 restore(_tToken t, _type& val) ynoexcept_spec(t(false, val))
131 {
132 t(false, val);
133 }
135 };
136
137 template<typename _type, typename _tToken>
138 struct state_guard_traits<_type, _tToken, true>
139 {
141
142 static void
143 save(_tToken t, _type& val) ynoexcept(is_nothrow_swappable<_type>())
144 {
145 using std::swap;
146
147 swap(val, static_cast<_type&>(t));
148 }
149
150 static void
151 restore(_tToken t, _type& val) ynoexcept(is_nothrow_swappable<_type>())
152 {
153 using std::swap;
154
155 swap(val, static_cast<_type&>(t));
156 }
158 };
159
160
161 template<typename _type, typename _tToken>
162 struct state_guard_impl
163 : private state_guard_traits<_type, _tToken>, private noncopyable
164 {
165 using value_type = _type;
166 using token_type = _tToken;
167
168 token_type token;
169 union
170 {
171 value_type value;
172 };
173
174 state_guard_impl(token_type t)
175 : token(t)
176 {}
177 ~state_guard_impl()
178 {}
179
181 template<typename... _tParams>
182 void
183 construct_and_save(_tParams&&... args)
184 ynoexcept(noexcept(value_type(yforward(args)...))
185 && noexcept(std::declval<state_guard_impl&>().save()))
186 {
187 ystdex::construct_in(value, yforward(args)...);
188 save();
189 }
190
192 void
193 destroy() ynoexcept(is_nothrow_destructible<value_type>())
194 {
195 ystdex::destruct_in(value);
196 }
197
198 void
199 save() ynoexcept(noexcept(state_guard_traits<value_type, token_type>
200 ::save(std::declval<token_type&>(), std::declval<value_type&>())))
201 {
202 state_guard_traits<value_type, token_type>::save(token, value);
203 }
204
205 void
206 restore()
207 ynoexcept_spec(state_guard_traits<value_type, token_type>::restore(
208 std::declval<token_type&>(), std::declval<value_type&>()))
209 {
210 state_guard_traits<value_type, token_type>::restore(token, value);
211 }
212
213 void
214 restore_and_destroy()
215 ynoexcept_spec(state_guard_traits<value_type, token_type>::restore(
216 std::declval<token_type&>(), std::declval<value_type&>()))
217 {
218 restore();
219 destroy();
220 }
221 };
223
224 } // namespace details;
225
233 template<typename _type, typename _tCond = bool,
234 typename _tToken = std::function<void(bool, _type&)>>
235 class state_guard : private details::state_guard_impl<_type, _tToken>
236 {
237 static_assert(is_nothrow_copy_constructible<_tCond>(),
238 "Invalid condition type found.");
239
240 private:
241 using base = details::state_guard_impl<_type, _tToken>;
242
243 public:
244 using typename base::value_type;
245 using typename base::token_type;
246 using condition_type = _tCond;
247
248 using base::token;
249 using base::value;
250
251 private:
253 condition_type enabled{};
254
255 public:
257
258 template<typename... _tParams>
259 state_guard(condition_type cond, token_type t, _tParams&&... args)
260 ynoexcept(and_<is_nothrow_constructible<base, token_type>,
261 is_nothrow_copy_constructible<condition_type>>()
262 && noexcept(std::declval<state_guard&>()
263 .base::construct_and_save(yforward(args)...)))
264 : base(t),
265 enabled(cond)
266 {
267 if(enabled)
268 base::construct_and_save(yforward(args)...);
269 }
270 ~state_guard() ynoexcept(is_nothrow_copy_constructible<condition_type>()
271 && noexcept(std::declval<state_guard&>().base::restore_and_destroy()))
272 {
273 if(enabled)
274 base::restore_and_destroy();
275 }
277
279 void
280 dismiss() ynoexcept(is_nothrow_copyable<condition_type>())
281 {
282 if(enabled)
283 base::destroy();
284 enabled = condition_type();
285 }
286
288 condition_type
289 engaged() const ynothrow
290 {
291 return engaged;
292 }
293 };
294
295 template<typename _type, typename _tToken>
296 class state_guard<_type, void, _tToken>
297 : private details::state_guard_impl<_type, _tToken>
298 {
299 private:
300 using base = details::state_guard_impl<_type, _tToken>;
301
302 public:
303 using typename base::value_type;
304 using typename base::token_type;
305 using condition_type = void;
306
307 using base::token;
308 using base::value;
309
310 template<typename... _tParams>
311 state_guard(token_type t, _tParams&&... args) ynoexcept(
312 is_nothrow_constructible<base, token_type>() && noexcept(std::declval<
313 state_guard&>().base::construct_and_save(yforward(args)...)))
314 : base(t)
315 {
316 base::construct_and_save(yforward(args)...);
317 }
319 ~state_guard()
320 ynoexcept_spec(std::declval<state_guard&>().base::restore_and_destroy())
321 {
322 base::restore_and_destroy();
323 }
324 };
326
327
334 template<typename _type, typename _tCond = bool, typename _tReference = _type&>
335 using swap_guard = state_guard<_type, _tCond, _tReference>;
336
337 } // namespace ystdex;
338
339 #endif
340
ystdex::details::state_guard_impl::restore
void restore()
Definition: scope_guard.hpp:206
platform::p
while p p
Definition: YCLib/FileSystem.h:225
ystdex::state_guard::~state_guard
~state_guard()
Definition: scope_guard.hpp:270
ystdex::share_guard
std::shared_ptr< _type > share_guard(_func f, _type *p={})
创建共享作用域守护。
Definition: scope_guard.hpp:106
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:740
ystdex::details::state_guard_impl::token
token_type token
Definition: scope_guard.hpp:168
ystdex::details::state_guard_impl::value
value_type value
Definition: scope_guard.hpp:171
ystdex::swap
void swap(container_adaptor< _tSeqCon > &x, container_adaptor< _tSeqCon > &y)
Definition: container.hpp:179
ystdex::details::state_guard_impl::construct_and_save
void construct_and_save(_tParams &&...args)
Definition: scope_guard.hpp:183
ystdex::state_guard< _type, void, _tToken >::condition_type
void condition_type
Definition: scope_guard.hpp:305
ystdex::details::state_guard_traits::save
static void save(_tToken t, _type &val)
Definition: scope_guard.hpp:124
ystdex::one_shot
调用一次的函数包装模板。
Definition: functional.hpp:664
ystdex::details::state_guard_traits::restore
static void restore(_tToken t, _type &val)
Definition: scope_guard.hpp:130
ystdex::details::state_guard_impl
Definition: scope_guard.hpp:162
ystdex::guard::operator=
guard & operator=(guard &&)=default
ystdex::details::state_guard_impl::restore_and_destroy
void restore_and_destroy()
Definition: scope_guard.hpp:214
ystdex::details::state_guard_impl::destroy
void destroy()
Definition: scope_guard.hpp:193
ystdex::details::state_guard_impl::state_guard_impl
state_guard_impl(token_type t)
Definition: scope_guard.hpp:174
ystdex::state_guard
使用临时状态暂存对象的作用域守护。
Definition: scope_guard.hpp:235
ystdex::details::state_guard_traits< _type, _tToken, true >::save
static void save(_tToken t, _type &val)
Definition: scope_guard.hpp:143
ystdex::is_nothrow_swappable
Definition: type_traits.hpp:530
ystdex::state_guard::condition_type
_tCond condition_type
Definition: scope_guard.hpp:246
ynoexcept_spec
#define ynoexcept_spec(...)
表达式 noexcept 异常规范。
Definition: ydef.h:768
ystdex::guard::func
_func func
Definition: scope_guard.hpp:50
ystdex::share_raw
std::shared_ptr< _type > share_raw(_type *p, _tParams &&...args)
Definition: memory.hpp:664
ystdex::construct_in
void construct_in(_type &obj, _tParams &&...args)
原地构造。
Definition: memory.hpp:164
base.h
基类实用设施。
std
Definition: memory.hpp:1100
ystdex::guard
作用域守护：析构时调用保存的函数对象或引用。
Definition: scope_guard.hpp:48
ystdex
特性测试宏。
Definition: ydef.h:796
ystdex::details::state_guard_traits
Definition: scope_guard.hpp:119
memory.hpp
存储和智能指针特性。
ystdex::state_guard::enabled
condition_type enabled
Definition: scope_guard.hpp:253
ystdex::details::state_guard_impl::save
void save()
Definition: scope_guard.hpp:199
ystdex::guard::guard
guard(_tParams &&...args)
Definition: scope_guard.hpp:54
ystdex::guard::~guard
~guard()
Definition: scope_guard.hpp:59
type_traits.hpp
ISO C++ 类型特征扩展。
ystdex::destruct_in
void destruct_in(_type &obj)
原地析构。
Definition: memory.hpp:218
ystdex::is_nothrow_copyable
Definition: type_traits.hpp:889
ystdex::state_guard::engaged
condition_type engaged() const
Definition: scope_guard.hpp:289
ystdex::state_guard::state_guard
state_guard(condition_type cond, token_type t, _tParams &&...args)
Definition: scope_guard.hpp:259
ynoexcept
#define ynoexcept(...)
YSLib 无异常抛出保证：指定特定的异常规范。
Definition: ydef.h:720
ystdex::guard::make_guard
guard< _type > make_guard(_tParams &&...args)
创建作用域守护。
Definition: scope_guard.hpp:76
ystdex::details::state_guard_traits< _type, _tToken, true >::restore
static void restore(_tToken t, _type &val)
Definition: scope_guard.hpp:151
ystdex::noncopyable
不可复制对象：禁止派生类调用默认原型的复制构造函数和复制赋值操作符。
Definition: base.h:41
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:969
ystdex::state_guard< _type, void, _tToken >::~state_guard
~state_guard()
Definition: scope_guard.hpp:319
ystdex::details::state_guard_impl::value_type
_type value_type
Definition: scope_guard.hpp:165
ystdex::details::state_guard_impl::token_type
_tToken token_type
Definition: scope_guard.hpp:166
YSLib::IO::f
const string &path const String &path _func f
Definition: YSLib/Service/FileSystem.h:430
CHRLib::args
_tParams && args
Definition: CharacterProcessing.h:538
ystdex::details::state_guard_impl::~state_guard_impl
~state_guard_impl()
Definition: scope_guard.hpp:177
ystdex::make_guard
guard< _type > make_guard(_type f)
Definition: scope_guard.hpp:83
ystdex::state_guard< _type, void, _tToken >::state_guard
state_guard(token_type t, _tParams &&...args)
Definition: scope_guard.hpp:311
ystdex::and_
逻辑操作元函数。
Definition: type_traits.hpp:394
ystdex::unique_guard
guard< one_shot< _type > > unique_guard(_type f)
Definition: scope_guard.hpp:92
platform::bool
std::FILE *fp bool
Definition: FileIO.h:1187
ystdex::state_guard::dismiss
void dismiss()
Definition: scope_guard.hpp:280