YSTest: utility.hpp 源文件

YSTest PreAlpha_b500_20140530 The YSLib Test Project

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

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
utility.hpp
浏览该文件的文档.
1 /*
2 © 2010-2014 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
28 #ifndef YB_INC_ystdex_utility_hpp_
29 #define YB_INC_ystdex_utility_hpp_ 1
30
31 #include "type_op.hpp" // for ../ydef.h, ystdex::qualified_decay;
32 #include "cassert.h"
33 #include <utility>
34 #include <stdexcept> // for std::logic_error;
35
36 namespace ystdex
37 {
38
40
41 class YB_API unsupported : public std::logic_error
43 {
44 public:
45 unsupported()
46 : logic_error("Unsupported operation found.")
47 {}
48 template<typename _type>
49 unsupported(_type&& arg)
50 : logic_error(yforward(arg))
51 {}
52 };
53
54
56 class YB_API unimplemented : public unsupported
57 {
58 public:
59 unimplemented()
60 : unsupported("Unimplemented operation found.")
61 {}
62 template<typename _type>
63 unimplemented(_type&& arg)
64 : unsupported(yforward(arg))
65 {}
66 };
68
69
75 class noncopyable
76 {
77 protected:
82 yconstfn
83 noncopyable() = default;
87 ~noncopyable() = default;
88
89 public:
93 yconstfn
94 noncopyable(const noncopyable&) = delete;
95
99 noncopyable&
100 operator=(const noncopyable&) = delete;
101 };
102
103
104 /*
105 \brief 不可转移对象：禁止继承此类的对象调用默认原型的转移构造函数和转移赋值操作符。
106 \warning 非虚析构。
107 \since build 373
108 */
109 class nonmovable
110 {
111 protected:
116 yconstfn
117 nonmovable() = default;
121 ~nonmovable() = default;
122
123 public:
127 yconstfn
128 nonmovable(const nonmovable&) = delete;
129
133 nonmovable&
134 operator=(const nonmovable&) = delete;
135 };
136
137
142 class YB_API cloneable
143 {
144 public:
145 #if YB_IMPL_MSCPP
146 cloneable(cloneable&&)
148 {};
149 #endif
150 virtual cloneable*
151 clone() const = 0;
152
153 virtual
154 ~cloneable()
155 {}
156 };
157
158
166 template<typename _type>
167 decay_t<_type>
168 decay_copy(_type&& arg)
169 {
170 return std::forward<_type>(arg);
171 }
172
173
180 template<typename _type>
181 typename qualified_decay<_type>::type
182 decay_forward(_type&& arg)
183 {
184 return std::forward<_type>(arg);
185 }
186
187
194 template<typename _type, size_t _vN>
195 yconstfn size_t
196 arrlen(_type(&)[_vN])
197 {
198 return _vN;
199 }
200 template<typename _type, size_t _vN>
201 yconstfn size_t
202 arrlen(_type(&&)[_vN])
203 {
204 return _vN;
205 }
207
208
214 template<typename _type>
215 struct boxed_value
216 {
217 _type value;
218
219 template<typename... _tParams>
220 yconstfn
221 boxed_value(_tParams&&... args)
222 : value(yforward(args)...)
223 {}
224
225 operator _type&() ynothrow
226 {
227 return value;
228 }
229
230 operator const _type&() const ynothrow
231 {
232 return value;
233 }
234 };
235
236
242 template<typename _type>
243 using classify_value_t = conditional_t<std::is_class<_type>::value, _type,
244 boxed_value<_type>>;
245
246
256 template<typename _fCallable, typename... _tParams>
257 inline void
258 call_once(bool& b, _fCallable&& f, _tParams&&... args)
259 {
260 if(!b)
261 {
262 f(yforward(args)...);
263 b = true;
264 }
265 }
266
267
274 template<typename _type, typename, typename...>
275 inline _type&
276 parameterize_static_object()
277 {
278 static _type obj;
279
280 return obj;
281 }
288 template<typename _type, size_t...>
289 inline _type&
290 parameterize_static_object()
291 {
292 static _type obj;
293
294 return obj;
295 }
296
297
307 template<typename _tKey, typename... _tKeys, typename _fInit,
308 typename... _tParams>
309 inline auto
310 get_init(_fInit&& f, _tParams&&... args) -> decltype(f(yforward(args)...))&
311 {
312 using obj_type = decltype(f(yforward(args)...));
313
314 auto& p(ystdex::parameterize_static_object<obj_type*, _tKey, _tKeys...>());
315
316 if(!p)
317 p = new obj_type(f(yforward(args)...));
318 return *p;
319 }
328 template<size_t... _vKeys, typename _fInit, typename... _tParams>
329 inline auto
330 get_init(_fInit&& f, _tParams&&... args) -> decltype(f(yforward(args)...))&
331 {
332 using obj_type = decltype(f(yforward(args)...));
333
334 auto& p(ystdex::parameterize_static_object<obj_type*, _vKeys...>());
335
336 if(!p)
337 p = new obj_type(f(yforward(args)...));
338 return *p;
339 }
340
341
363 template<class _type>
364 class nifty_counter
365 {
366 public:
367 using object_type = _type;
368
369 template<typename... _tParams>
370 nifty_counter(_tParams&&... args)
371 {
372 if(get_count()++ == 0)
373 get_object_ptr() = new _type(yforward(args)...);
374 }
376
377 ~nifty_counter()
379 {
380 if(--get_count() == 0)
381 delete get_object_ptr();
382 }
383
384 static object_type&
385 get() ynothrow
386 {
387 yassume(get_object_ptr());
388 return *get_object_ptr();
389 }
390
391 private:
392 static size_t&
393 get_count() ynothrow
394 {
395 ythread size_t count;
396
397 return count;
398 }
399 static object_type*&
400 get_object_ptr() ynothrow
401 {
402 ythread object_type* ptr;
403
404 return ptr;
405 }
406
407 public:
408 static size_t
409 use_count() ynothrow
410 {
411 return get_count();
412 }
414 };
415
416
437 template<typename _type, typename _tOnceFlag>
438 class call_once_init
439 {
440 public:
441 using object_type = _type;
442 using flag_type = _tOnceFlag;
443
444 template<typename... _tParams>
445 call_once_init(_tParams&&... args)
446 {
447 call_once(get_init_flag(), init<_tParams...>, yforward(args)...);
448 }
449 ~call_once_init()
450 {
451 call_once(get_uninit_flag(), uninit);
452 }
453
454 static object_type&
455 get()
456 {
457 yassume(get_object_ptr());
458 return *get_object_ptr();
459 }
460
461 private:
462 static flag_type&
463 get_init_flag()
464 {
465 static flag_type flag;
466
467 return flag;
468 }
469
470 static object_type*&
471 get_object_ptr()
472 {
473 static object_type* ptr;
474
475 return ptr;
476 }
477
478 static flag_type&
479 get_uninit_flag()
480 {
481 static flag_type flag;
482
483 return flag;
484 }
485
486 template<typename... _tParams>
487 static void
488 init(_tParams&&... args)
489 {
490 get_object_ptr() = new object_type(yforward(args)...);
491 }
492
493 static void
494 uninit()
495 {
496 delete get_object_ptr();
497 }
498 };
499
500 } // namespace ystdex;
501
502 #if !YB_HAS_BUILTIN_NULLPTR
503 using ystdex::nullptr;
504 #endif
505
506 #endif
507
ystdex::nifty_counter
使用引用计数的静态初始化管理器。
Definition: utility.hpp:364
ystdex::cloneable::~cloneable
virtual ~cloneable()
Definition: utility.hpp:154
ystdex::nonmovable
Definition: utility.hpp:109
ystdex::unimplemented::unimplemented
unimplemented(_type &&arg)
Definition: utility.hpp:63
ystdex::noncopyable::noncopyable
noncopyable()=default
protected 构造：默认实现。
ystdex::arrlen
size_t arrlen(_type(&)[_vN])
计算指定数组类型对象的长度。
Definition: utility.hpp:196
ystdex::call_once_init::init
static void init(_tParams &&...args)
Definition: utility.hpp:488
ystdex::nifty_counter::get_count
static size_t & get_count()
Definition: utility.hpp:393
YSLib::UI::args
yconstfn const string _tParams && args
Definition: Loader.h:111
ystdex::unsupported
异常：不支持的操作。
Definition: utility.hpp:42
ystdex::boxed_value
包装类类型的值的对象。
Definition: utility.hpp:215
ystdex::nonmovable::operator=
nonmovable & operator=(const nonmovable &)=delete
禁止赋值复制。
cassert.h
ISO C 断言/调试跟踪扩展。
ystdex::nullptr
const class ystdex::nullptr_t nullptr
ystdex::call_once_init::get_uninit_flag
static flag_type & get_uninit_flag()
Definition: utility.hpp:479
ystdex::call_once_init::get_object_ptr
static object_type *& get_object_ptr()
Definition: utility.hpp:471
ystdex::cloneable
可动态复制的抽象基类。
Definition: utility.hpp:142
ystdex::call_once_init::uninit
static void uninit()
Definition: utility.hpp:494
ystdex::decay_forward
qualified_decay< _type >::type decay_forward(_type &&arg)
退化传递。
Definition: utility.hpp:182
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:722
ystdex::call_once_init
使用 call_once 的静态初始化管理器。
Definition: utility.hpp:438
ystdex::nifty_counter::~nifty_counter
~nifty_counter()
Definition: utility.hpp:378
ystdex::nonmovable::nonmovable
nonmovable()=default
protected 构造：默认实现。
ystdex::call_once_init::flag_type
_tOnceFlag flag_type
Definition: utility.hpp:442
ystdex::unimplemented::unimplemented
unimplemented()
Definition: utility.hpp:59
ystdex::nifty_counter::nifty_counter
nifty_counter(_tParams &&...args)
Definition: utility.hpp:370
ystdex::noncopyable
不可复制对象：禁止派生类调用默认原型的复制构造函数和复制赋值操作符。
Definition: utility.hpp:75
yassume
#define yassume
假定：环境语义。
Definition: cassert.h:58
ystdex::unsupported::unsupported
unsupported()
Definition: utility.hpp:45
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
ystdex::noncopyable::operator=
noncopyable & operator=(const noncopyable &)=delete
禁止赋值复制。
ystdex::nifty_counter::get_object_ptr
static object_type *& get_object_ptr()
Definition: utility.hpp:400
YB_API
#define YB_API
YBase 应用程序编程接口：用于向库文件约定链接。
Definition: ydef.h:391
ystdex::classify_value_t
conditional_t< std::is_class< _type >::value, _type, boxed_value< _type >> classify_value_t
包装非类类型为类类型。
Definition: utility.hpp:244
ystdex::call_once_init::object_type
_type object_type
Definition: utility.hpp:441
ystdex::call_once_init::~call_once_init
~call_once_init()
Definition: utility.hpp:449
ystdex::boxed_value::boxed_value
boxed_value(_tParams &&...args)
Definition: utility.hpp:221
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
YSLib::UI::f
_tWidget _fCallable && f
Definition: ywgtevt.h:597
ystdex::noncopyable::~noncopyable
~noncopyable()=default
protected 析构：默认实现。
ystdex::get_init
auto get_init(_fInit &&f, _tParams &&...args) -> decltype(f(std::forward< decltype(args)>(args)...))&
取类型标识和初始化调用指定的对象。
Definition: utility.hpp:310
ystdex::call_once_init::call_once_init
call_once_init(_tParams &&...args)
Definition: utility.hpp:445
ystdex::parameterize_static_object
_type & parameterize_static_object()
类型参数化静态对象。
Definition: utility.hpp:276
ystdex::unimplemented
异常：未实现的操作。
Definition: utility.hpp:56
ystdex::nonmovable::~nonmovable
~nonmovable()=default
protected 析构：默认实现。
ystdex::nifty_counter::object_type
_type object_type
Definition: utility.hpp:367
ystdex::boxed_value::value
_type value
Definition: utility.hpp:217
type_op.hpp
C++ 类型操作。
ythread
#define ythread
线程局部存储：若实现支持，指定为 thread_local 。
Definition: ydef.h:539
ystdex::call_once
void call_once(bool &b, _fCallable &&f, _tParams &&...args)
按标识调用函数，保证调用一次。
Definition: utility.hpp:258
ystdex::any_ops::clone
Definition: any.h:310
ystdex::call_once_init::get_init_flag
static flag_type & get_init_flag()
Definition: utility.hpp:463
ystdex::unsupported::unsupported
unsupported(_type &&arg)
Definition: utility.hpp:49
ystdex::nifty_counter::use_count
static size_t use_count()
Definition: utility.hpp:409
ystdex::decay_copy
decay_t< _type > decay_copy(_type &&arg)
退化复制。
Definition: utility.hpp:168