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:
46  : logic_error("Unsupported operation found.")
47  {}
48  template<typename _type>
49  unsupported(_type&& arg)
50  : logic_error(yforward(arg))
51  {}
52 };
53 
54 
57 {
58 public:
60  : unsupported("Unimplemented operation found.")
61  {}
62  template<typename _type>
63  unimplemented(_type&& arg)
64  : unsupported(yforward(arg))
65  {}
66 };
68 
69 
76 {
77 protected:
82  yconstfn
83  noncopyable() = default;
87  ~noncopyable() = default;
88 
89 public:
93  yconstfn
94  noncopyable(const noncopyable&) = delete;
95 
100  operator=(const noncopyable&) = delete;
101 };
102 
103 
104 /*
105 \brief 不可转移对象:禁止继承此类的对象调用默认原型的转移构造函数和转移赋值操作符。
106 \warning 非虚析构。
107 \since build 373
108 */
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 
143 {
144 public:
145 #if YB_IMPL_MSCPP
148  {};
149 #endif
150  virtual cloneable*
151  clone() const = 0;
152 
153  virtual
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>
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,
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&
277 {
278  static _type obj;
279 
280  return obj;
281 }
288 template<typename _type, size_t...>
289 inline _type&
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>
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 
379  {
380  if(--get_count() == 0)
381  delete get_object_ptr();
382  }
383 
384  static object_type&
385  get() ynothrow
386  {
388  return *get_object_ptr();
389  }
390 
391 private:
392  static size_t&
394  {
395  ythread size_t count;
396 
397  return count;
398  }
399  static object_type*&
401  {
402  ythread object_type* ptr;
403 
404  return ptr;
405  }
406 
407 public:
408  static size_t
410  {
411  return get_count();
412  }
414 };
415 
416 
437 template<typename _type, typename _tOnceFlag>
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  }
450  {
452  }
453 
454  static object_type&
455  get()
456  {
458  return *get_object_ptr();
459  }
460 
461 private:
462  static flag_type&
464  {
465  static flag_type flag;
466 
467  return flag;
468  }
469 
470  static object_type*&
472  {
473  static object_type* ptr;
474 
475  return ptr;
476  }
477 
478  static flag_type&
480  {
481  static flag_type flag;
482 
483  return flag;
484  }
485 
486  template<typename... _tParams>
487  static void
488  init(_tParams&&... args)
489  {
491  }
492 
493  static void
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 
使用引用计数的静态初始化管理器。
Definition: utility.hpp:364
virtual ~cloneable()
Definition: utility.hpp:154
unimplemented(_type &&arg)
Definition: utility.hpp:63
noncopyable()=default
protected 构造:默认实现。
size_t arrlen(_type(&)[_vN])
计算指定数组类型对象的长度。
Definition: utility.hpp:196
static void init(_tParams &&...args)
Definition: utility.hpp:488
static size_t & get_count()
Definition: utility.hpp:393
yconstfn const string _tParams && args
Definition: Loader.h:111
异常:不支持的操作。
Definition: utility.hpp:42
包装类类型的值的对象。
Definition: utility.hpp:215
nonmovable & operator=(const nonmovable &)=delete
禁止赋值复制。
ISO C 断言/调试跟踪扩展。
const class ystdex::nullptr_t nullptr
static flag_type & get_uninit_flag()
Definition: utility.hpp:479
static object_type *& get_object_ptr()
Definition: utility.hpp:471
可动态复制的抽象基类。
Definition: utility.hpp:142
static void uninit()
Definition: utility.hpp:494
qualified_decay< _type >::type decay_forward(_type &&arg)
退化传递。
Definition: utility.hpp:182
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:722
使用 call_once 的静态初始化管理器。
Definition: utility.hpp:438
nonmovable()=default
protected 构造:默认实现。
_tOnceFlag flag_type
Definition: utility.hpp:442
nifty_counter(_tParams &&...args)
Definition: utility.hpp:370
不可复制对象:禁止派生类调用默认原型的复制构造函数和复制赋值操作符。
Definition: utility.hpp:75
#define yassume
假定:环境语义。
Definition: cassert.h:58
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
noncopyable & operator=(const noncopyable &)=delete
禁止赋值复制。
static object_type *& get_object_ptr()
Definition: utility.hpp:400
#define YB_API
YBase 应用程序编程接口:用于向库文件约定链接。
Definition: ydef.h:391
conditional_t< std::is_class< _type >::value, _type, boxed_value< _type >> classify_value_t
包装非类类型为类类型。
Definition: utility.hpp:244
boxed_value(_tParams &&...args)
Definition: utility.hpp:221
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
_tWidget _fCallable && f
Definition: ywgtevt.h:597
~noncopyable()=default
protected 析构:默认实现。
auto get_init(_fInit &&f, _tParams &&...args) -> decltype(f(std::forward< decltype(args)>(args)...))&
取类型标识和初始化调用指定的对象。
Definition: utility.hpp:310
call_once_init(_tParams &&...args)
Definition: utility.hpp:445
_type & parameterize_static_object()
类型参数化静态对象。
Definition: utility.hpp:276
异常:未实现的操作。
Definition: utility.hpp:56
~nonmovable()=default
protected 析构:默认实现。
C++ 类型操作。
#define ythread
线程局部存储:若实现支持,指定为 thread_local 。
Definition: ydef.h:539
void call_once(bool &b, _fCallable &&f, _tParams &&...args)
按标识调用函数,保证调用一次。
Definition: utility.hpp:258
static flag_type & get_init_flag()
Definition: utility.hpp:463
unsupported(_type &&arg)
Definition: utility.hpp:49
static size_t use_count()
Definition: utility.hpp:409
decay_t< _type > decay_copy(_type &&arg)
退化复制。
Definition: utility.hpp:168